Thursday, July 25, 2013

How ZF2's SharedEventManager Identifies Which Class to Listen to.

This is documented in the ZF2 official docs but I just want to save my thoughts while I was messing with the EventManager.

Problem: I was trying to grab an EventManager instance from 2 different modules.

Solution/Explanation:
The Zend\EventManager\EventManager class in ZF2 is not a singleton so EVERY module has its own instance of the EventManager. Therefore you cannot trigger events from other modules!
 
That is where the Zend\EventManager\SharedEventManager comes to the rescue although it itself is not a singleton you may ask but it is managed by Zend\EventManager\StaticEventManager which is one.

Confusion:
// Listener
$eventManager->getSharedManager()->attach('TriggerClass', 'shout', function ($e) {
    // do something
});
vs
// Listener
$eventManager->attach('shout', function ($e) {
    // do something

});
Please notice the extra argument of attach() TriggerClass. That is how the Shared Event Manager can find which shout event to trigger.

You may asked though how can the ShareEventManager find the event by just passing a string to attach() when the target trigger just reads like this:
class TriggerClass {
    public function shout()
    {
        $this->eventManager->trigger('shout', $this, array('Hey you!')); // notice i am NOT attaching to the SharedEventManager
    }
}
Sure it maps it using the same classname (although it does not matter) named TriggerClass but what if I want to use a different id like this one below to my listener and still want to trigger the same event?
// Listener
$eventManager->getSharedManager()->attach('Blah', 'shout', function ($e) {
    // do something
});
This is where the setIdentifiers() comes in. Back when implementing the setEventManager() of EventManagerInterface set the identifiers like below:
    public function setEventManager(EventManager\EventManagerInterface $events)
    {
        $events->setIdentifiers(array(
            'Blah'
        ));

        $this->events = $events;
        return $this;
    }
Now you can have a listener:
$this->getEventManager()->getSharedManager()->attach('Blah' ,'shout', function ($e) {
    var_dump($e->getParams());
    var_dump($e->getTarget());
 });
That is how the SharedEventManager maps out which Event to trigger.

12 comments:

Unknown said...

Sergei , October 22, 1970 Born in Christian Louboutin Bois Dore Moscow State Ozherelye. Perhaps influenced by Cheap LV Handbags their parents worship warrior army, join the army in cheap jordans 1989, and into the elite airborne military service. 1991, by the influence of the Soviet Union, the uggs on sale Russian christian louboutin army general lack of funds, a large number ugg australia of Air Jordan 11 Gamma Blue soldiers seeking veterans, but under extremely difficult conditions still dedicated, praise superiors, and soon was sent to the famous christian louboutin shoes Ryazan Higher ugg Airborne Command School studies. After graduating in 1994, entered the famous Pskov 76th Guards Airborne discount christian louboutin Division, served as discount nike jordans the reconnaissance platoon, reconnaissance ugg soldes deputy company commander, company commander, battalion airborne regiment scouting director and other duties.From 2000 to Cheap Louis Vuitton Handbags 2004, where the forces have repeatedly ordered war with Chechen militants. Because of their opponents in order to form the squad activity and haunted impermanence, the Russians had to ugg boots mobilize elite troops set up large uggs outlet number of small units, "a Discount Louis Vuitton small wholesale jordan shoes play small" Implementation siege. commanding troops in Chechnya, Christian Louboutin Daffodile Ingushetia and other places over the mountains, to track the militants fled. Discount LV Handbags Although the militants were extremely vigilant, but as long as the task has never had outsmarted.August 8, 2008, Georgia suddenly invade South Ossetia (Russian peacekeepers stationed ugg pas cher there), the Russian military to respond quickly, including including the 76th Guards Airborne Division, more than christian louboutin remise 50% 3,000 people were immediately delivered to the pro-Russian Abkhazia, Georgia cheap nike jordan shoes Army contain two infantry brigades, effectively coordinate the cheap christian louboutin direction of the Russian Bags Louis Vuitton troops in South Ossetia.

Unknown said...

2015-09-16 zhengjx
Up To 70% Off Air Retro Jordan 8s
michael kors handbags
michael kors outlet
Coach Factory Outlet Discount Online
Abercrombie And Fitch Clothing Outlet at New York
uggs outlet
Michael Kors Outlet Factory Online Store
Cheap Jordan 13 Shoes For Sale Free Shipping
ralph lauren
canada goose jackets
toms shoes
Abercrombie And Fitch New York Outlet Store
fitflops
coach outlet
coach factory outlet
Louis Vuitton Outlet Handbags Pursers In USA
Michael Kors Outlet Online Store Clearance
Coach Factory Outlet Online Sale Discount
Original Michael Kors Handbags Outlet Online
Coach Outlet Store Online Clearance
Cheap Michael Kors Handbags On Sale
Official Cheap Michael Kors Handbags Outlet
ugg boots
louis vuitton
cheap nfl jerseys
Louis Vuitton Outlet Store Locations
abercrombie & fitch
Coach Factory Outlet Official Website
louis vuitton outlet
michael kors

Unknown said...

cheng0928
louis vuitton handbags, http://www.louisvuittonhandbag.us/
hermes birkin, http://www.hermesbirkin.org/
mbt shoes, http://www.mbtshoesoutlet.us.com/
swarovski outlet, http://www.swarovski-outlet.co.uk/
hogan, http://www.scarpehogan-outlet.it/
ray ban, http://www.occhiali-rayban.it/
nfl jerseys wholesale, http://www.nfljerseys-wholesale.us.com/
nba jerseys, http://www.nbajerseys.us.com/
longchamp handbags, http://www.longchamphandbag.us.com/
cheap nhl jerseys, http://www.nhljerseys.us.com/
cheap mlb jerseys, http://www.cheapmlbjerseys.net/
north face jackets, http://www.thenorthfaces.in.net/
cheap snapbacks, http://www.cheapsnapbacks.us.com/
adidas wings, http://www.adidaswings.net/
basketball shoes, http://www.basketballshoes.us.com/
lebron james shoes, http://www.lebronjamesshoes.in.net/
lacoste polo shirts, http://www.lacostepoloshirts.cc/
vans shoes, http://www.vans-shoes.cc/
ugg boots, http://www.uggboot.com.co/
hermes belt, http://www.hermesbelts.us/

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

chenlina said...

chenlina20151127
uggs boots
canada goose jackets
ugg outlet
louis vuitton handbags
gucci shoes
ray ban outlet store
mont blanc legend
michael kors handbags
ray ban sunglasses
louis vuitton handbags
cheap uggs
hollister co
kobe 9
adidas superstars
hollister
north face jackets
tory burch sale
ugg australia
louis vuitton handbags
timberland outlet
michael kors outlet online
canada goose jackets
louis vuitton outlet
hollister co,hollister jeans,hollister.com,hollister ca
coach outlet store online
michael kors outlet
louis vuitton purses
michael kors outlet
uggs on sale
adidas originals
louis vuitton
louis vuitton handbags
michael kors outlet online
giuseppe zanotti
nike roshe run women
ugg boots on sale
michaek kors outlet online
nike huarache shoes
canada goose jackets
nike trainers uk
as

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

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
soccer shoes
true religion jeans outlet
pandora outlet
abercrombie & fitch
cheap nfl jerseys
michael kors outlet
michael kors outlet online
nike free runs
canada goose outlet
kobe bryant shoes
louis vuitton outlet
ugg outlet store
gucci shoes
oakley sunglasses cheap
lebron james shoes 13
michael kors outlet online
coach factory outlet
cheap uggs
louis vuitton bags
tory burch sale
hollister clothing
chanel handbags
oakley sunglasses
air jordans
pandora jewelry
cheap jordan shoes
cheap nfl jerseys
michael kors handbags
ray ban sunglasses
uggs for women
true religion jeans
kids lebron shoes
ray ban sunglasses outlet
ugg outlet
timberland outlet
replica watches
toms shoes
kobe 9
uggs outlet
michael kors uk
as

Unknown said...

20161021 junda
michael kors outlet
oakley sunglasses
ugg sale
juicy couture outlet
longchamp pas cher
tory burch outlet
true religion jeans
rolex watches
swarovski crystal
uggs outlet

Karen said...

The NFL Flag for Tennessee Titans is viewable from both sides with the opposite side being a reverse image.Fly this flag with any of our tailgate poles or 6' aluminum flagpoles and adjustable flag brackets.
house divided flags nhl
nfl house flags
nfl flags wholesalecheap Denver Broncos house flags, House Divided Flags,Sports Flags 3x5, Sports flags and banners,nfl House Divided Flags

Unknown said...

jets jersey
san antonio spurs jerseys
ray ban sunglasses
michael kors handbags online
michael kors outlet
ralph lauren
eagles jerseys
kobe 9
jordan shoes
packers jerseys

Unknown said...

mulberry purse
rolex replica
nike air max
pandora charms uk
michael kors outlet clearance
pandora jewelry
celine outlet store
ralph lauren outlet
adidas yeezy
michael kors handbags
mt0718