zfcampus/zf-rest — pull request #38 by weierophinney (2014-07-15 21:49:54+00:00)
Title: Can't attach to a resource's event using the SharedEventManager (e.g. using identifier 'Status\V1\Rest\Status\StatusResource')

Transcript from IRC:
```
weierophinney: is there a way to attach to a resource's events e.g. fetchAll? I can't seem to be able to retrieve the resource's event manager to attach to
djule5: yes -- use the SharedEventManager, and use the resource's service name as the identifier: $shared->attach('SomeApi\V1\Rest\Foo\FooResource', 'fetchAll', $callback, $priority)
djule5: the EM instance is actually attached to a ZF\Rest\Resource instance -- your "resource" class is actually a listener on that object's EM instance! :)
djule5: do $priority > 1 if you want it to execute first, $priority < 1 if you want it to execute later.
weierophinney: ok, but I'm trying to attach to any resource's fetchAll, is there a way to do that?
weierophinney: I've been looking at RestControllerFactory and trying to attach to the same $services->get('EventManager') instance in my bootstrap method but that doesn't seems to be working
djule5: use the SharedEventManager when trying to listen to events on either a REST controller or resource; we use the service names as identifiers.
weierophinney: I see in the shared event manager the ZF\Rest\RestController entry with a dispatch event... but I don't see any for the resource... am I missing something?
weierophinney: I'm looking at this in xdebug
djule5: Resources do not have a dispatch event. ZF\Rest\Resource triggers events based on the methods -- so: create, update, replaceList, patch, patchList, delete, deleteList, fetch, fetchAll.
djule5: Rest _controllers_ trigger only dispatch.
weierophinney: yep, I get this 100%
weierophinney: so I'm trying to attach on the resource's fetchAll
djule5: essentially, listening on "dispatch" is for pre-filtering the incoming request, or post-filtering what the controller returns. The events triggered by ZF\Rest\Resource are for interacting with the specific methods. You would attach to your resource's service name as the identifier (e.g. Status\V1\Rest\Status\StatusResource) , and then "fetchAll" as the event name.
weierophinney: I think I understand that correctly... but is it normal I don't see the resource's event manager in the shared manager? see http://i.imgur.com/svelY9i.png
djule5: yes, that's normal. There's a different way to query to get that information; cannot recall off the top of my head.
weierophinney: okay, sorry I'm new to events... however I'm trying to do exactly what you're suggesting and I'm never stepping in my callback method
weierophinney: e.g. in my module onBootstrap method, I have $e->getApplication()->getEventManager()->getSharedManager()->attach('Status\V1\Rest\Status\StatusResource', 'fetchAll', function(){ die; });
weierophinney: but the callback doesn't get called :(
djule5: okay -- can you raise an issue against zf-rest with exactly that information, please? I think I see what's going on, and it's possible we're not injecting the identifier via our Resource factory.
weierophinney: yes, it seems like the identifiers are getting overwritten? they're being set on $events->setIdentifiers($resourceIdentifiers); but then overwritten in $resource->setEventManager($events);
weierophinney: right?
djule5: whichever one is called last wins. In this case, it looks like Resource::setEventManager is not aggregating the resource service name, and that's the real issue here.
djule5: I can correct it -- but give me an issue, so you can track when I get it done.
```