/** * Init * This method must return self * @return self */ public function init() { parent::init(); $reflector = new \ReflectionClass($this); $this->_applicationNamespace = $reflector->getNamespaceName(); //Define BASE Asset paths $assetConfig = $this->config()->get("assets"); $assetPath = $assetConfig->get("assets", "assets"); if (!defined("BASE_ASSETS")) { define("BASE_ASSETS", Router::buildPath(SITE_URL, $assetPath)); } if (!defined("BASE_IMAGES")) { define("BASE_IMAGES", Router::buildPath(SITE_URL, $assetPath, $assetConfig->get("images", "images"))); } if (!defined("BASE_STYLES")) { define("BASE_STYLES", Router::buildPath(SITE_URL, $assetPath, $assetConfig->get("css", "css"))); } if (!defined("BASE_SCRIPTS")) { define("BASE_SCRIPTS", Router::buildPath(SITE_URL, $assetPath, $assetConfig->get("js", "js"))); } if (!defined("BASE_TEMPLATES")) { define("BASE_TEMPLATES", Filesystem::buildPath(PROJECT_PATH, $assetConfig->get("templates", "Templates"))); } return $this; }
/** * Render With * @param string $templateFile - Template file location * @return string - Parsed template contents */ public function renderWith($templateFile) { if (is_array($templateFile)) { $templateFile = call_user_func_array("Touchbase\\Filesystem\\Filesystem::buildPath", $templateFile); } foreach ($this->controller->templateSearchPaths() as $path) { $templateFileObj = File::create([$path, $templateFile]); if ($templateFileObj->exists()) { $templateFilePath = $templateFileObj->path; break; } } if (!isset($templateFilePath)) { return false; } $this->assign("controller", $this->controller); $this->assign("request", $this->controller->request()); $this->assign("errors", $this->controller->errors); $this->assign("messages", $this->controller->messages); //TODO: Sort this out! if (isset($this->controller) && $this->controller instanceof \Touchbase\Control\WebpageController) { $this->assign("assets", $this->controller->response()->assets()); } if ($template = $this->readTemplate($templateFilePath)) { /** * Auto CSS / JS include */ if (isset($this->controller) && $this->controller instanceof \Touchbase\Control\WebpageController) { //Include Template Styles $fileParts = pathinfo($templateFilePath); $templateName = strtolower(basename($fileParts['filename'], ".tpl")); //Make sure to only add styles for our phtml templates if ($fileParts['extension'] === "php") { $this->controller->response()->assets()->includeStyle($templateName . ".css"); $this->controller->response()->assets()->includeScript($templateName . ".js"); //Include Controller Sytles $controllerName = strtolower($this->controller->controllerName); $this->controller->response()->assets()->includeStyle($controllerName . ".css"); $this->controller->response()->assets()->includeScript($controllerName . ".js"); } } return $template; } return false; }
/** * Handle Request * @param HTTPRequest &$request * @param HTTPResponse &$response * @return string */ public function handleRequest(HTTPRequest &$request, HTTPResponse &$response) { //Pass through to Controller try { $response = new WebpageResponse($this); if ($request->isPost()) { $validation = Validation::create(); $this->validatePostRequest($request, $response, $validation); } $body = parent::handleRequest($request, $response); } catch (\Exception $e) { if (!$e instanceof HTTPResponseException) { error_log(print_r($e, true)); $e = new HTTPResponseException($e->getMessage(), 500); $e->response()->setHeader('Content-Type', 'text/plain'); } $body = $this->handleException($e); //If handleException returns a redirect. Follow it. if ($body instanceof \Touchbase\Control\HTTPResponse) { //TODO: Should we always return the response? if ($body->hasFinished()) { return $response = $body; } } $response->setBody($body); $response->setStatusCode($e->getCode(), $e->getMessage()); } if ($this->request()->isMainRequest()) { $applicationTitles = []; $application = $this->application; while ($application != null) { if (isset($application::$name)) { $applicationTitles[] = $application::$name; } $application = $application->application; } $this->response()->assets()->pushTitle(array_reverse($applicationTitles)); if (isset(static::$name)) { $this->response()->assets()->pushTitle(static::$name); } } return $body; }
/** * Asset Search Paths * @param string $type - CSS | JS | IMAGES * @return Generator<string> - A file path to search for self */ private function assetSearchPaths($type) { //Search order // - Application/self/{type}/Theme/ // - Application/self/{type} // - Base/self/{type}/Theme // - Base/self/{type} $assetConfig = $this->config("assets"); $assetsPath = $assetConfig->get("assets", "Assets"); $typePath = $assetConfig->get($type, $type . DIRECTORY_SEPARATOR); $searchPaths[] = null; //This allows the use of an absolute path to be used when merging paths. if ($this->controller) { $searchPaths[] = Filesystem::buildPath($this->controller->applicationPath, $assetsPath, $typePath, $this->controller->theme()); $searchPaths[] = Filesystem::buildPath($this->controller->applicationPath, $assetsPath, $typePath); } $searchPaths[] = Filesystem::buildPath(PROJECT_PATH, $assetsPath, $typePath, $this->controller->theme()); $searchPaths[] = Filesystem::buildPath(PROJECT_PATH, $assetsPath, $typePath); foreach ($searchPaths as $searchPath) { (yield $searchPath ? static::urlForPath($searchPath) : $searchPath); } }