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');

12 comments:

Unknown 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

Unknown 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...

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

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

Unknown said...

20170402 junda
polo outlet stores
louis vuitton
cheap oakley sunglasses
air max uk
michael kors outlet clearance
coach factory outlet
ray ban sunglasses
louis vuitton handbags
fitflops sale clearance
longchamp handbags

caiyan said...

tommy hilfiger outlet
hollister clothing store
michael kors handbags outlet
ralph lauren outlet
jerseys cheap
fitflops clearance
adidas sneakers
christian louboutin outlet
polo ralph lauren
birkenstock shoes
0413shizhong

Unknown said...

snapbacks wholesale
gucci outlet online
coach outlet online
cheap oakley sunglasses
michael kors outlet online
ralph lauren outlet
michael kors outlet clearance
pandora jewellery
cheap ray ban sunglasses
michael kors outlet clearance
20170617lck

Unknown said...

adidas originals tubular
pandora charms
coach handbags
louboutin pas cher
louboutin heels
michael kors handbags
rolex replica watches
vibram fivefingers hurt
adidas yeezy
pandora outlet store
2017.6.27chenlixiang

Digital Marketing Agency said...

Quikcbooks Support Phone Number

Quikcbooks Support Number
Quikcbooks Support Phone Number
Quikcbooks Tech Support Number
Quikcbooks Technical Support Number
Quikcbooks Tech Support Phone Number
Quikcbooks Technical Support Phone Number
Quikcbooks Helpdesk Phone Number
Quikcbooks Customer Service Phone Number
Quikcbooks Customer Service Number
Quikcbooks Customer Support Number

Quicken Support Phone Number

Quicken Support Number
Quicken Support Phone Number
Quicken Tech Support Number
Quicken Technical Support Number
Quicken Tech Support Phone Number
Quicken Technical Support Phone Number
Quicken Helpdesk Phone Number
Quicken Customer Service Phone Number
Quicken Customer Service Number
Quicken Customer Support Number

Sage Support Phone Number

Sage Support Number
Sage Support Phone Number
SageTech Support Number
Sage Technical Support Number
Sage Tech Support Phone Number
Sage Technical Support Phone Number
Sage Helpdesk Phone Number
Sage Customer Service Phone Number
Sage Customer Service Number
Sage Customer Support Number

Unknown said...

Great article, thanks for sharing
cara mengobati eksim
obat herbal gondok
obat infeksi mata tradisional
obat penyakit lupus
obat penggemuk badan
obat herbal demam berdarah
cara mengobati jerawat
obat mata kuning tradisional

Hualala said...

Nike 270
Air Jordan 9
Air Jordan
Pandora Jewelry
Yeezy boost
Jordan 11
Air Jordan 11
Pandora Jewelry
Red Bottom Shoes
Paul20190327