public function run()
{
// If router exists, then route the project to the appropriate controller
if (null !== $this->router) {
$this->start = time();
if (isset($_SERVER['REQUEST_METHOD'])) {
$session = '[' . $_SERVER['REQUEST_METHOD'] . ']';
if (isset($_SERVER['REMOTE_ADDR'])) {
$session .= ' ' . $_SERVER['REMOTE_ADDR'];
if (isset($_SERVER['SERVER_PORT'])) {
$session .= ':' . $_SERVER['SERVER_PORT'];
}
if (isset($_SERVER['HTTP_USER_AGENT'])) {
$session .= ' ' . $_SERVER['HTTP_USER_AGENT'];
}
}
} else {
$session = '[CLI]';
}
$this->log($session, time());
if (null !== $this->events->get('route.pre')) {
$this->log('[Event] Pre-Route', time(), \Pop\Log\Logger::NOTICE);
}
// Trigger any pre-route events, route, then trigger any post-route events
$this->events->trigger('route.pre', array('router' => $this->router));
// If still alive after 'route.pre'
if ($this->events->alive()) {
$this->log('Route Start', time());
$this->router->route($this);
// If still alive after 'route'
if ($this->events->alive()) {
if (null !== $this->events->get('route.post')) {
$this->log('[Event] Post-Route', time(), \Pop\Log\Logger::NOTICE);
}
$this->events->trigger('route.post', array('router' => $this->router));
// If still alive after 'route.post' and if a controller was properly
// routed and created, then dispatch it
if ($this->events->alive() && null !== $this->router->controller()) {
// Trigger any pre-dispatch events
if (null !== $this->events->get('dispatch.pre')) {
$this->log('[Event] Pre-Dispatch', time(), \Pop\Log\Logger::NOTICE);
}
$this->events->trigger('dispatch.pre', array('router' => $this->router));
// If still alive after 'dispatch.pre'
if ($this->events->alive()) {
// Get the action and dispatch it
$action = $this->router->getAction();
// Dispatch the found action, the error action or trigger the dispatch error events
if (null !== $action && method_exists($this->router->controller(), $action)) {
$this->router->controller()->dispatch($action);
} else {
if (method_exists($this->router->controller(), $this->router->controller()->getErrorAction())) {
$this->router->controller()->dispatch($this->router->controller()->getErrorAction());
} else {
if (null !== $this->events->get('dispatch.error')) {
$this->log('[Event] Dispatch Error', time(), \Pop\Log\Logger::ERR);
}
$this->events->trigger('dispatch.error', array('router' => $this->router));
}
}
// If still alive after 'dispatch'
if ($this->events->alive()) {
// Trigger any post-dispatch events
if (null !== $this->events->get('dispatch.post')) {
$this->log('[Event] Post-Dispatch', time(), \Pop\Log\Logger::NOTICE);
}
$this->events->trigger('dispatch.post', array('router' => $this->router));
}
}
}
}
}
$this->log('Route End', time());
}
}