Beispiel #1
0
 public function render($options = [])
 {
     if ($this->viewConfig['rendered']) {
         return;
     }
     $this->viewConfig['rendered'] = true;
     $options = $options + $this->viewConfig + ['view_path' => $this->name];
     $class = App::locate($options['class'], 'view');
     if (!$class) {
         $message = 'Cannot locate view class';
         throw new \Exception($message);
     }
     unset($options['class'], $options['rendered'], $options['render']);
     if (!isset($options['type'])) {
         $options['type'] = key(Action::$contentTypes);
     }
     if ($options['type'] == 'negotiate') {
         $acceptsTypes = explode(',', $this->request->accepts);
         foreach (Action::$contentTypes as $type => $mimeTypes) {
             if (array_intersect($mimeTypes, $acceptsTypes)) {
                 $options['type'] = $type;
                 break;
             }
         }
     }
     $view = new $class($this->request, $this->response, $options);
     $this->response->body($view->renderAll());
 }
Beispiel #2
0
 public static function dispatch()
 {
     global $argv;
     $args = array_slice($argv, 1);
     if (empty($args)) {
         $message = 'No command specified';
         throw new \Exception($message);
     }
     $command = str_replace(' ', '', ucwords(str_replace('_', ' ', array_shift($args))));
     $console = App::locate($command . 'Command', 'command');
     $invoke = new $console(static::input(), static::output(), $command);
     $invoke();
 }
Beispiel #3
0
 public function renderError($self, $params, $chain)
 {
     if (!Config::get('debug')) {
         return;
     }
     extract($params);
     $view = App::locate('View', 'view');
     $request = end(Action::$requests) ?: Action::request();
     $viewObj = new $view($request);
     $data = compact('error');
     $view_path = 'Error';
     $type = $request->type ?: key(Action::$contentTypes);
     $options = compact('data', 'view_path', 'type');
     echo $viewObj->render('view', 'debug_error', $options);
     $params['default'] = false;
     return $chain->next($self, $params, $chain);
 }
Beispiel #4
0
 public static function call($request)
 {
     $response = static::response();
     $params = compact('request', 'response');
     return static::filterStaticMethod(__FUNCTION__, $params, function ($self, $params) {
         extract($params);
         if (isset($request->params['call'])) {
             $invoke = $request->params['call'];
             if (!is_callable($invoke)) {
                 $message = 'Cannot invoke non-callable';
                 throw new ActionException($message);
             }
             return $invoke($request, $response);
         } else {
             $controller = App::locate($request->params['controller'], 'controller');
             if (!$controller) {
                 $message = 'Cannot locate controller';
                 throw new NotFoundException($message);
             }
             $invoke = new $controller($request, $response);
             if (!is_callable([$invoke, $request->params['action']])) {
                 $message = 'Cannot invoke non-callable controller action';
                 throw new ActionException($message);
             }
             return $invoke();
         }
     });
 }