Ejemplo n.º 1
0
 /**
  * @param $templateName string|array
  * @param $pageType string
  * @return bool
  * @throws \Exception
  */
 private function dispatchTemplate($templateName, $pageType)
 {
     if (is_array($templateName)) {
         foreach ($templateName as $name) {
             if ($this->dispatchTemplate($name, $pageType)) {
                 return true;
             }
         }
     } else {
         Log::debug("Looking for template '{$templateName}' for page type '{$pageType}'.", ['templateName' => $templateName, 'pageType' => $pageType]);
         $templateName = strtolower($templateName);
         // normalize the name, eg front_page becomes front-page
         $name = preg_replace('|[^a-z0-9_]+|', '-', $templateName);
         // camel case the the controller class name, eg front_page becomes FrontPage
         $nameparts = explode('-', $name);
         array_walk($nameparts, function (&$value, $index) {
             $value = ucfirst($value);
         });
         $classname = implode('', $nameparts);
         if (is_numeric($classname)) {
             $classname = 'Error' . $classname;
         }
         // Interpolate the class name
         $class = $this->context->namespace . '\\Controllers\\' . $classname . 'Controller';
         Log::debug("Looking for class.", ['class' => $class]);
         // Determine the action and controller method
         $action = $this->context->request->query->has('_action') ? ucfirst($this->context->request->query->get('_action')) : 'Index';
         $method = strtolower($this->context->request->getMethod()) . $action;
         $controller = null;
         // If the controller exists, create it ...
         if (class_exists($class)) {
             $controller = new $class($this->context);
         } else {
             // Otherwise, we check to see if the template exists.
             if ($this->context->ui->viewExists('templates/' . $name)) {
                 if ($pageType == 'none') {
                     Log::debug("Found template.", ['templateName' => $templateName]);
                     $this->context->cacheControl->sendHTTPHeaders();
                     // Template exists but page type doesn't map to a built-in
                     // controller, so we just render the template as is.
                     echo $this->context->ui->render('templates/' . $name, [$this->context]);
                     return true;
                 }
                 $controller = $this->context->createController($pageType, $name);
             } else {
                 Log::debug("Trying to mapping controller for class.", ['name' => $name]);
                 $controller = $this->context->mapController($name);
             }
         }
         // if we found a controller, then invoke the method and return it's output
         if ($controller) {
             Log::debug("Found controller.", ['templateName' => $templateName]);
             if (method_exists($controller, $method)) {
                 $response = call_user_func([$controller, $method], $this->context->request);
             } else {
                 // Try GET if method was something other ...
                 $method = 'get' . $action;
                 if (method_exists($controller, $method)) {
                     $response = call_user_func([$controller, $method], $this->context->request);
                 } else {
                     throw new \Exception("Missing method '{$method}' on class '{$class}'.");
                 }
             }
             if (is_object($response) && $response instanceof Response) {
                 $this->context->cacheControl->addResponseHeaders($response);
                 $response->send();
             } else {
                 if (is_string($response)) {
                     $this->context->cacheControl->sendHTTPHeaders();
                     echo $response;
                 }
             }
             return true;
         } else {
             Log::debug("Controller not found.", ['templateName' => $templateName]);
         }
         return false;
     }
     return false;
 }