Lazy-loads an EventManager instance if none registered.
public getEventManager ( ) : Zend\EventManager\EventManagerInterface | ||
return | Zend\EventManager\EventManagerInterface |
public function __construct($name = null, array $data = array(), $dataName = '') { parent::__construct($name, $data, $dataName); $this->app = \Bootstrap::$app; $this->sm = $this->app->getServiceManager(); $this->evm = $this->app->getEventManager(); }
public function testOnBootstrap() { $this->assertFalse($this->app->getEventManager()->getSharedManager()->getListeners('LearnZF2I18n', MvcEvent::EVENT_DISPATCH)); $e = new MvcEvent(); $e->setApplication($this->app); $this->module->onBootstrap($e); $this->assertCount(1, $this->app->getEventManager()->getSharedManager()->getListeners('LearnZF2I18n', MvcEvent::EVENT_DISPATCH)); }
/** * @return \Zend\Mvc\Application */ public function getApplication() { if ($this->spiffyApplication) { return $this->spiffyApplication; } Console::overrideIsConsole($this->getUseConsoleRequest()); $this->spiffyApplication = SpiffyTest::getInstance()->getApplication(); $events = $this->spiffyApplication->getEventManager(); $events->detach($this->spiffyApplication->getServiceManager()->get('SendResponseListener')); return $this->spiffyApplication; }
public function testUnlocatableControllerLoaderComposedOfAbstractFactory() { $this->setupPathController(); $controllerLoader = $this->serviceManager->get('ControllerLoader'); $controllerLoader->addAbstractFactory('ZendTest\\Mvc\\Controller\\TestAsset\\UnlocatableControllerLoaderAbstractFactory'); $log = array(); $this->application->getEventManager()->attach(MvcEvent::EVENT_DISPATCH_ERROR, function ($e) use(&$log) { $log['error'] = $e->getError(); }); $this->application->run(); $event = $this->application->getMvcEvent(); $dispatchListener = $this->serviceManager->get('DispatchListener'); $return = $dispatchListener->onDispatch($event); $this->assertArrayHasKey('error', $log); $this->assertSame('error-controller-not-found', $log['error']); }
/** * @dataProvider eventPropagation */ public function testEventPropagationStatusIsClearedBetweenEventsDuringRun($events) { $event = new MvcEvent(); $event->setTarget($this->application); $event->setApplication($this->application)->setRequest($this->application->getRequest())->setResponse($this->application->getResponse())->setRouter($this->serviceManager->get('Router')); $event->stopPropagation(true); // Intentionally not calling bootstrap; setting mvc event $r = new ReflectionObject($this->application); $eventProp = $r->getProperty('event'); $eventProp->setAccessible(true); $eventProp->setValue($this->application, $event); // Setup listeners that stop propagation, but do nothing else $marker = array(); foreach ($events as $event) { $marker[$event] = true; } $marker = (object) $marker; $listener = function ($e) use($marker) { $marker->{$e->getName()} = $e->propagationIsStopped(); $e->stopPropagation(true); }; $this->application->getEventManager()->attach($events, $listener); $this->application->run(); foreach ($events as $event) { $this->assertFalse($marker->{$event}, sprintf('Assertion failed for event "%s"', $event)); } }
public function it_aborts_bootstrap_on_console(MvcEvent $event, Application $application, ServiceLocatorInterface $serviceLocator, AccessListener $listener, EventManager $eventManager) { Console::overrideIsConsole(true); $application->getEventManager()->willReturn($eventManager); $serviceLocator->get(AccessListener::class)->willReturn($listener); $application->getServiceManager()->willReturn($serviceLocator); $event->getApplication()->willReturn($application); $listener->attach($eventManager)->shouldNotBeCalled(); $this->onBootstrap($event); }
/** * Get the application object * @return \Zend\Mvc\Application */ public static function getApplication() { if (self::$application instanceof \Zend\Mvc\ApplicationInterface) { return self::$application; } self::$application = \Zend\Mvc\Application::init(self::getApplicationConfig()); $oEventManager = self::$application->getEventManager(); $oEventManager->detach(self::$application->getServiceManager()->get('SendResponseListener')); return self::$application; }
public function setUp() { parent::setUp(); $this->application = \Zend\Mvc\Application::init(include TEST_APP_ROOT . '/config/application.config.php'); $this->em = $this->application->getServiceManager()->get('doctrine.entitymanager.orm_default'); $this->request = null; $this->response = null; $this->routeMatch = null; $this->viewModel = null; $this->event = $this->application->getMvcEvent(); $this->eventManager = $this->application->getEventManager(); $this->request = $this->event->getRequest(); if (!empty($this->entities)) { $tool = new \Doctrine\ORM\Tools\SchemaTool($this->em); foreach ($this->entities as $entity) { $this->doctrineEntities[] = $this->em->getClassMetadata($entity); } $tool->dropSchema($this->doctrineEntities); $tool->createSchema($this->doctrineEntities); $this->em->getConfiguration()->getResultCacheImpl()->flushAll(); $this->em->getConfiguration()->getResultCacheImpl()->deleteAll(); } }
/** * When application is not available, one will be initialized to respond to * the esi request. * * @param Uri $uri * @return \Zend\Mvc\Application */ public function getApplication(Uri $uri) { if (!$this->application instanceof Application) { $applicationConfig = $this->getEsiApplicationConfigProvider()->getEsiApplicationConfig($uri); $this->application = Application::init($applicationConfig); // Remove the finish listeners so response header and content is not automatically echo'd $this->application->getEventManager()->clearListeners(MvcEvent::EVENT_FINISH); } // The request needs to be augmented with the URI passed in $request = $this->application->getRequest(); $request->setUri($uri); $request->setRequestUri($uri->getPath() . '?' . $uri->getQuery()); $request->setQuery(new Parameters($uri->getQueryAsArray())); return $this->application; }
/** * Set the Zend\Mvc\Application and attach the event listeners. * * @param \Zend\Mvc\Application $application * @return ZendHttpKernel */ public function setApplication(Application $application) { if ($this->application === $application) { // Application unchanged. return $this; } // Unregister from previous application. if ($this->application !== null) { // Detach from old application. $this->detach($this->application->getEventManager()); } // Set the application. $this->application = $application; $this->attach($application->getEventManager()); return $this; }
/** * @group 2981 */ public function testReturnsResponseFromListenerWhenDispatchEventShortCircuits() { $this->application->bootstrap(); $testResponse = new Response(); $response = $this->application->getResponse(); $events = $this->application->getEventManager(); $events->clearListeners(MvcEvent::EVENT_ROUTE); $events->attach(MvcEvent::EVENT_DISPATCH, function ($e) use($testResponse) { $testResponse->setContent('triggered'); return $testResponse; }, 100); $self = $this; $triggered = false; $events->attach(MvcEvent::EVENT_FINISH, function ($e) use($self, $testResponse, &$triggered) { $self->assertSame($testResponse, $e->getResponse()); $triggered = true; }); $this->application->run(); $this->assertTrue($triggered); }
/** * Marshall a bad controller exception event * * @param string $controllerName * @param MvcEvent $event * @param Application $application * @param \Exception $exception * @return mixed */ protected function marshallBadControllerEvent($controllerName, MvcEvent $event, Application $application, \Exception $exception) { $event->setError($application::ERROR_EXCEPTION)->setController($controllerName)->setParam('exception', $exception); $events = $application->getEventManager(); $results = $events->trigger(MvcEvent::EVENT_DISPATCH_ERROR, $event); $return = $results->last(); if (!$return) { $return = $event->getResult(); } return $return; }
/** * Marshal a middleware not callable exception event * * @param string $type * @param string $middlewareName * @param MvcEvent $event * @param Application $application * @param \Exception $exception * @return mixed */ protected function marshalMiddlewareNotCallable($type, $middlewareName, MvcEvent $event, Application $application, \Exception $exception = null) { $event->setName(MvcEvent::EVENT_DISPATCH_ERROR); $event->setError($type); $event->setController($middlewareName); $event->setControllerClass('Middleware not callable: ' . $middlewareName); if ($exception !== null) { $event->setParam('exception', $exception); } $events = $application->getEventManager(); $results = $events->triggerEvent($event); $return = $results->last(); if (!$return) { $return = $event->getResult(); } return $return; }
/** * Valid route but with no login with no login */ public function it_dispatches_unauthorized(MvcEvent $event, RouteMatch $match, EventManagerInterface $eventManager, Application $application, $accessService) { $application->getEventManager()->willReturn($eventManager); $accessService->requiresAuthentication(self::CONTROLLER_ADMIN, 'index')->willReturn(true); $accessService->hasUser()->willReturn(false); $event->getTarget()->willReturn($application); $match->getParam('controller')->willReturn(self::CONTROLLER_ADMIN); $match->getParam('action')->willReturn('index'); $match->getMatchedRouteName()->willReturn('admin'); $event->getRouteMatch()->willReturn($match); $accessService->getRoles()->willReturn([]); $eventManager->triggerEvent($event)->shouldBeCalled(); $event->setError(AccessService::ACCESS_UNAUTHORIZED)->shouldBeCalled(); $event->setParam('route', 'admin')->shouldBeCalled(); $event->setParam('controller', self::CONTROLLER_ADMIN)->shouldBeCalled(); $event->setParam('action', 'index')->shouldBeCalled(); $event->setParam('roles', 'none')->shouldBeCalled(); $event->setName(MvcEvent::EVENT_DISPATCH_ERROR)->shouldBeCalled(); $this->verifyAccess($event); }