laminas/laminas-eventmanager

How i can get all events=>callbacks?

weierophinney opened this issue · 7 comments

in EventManager v2.* can i get all events names\callbacks for my application


Originally posted by @maxgu at zendframework/zend-eventmanager#22

I have dirty solution (in \Zend\EventManager\EventManager):

class EventManager implements EventManagerInterface
{
    // ...
    public static $allEvents = [];
    // ...
    public function attach($event, $callback = null, $priority = 1)
    {
        // ...
        if (empty($this->events[$event])) {
            $this->events[$event] = new PriorityQueue();
            self::$allEvents[$event] = new PriorityQueue();
        }

        // ...

        $this->events[$event]->insert($listener, $priority);
        self::$allEvents[$event]->insert($listener, $priority);
        return $listener;
    }

after this i see all :) but this is unacceptable and very dirty.


Originally posted by @maxgu at zendframework/zend-eventmanager#22 (comment)

@froschdesign
no, this not work, because some services have own EventManager:

$this->setEventManager(new EventManager());
// or
$this->events = new EventManager();

in my task i need get all (ALL in whole app) events and handlers.


Originally posted by @maxgu at zendframework/zend-eventmanager#22 (comment)

Now i have problem: I have big application with many modules, and when i see in code $this->getEventManager()->trigger($event); i have no idea what will be executed. I investigate this and spend many time with Crtl+F in whole project.

I want solve this faster if i have map: events=>callbacks+priorities, like this:

loadModules
    Zend\Loader\ModuleAutoloader::register 9000
    Zend\ModuleManager\Listener\LocatorRegistrationListener::onLoadModules -1000
    Zend\ModuleManager\Listener\ConfigListener::onloadModulesPre 1000
    Zend\ModuleManager\Listener\ConfigListener::onLoadModules -1000
    Zend\ModuleManager\ModuleManager::onLoadModules 1
loadModule.resolve
    Zend\ModuleManager\Listener\ModuleResolverListener
loadModule
    Zend\ModuleManager\Listener\AutoloaderListener
    Zend\ModuleManager\Listener\ModuleDependencyCheckerListener
    Zend\ModuleManager\Listener\InitTrigger
    Zend\ModuleManager\Listener\OnBootstrapListener
    Zend\ModuleManager\Listener\LocatorRegistrationListener::onLoadModule 1
    Zend\ModuleManager\Listener\ConfigListener::onLoadModule 1
    Zend\ModuleManager\Listener\ServiceListener::onLoadModule 1
mergeConfig
    Zend\ModuleManager\Listener\ConfigListener::onMergeConfig 1000
loadModules.post
    Zend\ModuleManager\Listener\ServiceListener::onLoadModulesPost 1
bootstrap
    Zend\Mvc\View\Http\ViewManager::onBootstrap 10000
route
    Zend\Mvc\RouteListener::onRoute 1
    Zend\Mvc\HttpMethodListener::onRoute 10000
    Zend\Mvc\ModuleRouteListener::onRoute 1
    callback function
    PhlySimplePage\Module::onRoutePost -100
    T4web\Authentication\Service\Checker::check -100
dispatch
    Application\Controller\IndexController::onDispatch 1
sendResponse
    Zend\Mvc\ResponseSender\PhpEnvironmentResponseSender
    Zend\Mvc\ResponseSender\ConsoleResponseSender
    Zend\Mvc\ResponseSender\SimpleStreamResponseSender
    Zend\Mvc\ResponseSender\HttpResponseSender
finish
    Zend\Mvc\SendResponseListener::sendResponse -10000
    callback function
renderer
    Zend\View\Strategy\PhpRendererStrategy::selectRenderer
response
    Zend\View\Strategy\PhpRendererStrategy::injectResponse
dispatch.error
    Zend\Mvc\View\Http\RouteNotFoundStrategy::detectNotFoundError 1
    Zend\Mvc\View\Http\RouteNotFoundStrategy::prepareNotFoundViewModel 1
    Zend\Mvc\View\Http\ExceptionStrategy::prepareExceptionViewModel 1
    Zend\Mvc\View\Http\InjectViewModelListener::injectViewModel -100
render.error
    Zend\Mvc\View\Http\ExceptionStrategy::prepareExceptionViewModel 1
    Zend\Mvc\View\Http\InjectViewModelListener::injectViewModel -100
    Zend\Mvc\View\Http\DefaultRenderingStrategy::render -10000
render
    Zend\Mvc\View\Http\DefaultRenderingStrategy::render -10000

Originally posted by @maxgu at zendframework/zend-eventmanager#22 (comment)

@froschdesign He's asking about v2, not v3.

@maxgu — The following methods will assist:

  • EventManager::getEvents() returns a list of all events currently registered.
  • EventManager::getListeners($event) returns a list of all CallbackHandlers associated with a given event. Caveat: it only returns those directly attached to the EM instance. You can use $handler->getMetadatum('priority') to get the priority at which the given handler is attached.
  • EventManager::getIdentifiers() will return the list of identifiers the EM instance exposes and will look for shared listeners on.
  • EventManager::getSharedManager() will return the shared event manager instance.
  • SharedEventManager::getEvents() will return a list of all events with shared listeners, across all EM instances; likely, if you're only looking for listeners for a single EM instance, you won't need this.
  • SharedEventManager::getListeners($id, $event) returns a list of CallbackHandlers representing the shared listeners for the given identifier.

Hopefully with the above information, you can stitch something together!


Originally posted by @weierophinney at zendframework/zend-eventmanager#22 (comment)

@snapshotpl thnx, but ZfSnapEventDebugger show ONLY triggered on current URI events, it not show (for examlpe):

dispatch.error
    Zend\Mvc\View\Http\RouteNotFoundStrategy::detectNotFoundError 1
    Zend\Mvc\View\Http\RouteNotFoundStrategy::prepareNotFoundViewModel 1
    Zend\Mvc\View\Http\ExceptionStrategy::prepareExceptionViewModel 1
    Zend\Mvc\View\Http\InjectViewModelListener::injectViewModel -100

Originally posted by @maxgu at zendframework/zend-eventmanager#22 (comment)