Saturday, September 7, 2013

How Does a ServiceManager Plugin gets Initialized?

Hopefully this post will explain and answer this question.

For this example, I will trace the initialization of the FilterManager Plugin.
For those who are not familiar with the plugin functionality of ZF2, it is basically an extension of the Service Manager that you can use for organization and other things.

As an example, you are calling a plugin manager when you do something like below:

// from Controller
$stringTrimFilter = $this->getServiceLocator()->get('FilterManager')->get('stringTrim');

// from inside a View. basePath is a View Helper Plugin
$this->basePath('/some/uri');

Now lets get down to business. So how does a plugin gets created and initialized?

1.) We will start from the ModuleManagerFactory for simplicity's sake. In the ModuleManagerFactory (https://github.com/zendframework/zf2/blob/master/library/Zend/Mvc/Service/ModuleManagerFactory.php) the Service Listener Factory gets initialized. (see line 38 and called on 44)

2.) In the Service Listener Factory (https://github.com/zendframework/zf2/blob/master/library/Zend/Mvc/Service/ServiceListenerFactory.php) a bunch of Plugin Factories gets initialized also.

One of the plugin factories that gets initialized is the FilterManager factory which looks like this. See line 54:

'FilterManager' => 'Zend\Mvc\Service\FilterManagerFactory',

3.) Lets go to the FilterManagerFactory (https://github.com/zendframework/zf2/blob/master/library/Zend/Mvc/Service/FilterManagerFactory.php).The class is basically a ServiceManager Factory implementation class.

4.) The FilterManagerFactory file extends AbstractPluginManagerFactory (https://github.com/zendframework/zf2/blob/master/library/Zend/Mvc/Service/AbstractPluginManagerFactory.php) which is basically an implementation of FactoryInterface.php. That means we are instantiating an object. What object?

5) The FilterManagerFactory will INSTANTIATE and RETURN an instance of FilterPluginManager (https://github.com/zendframework/zf2/blob/master/library/Zend/Filter/FilterPluginManager.php) see line 17:

const PLUGIN_MANAGER_CLASS = 'Zend\Filter\FilterPluginManager';

6.) Now an instance of FilterPluginManager is created. It is important to note that FilterPluginManager extends AbstractPluginManager (https://github.com/zendframework/zf2/blob/master/library/Zend/ServiceManager/AbstractPluginManager.php) which is basically an own ServiceManager class!

The FilterPluginManager class also invokes a bunch of ZF2 filters. The class also gets mapped to the service key "FilterManager" key. Take a look at step 2 again.

Moreover this class implements the abstract method validatePlugin() which is used to validate the actual plugin object that you will inject to this manager like for example, StringTrim!

    public function validatePlugin($plugin)
    {
        if ($plugin instanceof FilterInterface) {
            // we're okay
            return;
        }
        if (is_callable($plugin)) {
            // also okay
            return;
        }
        throw new Exception\RuntimeException(sprintf(
            'Plugin of type %s is invalid; must implement %s\FilterInterface or be callable',
            (is_object($plugin) ? get_class($plugin) : gettype($plugin)),
            __NAMESPACE__
        ));
    }

7.) Now we are back in the ModuleManagerFactory (see step 1). Since we now have an instance of the FilterPluginManager via the key FilterManager, this instance is added to the Service Listener (see line 76 of ModuleManagerFactory.php) or below:

$serviceListener->addServiceManager(
    'FilterManager',
    'filters',
    'Zend\ModuleManager\Feature\FilterProviderInterface',
    'getFilterConfig'
 );

8.) Lets take a look at the FilterProviderInterface (https://github.com/zendframework/zf2/blob/master/library/Zend/ModuleManager/Feature/FilterProviderInterface.php) which difines the method getFilterConfig(). What is this method again? This is where you register your custom filters remember in Module.php?
public function getFilterConfig()
{
    return array(
        'factories' => array(
            'customFilter' => function ($sm) {
                // create your filter...
            }
        ),         
    )
}

9.) Thats pretty much the workflow and registration of a ServiceManager Plugin. Now you can call it like this:

$stringTrimFilter = $this->getServiceLocator()->get('FilterManager')->get('stringTrim');
// or your custom filter
$customfilter = $this->getServiceLocator()->get('FilterManager')->get('customFilter');

18 comments:

Guo Guo said...

yao20150324
michael kors handbags
thomas sabo
valentino shoes
ralph lauren outlet
mac cosmetics
true religion outlet
tods outlet
adidas wings
iphone 6 cases
thomas sabo uk
hollister clothing
ray ban uk
ralph lauren outlet
abercrombie
cheap oakley sunglasses
mont blanc
hollister shirts
burberry outlet online
burberry handbags
replica watches
converse shoes
ghd
tods outlet
toms shoes
lacoste shirts
michael kors outlet online
michael kors handbags
louis vuitton outlet
ferragamo shoes
soccer jerseys
michael kors outlet
ray ban sunglasses

Unknown said...

tory burch shoes
new york jets jerseys
prada sneakers
boston celtics jersey
bobby orr blackhawks jersey,jeremy roenick authentic jersey,jonathan toews blackhawks jersey,stan mikita blackhawks jersey,corey crawford blackhawks jersey,michael jordan jersey,michal handzus jersey,peter regin jersey
the north face
ugg boots
nike free run uk
michael kors handbags
the north face outlet
real madrid jersey
chanel handbags
air jordan shoes
christian louboutin uk
jeremy maclin jersey,jamaal charles jersey,joe montana jersey,justin houston jersey,dontari poe jersey,eric berry jersey
ralph lauren,ralph lauren uk,ralph lauren outlet,ralph lauren outlet online,polo ralph lauren outlet,ralph lauren polo
ahmad bradshaw jersey,josh mcnary jersey,andrew luck jersey,donte moncrief jersey,delano howell jersey,robert mathis jersey,andrew luck jersey,trent richardson jersey
nike free
eric decker jersey,muhammad wilkerson jersey,michael vick jersey,geno smith jersey,dennis byrd jersey,jace amaro jersey,calvin pace jersey,curtis martin jersey,
north face jackets,north face,the north face,northface,north face outlet,north face jackets clearance,the north face
patriots jerseys
warriors jerseys
nike air max,nike roshe,nike huarache
swarovski crystal
kobe bryant shoes
san antonio spurs jerseys
canada goose jackets
los angeles lakers jerseys
kate spade sale
coach outlet
zhuweiming0627

chenlina said...

chenlina20150711
toms shoes
michael kors handbags
louis vuitton outlet
louis vuitton outlet
oakley vault
kids lebron james shoes
longchamp outlet
ray ban sunglasses outlet
abercrombie and fitch
true religion
longchamp pas cher
fitflop sale
air max uk
michael kors outlet
louboutins
oakley sunglasses wholesale
true religion outlet
ray-ban wayfarer
louis vuitton
coach outlet
gucci bags
air jordans
ray ban glasses
michael kors outlet online sale
ed hardy outlet
pandora jewelry
louis vuitton handbags
oakley sunglass
mont blanc pens
jordan 8 playoffs
coach factory outlet
michael kors handbags
michael kors outlet online
dior bags
lululemon sale
jordan pas cher
tods outlet
p90x
aviator sunglasses
tods outlet store

Hua Cai said...

tommy hilfiger outlet
coach outlet
longchamp handbags
calvin klein underwear
the north face jackets
cheap nfl jerseys
moncler coats
michael kors uk
air jordan 13
jordan 4
coach outlet online
swarovski jewelry
los angeles lakers jerseys
minnesota vikings jerseys
nhl jerseys
chelsea jersey
cheap oakley sunglasses
swarovski crystal
oakley sunglasses
fitflop
tods shoes
burberry handbags
louis vuitton outlet
ray ban uk
barcelona jersey
mont blanc
michael kors handbags
oakland raiders jerseys
gucci outlet
tods outlet
nike trainers
custom mlb jerseys
iphone 6 plus cases
chicago blackhawks jersey
true religion jeans
michael kors outlet
kate spade outlet
ghd
michael kors handbags
tiffany jewelry
2015717caihauli

mao qiuyun said...

ray ban sunglasses, http://www.raybansunglassesonline.us.com/
nike trainers, http://www.niketrainers.me.uk/
gucci handbags, http://www.guccihandbags-outlet.co.uk/
herve leger, http://www.herveleger.us.com/
timberland boots, http://www.timberlandboots.name/
coach outlet online, http://www.coachoutletonline-store.us.com/
michael kors outlet, http://www.michaelkorsoutletcanada.in.net/
juicy couture outlet, http://www.juicycoutureoutlet.net/
calvin klein underwear, http://www.calvinklein.in.net/
ray ban sunglasses, http://www.raybansunglass.us.com/
prada outlet, http://www.pradaoutlet.us/
lululemon outlet, http://www.lululemonoutletstore.in.net/
tory burch shoes, http://www.toryburchshoesoutlet.com/
michael kors outlet, http://www.michaelkorsoutlet.org.uk/
replica watches, http://www.replicawatchesforsale.us.com/
pandora jewelry, http://www.pandorajewelryoutlet.us.com/
nfl jerseys wholesale, http://www.nfljerseys-wholesale.us.com/
michael kors outlet, http://www.michaelkorsoutletonlinstore.us.com/
oakley outlet, http://www.oakleyoutlet.in.net/
oakley sunglasses, http://www.oakleysunglasses-outlet.us.com/
michael kors bags, http://www.michaelkorsbags.uk/
ray ban sunglasses, http://www.ray-bansunglasses.eu.com/
oakley sunglasses, http://www.oakley.in.net/
air max 2015, http://www.airmax2015.in.net/
the north face uk, http://www.thenorthfaces.org.uk/
swarovski outlet, http://www.swarovski-outlet.co.uk/
pandora jewelry, http://www.pandora.eu.com/
chanel handbags, http://www.chanelhandbags-outlet.co.uk/
1002maoqiuyun

oakleyses said...

tory burch outlet, longchamp outlet, burberry, ray ban sunglasses, tiffany and co, nike outlet, replica watches, louis vuitton, ray ban sunglasses, nike air max, jordan shoes, polo ralph lauren outlet, michael kors outlet, tiffany jewelry, michael kors outlet, christian louboutin outlet, burberry outlet online, michael kors outlet, michael kors, oakley sunglasses, longchamp, prada outlet, prada handbags, cheap oakley sunglasses, oakley sunglasses, oakley sunglasses, ugg boots, louboutin shoes, michael kors outlet, ugg boots, polo ralph lauren outlet, louis vuitton outlet, nike free, chanel handbags, louboutin outlet, louis vuitton outlet, oakley sunglasses, ugg boots, replica watches, michael kors outlet, kate spade outlet, uggs on sale, nike air max, louboutin, louis vuitton, louis vuitton

oakleyses said...

air jordan pas cher, ray ban uk, nike roshe, true religion jeans, air force, abercrombie and fitch, north face, new balance pas cher, sac longchamp, longchamp pas cher, mulberry, oakley pas cher, nike free run uk, coach outlet, nike blazer, hermes, michael kors, nike air max, air max, hollister pas cher, true religion jeans, ralph lauren pas cher, vans pas cher, michael kors, coach outlet, true religion jeans, hollister, michael kors, north face, nike air max, nike roshe run, coach purses, burberry, lululemon, michael kors, lacoste pas cher, hogan, tn pas cher, sac guess, replica handbags, louboutin pas cher, vanessa bruno, timberland, kate spade handbags, converse pas cher, ralph lauren uk, nike free

oakleyses said...

nike roshe, new balance, beats by dre, p90x workout, instyler, valentino shoes, nfl jerseys, babyliss, ghd, hollister, s5 cases, lululemon, soccer shoes, jimmy choo shoes, nike air max, mac cosmetics, giuseppe zanotti, abercrombie and fitch, hollister, herve leger, asics running shoes, nike huarache, celine handbags, louboutin, mont blanc, iphone 6s cases, north face outlet, birkin bag, iphone 5s cases, north face outlet, wedding dresses, mcm handbags, oakley, iphone 6 cases, ralph lauren, longchamp, timberland boots, nike trainers, baseball bats, soccer jerseys, iphone 6s plus cases, insanity workout, iphone 6 plus cases, iphone cases, bottega veneta, chi flat iron, reebok shoes, ipad cases, vans shoes, ferragamo shoes

oakleyses said...

moncler, moncler outlet, marc jacobs, moncler, hollister, montre pas cher, vans, ray ban, replica watches, pandora charms, louis vuitton, supra shoes, sac louis vuitton pas cher, pandora jewelry, swarovski, pandora charms, moncler, bottes ugg, barbour, canada goose, ugg,uggs,uggs canada, louis vuitton, doke gabbana outlet, converse, louis vuitton, lancel, louis vuitton, hollister, moncler, pandora jewelry, doudoune canada goose, juicy couture outlet, nike air max, converse outlet, barbour jackets, moncler, ugg,ugg australia,ugg italia, links of london, moncler, gucci, canada goose outlet, canada goose, canada goose outlet, coach outlet, canada goose uk, ugg boots uk, canada goose, juicy couture outlet, ugg pas cher, wedding dresses, canada goose

chenlina said...

chenlina20160127
michael kors outlet
michael kors outlet online
polo ralph lauren
oakkey sunglasses
swarovski outlet
louis vuitton outlet
jordan retro
louis vuitton handbags
abercrombie outlet
oakley sunglasses
jordan retro 11
toms outlet
coach outlet online
coach outlet
burberry scarf
coach factory outlet
coach factory outlet
ed hardy clothing
cheap oakley sunglasses
lebron shoes
louis vuitton handbags
adidas superstars
louis vuitton outlet
ray ban sunglasses outlet
cheap jordans
coach factory outlet
michael kors outlet
ugg sale
beats by dr dre
canada goose outlet
louis vuitton
christian louboutin shoes
oakley outlet
ray ban sunglasses
ugg outlett
oakley sunglasses
abercrombie and fitch
ray ban sunglasses
soccer shoes
true religion jeans outlet
as

柯云 said...

2016-07-20keyun
gucci outlet
kate spade handbags
ralph lauren clearance outlet
louis vuitton outlet stores
air jordan shoes
adidas wings
cheap jordan shoes
supra shoes
polo ralph lauren
louboutin shoes
hollister clothing
coach factory outlet
the north face jackets
louis vuitton bags
louis vuitton outlet stores
toms shoes outlet
ghd hair straighteners
cheap rolex watches
jordan concords
cheap toms
hermes outlet
louis vuitton outlet
ralph lauren
coach outlet
toms wedges
louis vuitton purses
true religion jeans
ray ban sunglasses outlet
michael kors handbags
louboutin shoes
kobe 8
pandora charms
coach outlet store online clearances
polo outlet
yeezy boost 350
coach factory outlet online
cheap louis vuitton handbags
nfl jerseys

Unknown said...

nike blazer pas cher
reebok outlet store
adidas nmd runner
longchamp handbags
adidas uk
uggs outlet
abercrombie and fitch
michael kors handbags
coach outlet
michael kors handbags
michael kors outlet online
ray ban sunglasses
longchamp solde
prada sunglasses
swarovski jewelry
hollister uk
ugg boots
football shirts
ugg australia
canada goose jackets
cheap ugg boots
lacoste pas cher
uggs outlet
michael kors outlet online
michael kors handbags
toms shoes
uggs outlet
coach outlet
coach outlet clearance
dior sunglasses
adidas wings
lululemon outlet
louis vuitton outlet
the north face jackets
true religion uk
chanyuan0913

roshini said...


Sage Support Phone Number
Sage 50 Tech Support Phone Number
Sage 50 Support Number
Sage 50 Technical Support Phone Number
Sage 50 Tech Support Number
Sage 50 Technical Support Phone Number
Sage 50 Technical Support Number
Sage 50 Customer Support Phone Number
Sage 50 Customer Service Number

Soni yadav said...

Aol Technical Support Phone Number
Aol Technical Support Number
Aol Tech Support Number
Aol Tech Support phone Number
Aol Support Phone Number
Aol Tech Support Number
Outlook Technical Support Phone Number
Outlook Technical Support Number
Outlook Tech Support Number
Outlook Tech Support Phone Number
Outlook Support Phone Number
Outlook Tech Support Number

Anjilo Jilo said...


Quicken Support Phone Number
Quicken Support Phone Number
Quicken Support Number
Quicken Tech Support Number
Quicken Tech Support Phone Number
Quicken Technical Support Number
Quicken Technical Support Phone Number
Quicken Technical Support Number
Quicken Tech Support Phone Number
Quicken Tech Support Number

mohan kumar said...

Quickbooks Support Phone Number
Quickbooks Pro Support Phone Number
Quickbooks Pro Tech Support Number
Quickbooks Pro Support Number
Quickbooks Pro Technical Support Number
Quickbooks Pro Tech Support Phone Number
Quickbooks Pro Technical Support Phone Number
Quickbooks Pro Help Desk Phone Number
Quickbooks Pro Help Desk Number
Quickbooks Pro Customer Service Number
Quickbooks Pro Customer Service Phone Number
Quickbooks Pro Customer Support Number
Quickbooks Pro Customer Support Phone Number

Kanika Jaiswal said...

QuickBooks Technician
Quickbooks Support Phone Number
Quickbooks Tech Support Number
Quickbooks Support Number
Quickbooks Technical Support Number
Quickbooks Tech Support Phone Number
Quickbooks Technical Support Phone Number
Quickbooks Customer Service Number
Quickbooks Customer Service Phone Number
Quickbooks Customer Support Number
Quickbooks Customer Support Phone Number
Quickbooks Help Desk Phone Number
Quickbooks Help Desk Number

Unknown said...

dolce and gabbana shoes
cheap michael kors handbags
hugo boss sale
saints jerseys
red valentino
oklahoma city thunder jerseys
moncler jackets
minnesota vikings jerseys
jaguars jersey
canada goose jackets