/** * Finalize the request and send the response. * * @param int $code * @param array $headers * @throws \Pop\Mvc\Exception * @return void */ public function send($code = 200, array $headers = null) { if (null === $this->view) { throw new Exception('The view object is not defined.'); } if (!$this->view instanceof View) { throw new Exception('The view object is not an instance of Pop\\Mvc\\View.'); } if (null !== $this->project->logger()) { $this->project->log("Response [" . $code . "]", time()); } $this->response->setCode($code); if (null !== $headers) { foreach ($headers as $name => $value) { $this->response->setHeader($name, $value); } } // Trigger any dispatch events, then send the response if (null !== $this->project->getEventManager()->get('dispatch')) { $this->project->log('[Event] Dispatch', time(), \Pop\Log\Logger::NOTICE); } $this->project->getEventManager()->trigger('dispatch', array('controller' => $this)); $this->response->setBody($this->view->render(true)); if (null !== $this->project->getEventManager()->get('dispatch.send')) { $this->project->log('[Event] Dispatch Send', time(), \Pop\Log\Logger::NOTICE); } $this->project->getEventManager()->trigger('dispatch.send', array('controller' => $this)); $this->response->send(); }
/** * Route to the correct controller * * @param \Pop\Project\Project $project * @return void */ public function route(\Pop\Project\Project $project = null) { if (null !== $project) { $this->project = $project; } // If the request isn't root '/', traverse the URI path if ($this->request->getPath(0) != '') { $this->controllerClass = $this->traverseControllers($this->controllers); // Else, use root '/' } else { $this->controllerClass = isset($this->controllers['/']) ? $this->controllers['/'] : null; } // If found, create the controller object if (null !== $this->controllerClass && class_exists($this->controllerClass)) { // Push the real base path and URI into the request object $realBasePath = $this->request->getBasePath() . $this->basePath; $realUri = substr($this->request->getFullUri(), strlen($this->request->getBasePath() . $this->basePath)); // Create the controller object $this->controller = new $this->controllerClass($this->request->setRequestUri($realUri, $realBasePath), null, $this->project); // Trigger any route events if (null !== $this->project) { if (null !== $this->project->getEventManager()->get('route')) { $this->project->log('[Event] Route', time(), \Pop\Log\Logger::NOTICE); } $this->project->getEventManager()->trigger('route', array('router' => $this)); } // Else, trigger any route error events } else { if (null !== $this->project) { if (null !== $this->project->getEventManager()->get('route.error')) { $this->project->log('[Event] Route Error', time(), \Pop\Log\Logger::NOTICE); } $this->project->getEventManager()->trigger('route.error', array('router' => $this)); } } }
public function testEventManager() { $func = function () { return 'Hello World'; }; $p = new Project(); $p->attachEvent('route.pre', $func, 2); $this->assertEquals(1, count($p->getEventManager()->get('route.pre'))); $p->detachEvent('route.pre', $func); $this->assertEquals(0, count($p->getEventManager()->get('route.pre'))); }