Esempio n. 1
0
 /**
  * Request dispatcher.
  *
  * @return mixed Controller/action response
  *
  */
 private function dispatch()
 {
     if ($this->route->isActionClosure()) {
         $controller = 'ClosureContext';
         $action = 'bind';
     } else {
         $controller = $this->route->controller();
         $controller = $controller . Configuration::CONTROLLER_NAME_SUFFIX;
         $action = $this->route->action();
     }
     if (class_exists($controller, true)) {
         // Load reflection of controller/class
         $this->container->reflectionLoad($controller);
         $withConstructor = false;
         $dependencies = array();
         $constructor = $this->container->reflectionGetConstructor();
         // Check if class has a contructor method (either "__construct" or method that is the same name as its class)
         if (isset($constructor->name)) {
             $dependencies = $this->container->reflectionGetMethodDependencies($constructor->name);
             if ($constructor->name == '__construct') {
                 // $skipReflection = true;
                 $withConstructor = true;
             }
         }
         // Create class instance (object)
         $obj = $this->container->reflectionCreateInstance($dependencies, $withConstructor);
         // Prepare/set Closure function
         if ($this->route->isActionClosure()) {
             $obj->set($this->route->getClosure());
         }
         // Check if object method exists
         if (method_exists($obj, $action)) {
             // Check if action method needs dependency
             $dependencies = $this->container->reflectionGetMethodDependencies($action);
             // Call the controller's action method
             call_user_func_array(array($obj, $action), $dependencies);
         } else {
             // No action method found!
             if (!$this->config->application['production']) {
                 $this->http->header($this->http->getValue('SERVER_PROTOCOL') . ' 404 Not Found', true, 404);
                 throw new Exception('Cannot find method "' . $action . '" in controller class "' . $controller . '"');
             } else {
                 $this->view->layout('error')->page('error.404')->render();
             }
         }
     } else {
         // No controller class found!
         if (!$this->config->application['production']) {
             $this->http->header($this->http->getValue('SERVER_PROTOCOL') . ' 404 Not Found', true, 404);
             throw new Exception('Cannot find controller class "' . $controller . '"');
         } else {
             $this->view->layout('error')->page('error.404')->render();
         }
     }
 }
Esempio n. 2
0
 /**
  * Render template/layout
  *     
  * @param array $objVars Variables to be passed to the layout and page template
  *
  */
 public function render($objVars = array())
 {
     // Check if view 'base' (path) configuration is set
     if (!isset($this->config->view['base'])) {
         if (!$this->config->application['production']) {
             throw new RuntimeException('Application view is not configured properly.');
         } else {
             print 'Application view is not configured properly.';
         }
     }
     // Get base absolute path
     $this->viewBase = $this->getAbsolutePath($this->config->view['base']);
     // Store variables (to be passed to the layout and page/view template)
     // Accessible via '__get' method
     if (isset($objVars) && is_array($objVars)) {
         $this->variables = $objVars;
     }
     $layoutName = null;
     // Check if layout is defined
     if (isset($this->useLayout)) {
         $layoutFile = $this->viewBase . DIRECTORY_SEPARATOR . trim($this->config->view['template']['layout'], DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . str_replace($this->config->PHP_FILE_EXTENSION, '', $this->useLayout) . $this->config->PHP_FILE_EXTENSION;
         $layoutName = $this->useLayout;
         // Else, use default layout name
     } else {
         $layoutFile = $this->viewBase . DIRECTORY_SEPARATOR . trim($this->config->view['template']['layout'], DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . str_replace($this->config->PHP_FILE_EXTENSION, '', $this->config->view['layout']) . $this->config->PHP_FILE_EXTENSION;
         $layoutName = $this->config->view['layout'];
     }
     if (!file_exists($layoutFile)) {
         $this->http->header($this->http->getValue('SERVER_PROTOCOL') . ' 500 Internal Server Error', true, 500);
         if (!$this->config->application['production']) {
             throw new RuntimeException('Layout "' . $layoutFile . '" not found or does not exists.');
         } else {
             print 'Layout "' . $layoutName . '" not found or does not exists.';
         }
     } else {
         include $layoutFile;
     }
 }