Esempio n. 1
0
 public function render(ViewContext $viewContext)
 {
     $request = $viewContext->getHttpContext()->getRequest();
     $response = $viewContext->getHttpContext()->getResponse();
     $routeData = $viewContext->getRouteData();
     $viewFile = String::set($this->viewFilePattern)->prepend(Environment::getControllerPath())->replace('@module', String::set($routeData->module)->toLower()->toUpperFirst())->replace('@controller', String::set($routeData->controller)->toLower()->toUpperFirst())->replace('@action', String::set($routeData->action)->toLower()->toUpperFirst())->append('.php')->replace('\\', '/');
     if (file_exists($viewFile)) {
         extract($viewContext->getViewBag()->toArray());
         ob_start();
         require_once $viewFile;
         $this->output['view'] = ob_get_clean();
         if ($this->layoutFile) {
             if (substr($this->layoutFile, 0, 1) == '~') {
                 $this->layoutFile = \System\Std\Environment::getControllerPath() . substr($this->layoutFile, 1);
             }
             if (file_exists($this->layoutFile)) {
                 ob_start();
                 require_once $this->layoutFile;
                 $this->output['layoutFile'] = ob_get_clean();
             } else {
                 throw new ViewNotFoundException(sprintf("The layout file '%s' was not found", $this->layoutFile));
             }
         }
         if (isset($this->output['layoutFile'])) {
             return $this->output['layoutFile'];
         }
         return $this->output['view'];
     } else {
         throw new ViewNotFoundException(sprintf("The view '%s' was not found.", $viewFile));
     }
 }
Esempio n. 2
0
 /**
  * Dispatches a controller. This method is declared final and cannot be overriden.
  * 
  * @method  run
  * @return  void
  */
 public final function run()
 {
     if ($this->routes->count() == 0) {
         throw new \RuntimeException('One or more routes must be registered.');
     }
     $controllerDispacthed = false;
     foreach ($this->routes as $route) {
         $route->setHttpRequest($this->httpContext->getRequest());
         $routeData = $route->execute();
         if ($routeData) {
             $namespace = '';
             if (Environment::getRootPath() != Environment::getControllerPath()) {
                 $namespace = String::set(Environment::getControllerPath())->replace(Environment::getRootPath(), '')->trim('/');
             }
             $moduleName = $routeData->get('module') ? $routeData->get('module') . '.' : '';
             $class = String::set(sprintf('%s.%sControllers.%sController', $namespace, ucfirst(strtolower($moduleName)), ucfirst(strtolower($routeData->get('controller')))));
             try {
                 $controller = Object::getInstance($class);
                 $controller->getRegistry()->merge(get_object_vars($this));
             } catch (\ReflectionException $e) {
                 throw new Mvc\ControllerNotFoundException(sprintf("The controller '%s' does not exist.", $class));
             }
             if (!$controller instanceof Mvc\Controller) {
                 throw new Mvc\MvcException(sprintf("The controller '%s' does not inherit from System\\Web\\Mvc\\Controller.", $class));
             }
             $moduleClassName = String::set(sprintf('%s.%sControllers.%s', $namespace, ucfirst($moduleName), 'Module'));
             $this->authenticateRequest($controller);
             $this->preAction($controller);
             $moduleInstance = Object::getInstance($moduleClassName, null, false);
             if ($moduleInstance) {
                 if (method_exists($moduleInstance, 'load')) {
                     $moduleInstance->load($controller);
                 }
             }
             $controller->execute($this->httpContext);
             if ($moduleInstance) {
                 if (method_exists($moduleInstance, 'unload')) {
                     $moduleInstance->unload($controller);
                 }
             }
             $this->postAction($controller);
             $controllerDispacthed = true;
             break;
         }
     }
     if (false === $controllerDispacthed) {
         throw new Mvc\MvcException("Unable to dispatch a controller. None of the registered routes matched the request URI.");
     }
 }