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

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 ( the Service Listener Factory gets initialized. (see line 38 and called on 44)

2.) In the Service Listener Factory ( 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 ( class is basically a ServiceManager Factory implementation class.

4.) The FilterManagerFactory file extends AbstractPluginManagerFactory ( 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 ( 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 ( 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
        if (is_callable($plugin)) {
            // also okay
        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)),

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:


8.) Lets take a look at the FilterProviderInterface ( 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');


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
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
michael kors handbags
tiffany jewelry

Unknown said...

ray ban sunglasses,
nike trainers,
gucci handbags,
herve leger,
timberland boots,
coach outlet online,
michael kors outlet,
juicy couture outlet,
calvin klein underwear,
ray ban sunglasses,
prada outlet,
lululemon outlet,
tory burch shoes,
michael kors outlet,
replica watches,
pandora jewelry,
nfl jerseys wholesale,
michael kors outlet,
oakley outlet,
oakley sunglasses,
michael kors bags,
ray ban sunglasses,
oakley sunglasses,
air max 2015,
the north face uk,
swarovski outlet,
pandora jewelry,
chanel handbags,

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

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

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

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