getMvcEvent() public method

Get the MVC event instance
public getMvcEvent ( ) : MvcEvent
return MvcEvent
 /**
  * @return Response
  */
 public function __invoke()
 {
     $routeMatch = $this->application->getMvcEvent()->getRouteMatch();
     $redirect = $this->getRedirect($routeMatch->getMatchedRouteName(), $this->getRedirectRouteFromRequest());
     $response = $this->application->getResponse();
     $response->getHeaders()->addHeaderLine('Location', $redirect);
     $response->setStatusCode(302);
     return $response;
 }
Example #2
0
 public function __construct(HelperPluginManager $pluginManager)
 {
     $this->pluginManager = $pluginManager;
     $this->serviceManager = $pluginManager->getServiceLocator();
     $this->app = $pluginManager->getServiceLocator()->get('Application');
     $this->request = $this->app->getRequest();
     $this->event = $this->app->getMvcEvent();
     $this->em = $this->serviceManager->get('Doctrine\\ORM\\EntityManager');
     $this->translator = $this->serviceManager->get('translator');
 }
Example #3
0
 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']);
 }
Example #4
0
 public function testCanRecoverFromApplicationError()
 {
     $this->application->bootstrap();
     $response = $this->getMock('Zend\\Stdlib\\ResponseInterface');
     $errorMock = $this->getMock('stdClass', array('__invoke'));
     $finishMock = $this->getMock('stdClass', array('__invoke'));
     $routeMock = $this->getMock('stdClass', array('__invoke'));
     $dispatchMock = $this->getMock('stdClass', array('__invoke'));
     $errorMock->expects($this->once())->method('__invoke')->will($this->returnCallback(function (MvcEvent $event) {
         $event->stopPropagation(true);
         $event->setRouteMatch(new Router\RouteMatch(array()));
         $event->setError('');
     }));
     $routeMock->expects($this->once())->method('__invoke')->will($this->returnCallback(function (MvcEvent $event) {
         $event->stopPropagation(true);
         $event->setError(Application::ERROR_ROUTER_NO_MATCH);
         return $event->getApplication()->getEventManager()->trigger(MvcEvent::EVENT_DISPATCH_ERROR, $event)->last();
     }));
     $dispatchMock->expects($this->once())->method('__invoke')->will($this->returnValue($response));
     $finishMock->expects($this->once())->method('__invoke')->will($this->returnCallback(function (MvcEvent $event) {
         $event->stopPropagation(true);
     }));
     $this->application->getEventManager()->attach(MvcEvent::EVENT_DISPATCH_ERROR, $errorMock, 100);
     $this->application->getEventManager()->attach(MvcEvent::EVENT_ROUTE, $routeMock, 100);
     $this->application->getEventManager()->attach(MvcEvent::EVENT_DISPATCH, $dispatchMock, 100);
     $this->application->getEventManager()->attach(MvcEvent::EVENT_FINISH, $finishMock, 100);
     $this->application->run();
     $this->assertSame($response, $this->application->getMvcEvent()->getResponse());
 }
Example #5
0
 /**
  * Sets the request.
  *
  * @param \Symfony\Component\HttpFoundation\Request $request
  * @return null
  */
 protected function setSymfonyRequest(SymfonyRequest $request)
 {
     $zendRequest = self::createZendRequest($request);
     $serviceManager = $this->application->getServiceManager();
     $serviceManager->setAllowOverride(true);
     $serviceManager->setService('Request', $zendRequest);
     $serviceManager->setAllowOverride(false);
     $this->application->getMvcEvent()->setRequest($zendRequest);
 }
 public function testCompleteRequestShouldReturnApplicationInstance()
 {
     $r = new ReflectionObject($this->application);
     $method = $r->getMethod('completeRequest');
     $method->setAccessible(true);
     $this->application->bootstrap();
     $event = $this->application->getMvcEvent();
     $result = $method->invoke($this->application, $event);
     $this->assertSame($this->application, $result);
 }
Example #7
0
 /**
  * Assert that the application caught an exception
  * 
  * @param string $class
  * @param string $message
  */
 protected function assertApplicationException($class = 'Exception', $message = null)
 {
     $event = $this->application->getMvcEvent();
     $this->assertTrue($event->isError());
     $exception = $event->getParam('exception');
     $this->assertInstanceOf($class, $exception);
     if (null !== $message) {
         $this->assertEquals($message, $exception->getMessage());
     }
 }
Example #8
0
 /**
  * @param $controllerName
  * @param array $controller
  */
 private function createPath($controllerName, array $controller)
 {
     foreach ($controller as $action => $template) {
         try {
             /** @var AbstractActionController $controllerClass */
             $controllerClass = $this->controllerManager->get($controllerName);
             $controllerClass->setEvent($this->application->getMvcEvent());
             $viewModel = $controllerClass->{$action}();
         } catch (\Exception $e) {
             $viewModel = null;
         }
         $this->createPathFromViewModel($viewModel, $controllerName, $action);
     }
 }
Example #9
0
 protected function dispatch($controller, $action, $renderView = FALSE)
 {
     $this->event = $this->application->getMvcEvent();
     $this->request = new Request();
     //		$this->routeMatch = $this->eventManager->trigger(MvcEvent::EVENT_ROUTE, $this->event)->first();
     $this->routeMatch = new RouteMatch(array('controller' => $controller));
     $this->routeMatch->setParam('action', $action);
     $this->event->setRouteMatch($this->routeMatch);
     $this->controller->setEvent($this->event);
     return $this->controller->dispatch($this->request, $this->response);
     //		$this->request->setUri($url);
     //		$this->viewModel = $this->eventManager->trigger(MvcEvent::EVENT_DISPATCH, $this->event)->first();
     //		$responseEvent = ($renderView ? MvcEvent::EVENT_RENDER : MvcEvent::EVENT_FINISH);
     //		$this->response = $this->eventManager->trigger($responseEvent, $this->event)->first();
 }
Example #10
0
 /**
  * Sets up the view integration
  *
  * Pulls the View object and PhpRenderer strategy from the locator, and 
  * attaches the former to the latter. Then attaches the 
  * DefaultRenderingStrategy to the application event manager.
  * 
  * @param  Application $application 
  * @return void
  */
 protected function setupView($application)
 {
     // Basic view strategy
     $locator = $application->getLocator();
     $events = $application->events();
     $staticEvents = StaticEventManager::getInstance();
     $view = $locator->get('Zend\\View\\View');
     $phpRendererStrategy = $locator->get('Zend\\View\\Strategy\\PhpRendererStrategy');
     $defaultViewStrategy = $locator->get('Zend\\Mvc\\View\\DefaultRenderingStrategy');
     $view->events()->attachAggregate($phpRendererStrategy);
     $events->attachAggregate($defaultViewStrategy);
     // Error strategies
     $noRouteStrategy = $locator->get('Zend\\Mvc\\View\\RouteNotFoundStrategy');
     $exceptionStrategy = $locator->get('Zend\\Mvc\\View\\ExceptionStrategy');
     $events->attachAggregate($noRouteStrategy);
     $events->attachAggregate($exceptionStrategy);
     // Template/ViewModel listeners
     $arrayListener = $locator->get('Zend\\Mvc\\View\\CreateViewModelFromArrayListener');
     $injectTemplateListener = $locator->get('Zend\\Mvc\\View\\InjectTemplateListener');
     $injectViewModelListener = $locator->get('Zend\\Mvc\\View\\InjectViewModelListener');
     $staticEvents->attach('Zend\\Stdlib\\Dispatchable', 'dispatch', array($arrayListener, 'createViewModelFromArray'), -80);
     $staticEvents->attach('Zend\\Stdlib\\Dispatchable', 'dispatch', array($injectTemplateListener, 'injectTemplate'), -90);
     $events->attach('dispatch.error', array($injectViewModelListener, 'injectViewModel'), -100);
     $staticEvents->attach('Zend\\Stdlib\\Dispatchable', 'dispatch', array($injectViewModelListener, 'injectViewModel'), -100);
     // Inject MVC Event with view model
     $mvcEvent = $application->getMvcEvent();
     $viewModel = $mvcEvent->getViewModel();
     $viewModel->setTemplate($defaultViewStrategy->getLayoutTemplate());
     // Inject MVC Event view model as root view model
     $renderer = $phpRendererStrategy->getRenderer();
     $modelHelper = $renderer->plugin('view_model');
     $modelHelper->setRoot($viewModel);
 }