/** * @inheritdoc */ public function prepare(\Parable\Http\Response $response) { $content = $response->getContent(); if (!json_decode($content)) { $content = json_encode($content); } $response->setContent($content); }
/** * @param \Parable\Routing\Route $route * * @return $this */ public function dispatch(\Parable\Routing\Route $route) { $this->hook->trigger('parable_dispatch_before', $route); $controller = null; /* Start output buffering and set $content to null */ $content = null; $this->response->startOutputBuffer(); /* Call the relevant code */ if ($route->controller && $route->action) { $controller = \Parable\DI\Container::get($route->controller); $content = $controller->{$route->action}($route); } elseif ($route->callable) { $call = $route->callable; $content = $call($route); } /* Try to get the relevant view */ $templateFile = null; if ($route->template) { $templateFile = $this->path->getDir($route->template); } else { if ($controller) { $reflection = new \ReflectionClass($controller); $templateFile = $this->path->getDir('app/View/' . $reflection->getShortName() . '/' . $route->action . '.phtml'); } } if ($templateFile && file_exists($templateFile)) { $this->view->setTemplatePath($templateFile); $this->view->render(); } /* Get the output buffer content and check if $content holds anything. If so, append it to the $bufferContent */ $bufferContent = $this->response->returnOutputBuffer(); if ($content) { $bufferContent .= $content; } /* And append the content to the response object */ $this->response->appendContent($bufferContent); $this->hook->trigger('parable_dispatch_after', $route); return $this; }
/** * Do all the setup * * @return $this */ public function run() { /* Set the basedir on paths */ $this->path->setBasedir(BASEDIR); /* Load all known Config files now that we know the baseDir */ $this->hook->trigger('parable_config_load_before'); $this->config->load(); $this->hook->trigger('parable_config_load_after', $this->config); /* Start the session if session.autoEnable is true */ if ($this->config->get('session.autoEnable') !== false) { $this->hook->trigger('parable_session_start_before'); $this->values->session->start(); $this->hook->trigger('parable_session_start_after', $this->values->session); } /* Build the base Url */ $this->url->buildBaseurl(); /* Load the routes */ $this->loadRoutes(); /* Get the current url */ $currentUrl = $this->url->getCurrentUrl(); /* Load the config */ if ($this->config->get('database.type')) { $this->database->setConfig($this->config->get('database')); } /* See if there's an init directory defined in the config */ if ($this->config->get('initLocations')) { $this->loadInits(); } /* And try to match the route */ $this->hook->trigger('parable_route_match_before', $currentUrl); $route = $this->router->matchCurrentRoute(); $this->hook->trigger('parable_route_match_after', $route); if ($route) { $this->hook->trigger('parable_http_200', $route); $this->dispatcher->dispatch($route); } else { $this->response->setHttpCode(404); $this->hook->trigger('parable_http_404', $currentUrl); } $this->hook->trigger('parable_response_send'); $this->response->send(); return $this; }
/** * @param string $templatePath * * @return string */ public function partial($templatePath) { $this->response->startOutputBuffer(); $this->loadTemplatePath($templatePath); return $this->response->returnOutputBuffer(); }
/** * @inheritdoc */ public function init(\Parable\Http\Response $response) { $response->setContentType($this->contentType); }