Exemplo n.º 1
0
 public function testStopPropagation()
 {
     $eventManager = new EventManager();
     $event = new Event('event1');
     $event->setParam('count', 0);
     $eventManager->addListener('event1', function ($e) {
         $count = $e->getParam('count', 0);
         $e->setParam('count', $count + 1);
     }, 2);
     $eventManager->addListener('event1', function ($e) {
         $count = $e->getParam('count', 0);
         $e->setParam('count', $count + 1);
     }, 2);
     $eventManager->addListener('event1', function ($e) {
         $count = $e->getParam('count', 0);
         $e->setParam('count', $count + 1);
     }, 1);
     $eventManager->addListener('event1', function ($e) {
         $count = $e->getParam('count', 0);
         $e->setParam('count', $count + 1);
         $e->stopPropagation();
     });
     $eventManager->trigger($event);
     $this->assertEquals($event->getParam('count'), 2);
 }
Exemplo n.º 2
0
 public function testEvent()
 {
     $event = new Event('Test');
     $this->assertEquals($event->getName(), 'Test');
     $this->assertEquals($event->getParams(), array());
     $this->assertNull($event->getParam('some'));
     $this->assertFalse($event->getParam('some', false));
     $this->assertFalse($event->isStoppedPropagation());
     $event = new Event('Test2', array('foo' => 'bar', 'a' => 100));
     $this->assertEquals($event->getName(), 'Test2');
     $event->setName('NewTest2');
     $this->assertEquals($event->getName(), 'NewTest2');
     $this->assertEquals($event->getParams(), array('foo' => 'bar', 'a' => 100));
     $this->assertEquals($event->getParam('foo'), 'bar');
     $event->setParam('foo', 'baz');
     $this->assertEquals($event->getParam('foo'), 'baz');
     $event->stopPropagation();
     $this->assertTrue($event->isStoppedPropagation());
 }
Exemplo n.º 3
0
 /**
  * Dispatch
  * 
  * @param ServerRequestInterface $request
  * @param ResponseInterface $response
  * @param callable $next
  * @return ResponseInterface
  * @throws Exception\InvalidRouterResultException
  * @throws Exception\InvalidRouterException
  * @throws Exception\ControllerNotFoundException
  * @throws Exception\InvalidControllerException
  */
 protected function dispatch(ServerRequestInterface $request, ResponseInterface $response, callable $next)
 {
     $em = $this->serviceProvider->getEventManager();
     $event = new Event('', array('application' => $this));
     $em->trigger($event->setName(self::EVENT_PRE_DISPATCH));
     $request = $this->serviceProvider->getRequest();
     $result = $request->getAttribute('router_result');
     if ($result instanceof RouterResult === false) {
         throw new Exception\InvalidRouterResultException("Router result not isset in Request");
     }
     $handler = $result->getHandler();
     $handler['controller'] = isset($handler['controller']) ? $handler['controller'] : 'index';
     $handler['action'] = isset($handler['action']) ? $handler['action'] : 'index';
     if (!is_string($handler['controller'])) {
         throw new Exception\InvalidRouterException('Invalid controller name in route handler');
     }
     if (!is_string($handler['action'])) {
         throw new Exception\InvalidRouterException('Invalid action name in route handler');
     }
     $controllerName = $handler['controller'];
     $controllers = $this->getConfig('controllers');
     $controllerClass = isset($controllers[$controllerName]) ? $controllers[$controllerName] : null;
     if (!class_exists($controllerClass)) {
         throw new Exception\ControllerNotFoundException(sprintf('Controller "%s" not exist.', $controllerName));
     }
     $r = new \ReflectionClass($controllerClass);
     if (!$r->isSubclassOf('Tlumx\\Controller')) {
         throw new Exception\InvalidControllerException(sprintf('Controller "%s" is not an instance of Tlumx\\Controller.', $controllerClass));
     }
     $request = $request->withAttribute('router_result_handler', $handler);
     $this->serviceProvider->setRequest($request);
     $controller = new $controllerClass($this->serviceProvider);
     $controller->run();
     $request = $this->serviceProvider->getRequest();
     $response = $this->serviceProvider->getResponse();
     $em->trigger($event->setName(self::EVENT_POST_DISPATCH));
     $request = $this->serviceProvider->getRequest();
     return $next($request, $response);
 }