コード例 #1
0
ファイル: App.php プロジェクト: p-m-d/naf
 /**
  * @todo needs lots of love
  *
  * @param string $name
  * @param string $type
  * @throws \Exception
  * @return string
  */
 public static function locate($name, $type = null)
 {
     if (strpos($name, '\\') && class_exists($name)) {
         return $name;
     }
     $type = $type ? strtolower($type) : $type;
     $params = compact('name', 'type');
     return static::filterStaticMethod(__FUNCTION__, $params, function ($self, $params) {
         extract($params);
         if (isset($type, $self::$classPaths[$type])) {
             foreach ($self::$classPaths[$type] as $path) {
                 $app = Config::get('app.namespace');
                 $namespaces = array_merge([$app], $self::$namespaces, [__NAMESPACE__]);
                 foreach ($namespaces as $namespace) {
                     $classPath = "{$namespace}\\{$path}\\{$name}";
                     if (class_exists($classPath)) {
                         return $classPath;
                     }
                 }
             }
         }
     });
     $message = sprintf("Class '%s' of type '%s' not found.", $name, $type);
     throw new \Exception($message);
 }
コード例 #2
0
ファイル: View.php プロジェクト: p-m-d/naf-action
 public function __construct($request = null, $response = null, $options = array())
 {
     $this->viewConfig(Config::merge(self::$view, static::$view, $options));
     $this->request = $request ?: Action::request();
     $this->response = $response ?: Action::response();
     if (isset($this->viewConfig['type'])) {
         $this->response->type = $this->viewConfig['type'];
     } else {
         $this->viewConfig['type'] = $this->response->type;
     }
 }
コード例 #3
0
ファイル: ErrorHandler.php プロジェクト: p-m-d/naf-action
 public function renderException($self, $params, $chain)
 {
     if (ob_get_length()) {
         ob_end_clean();
     }
     extract($params);
     $request = end(Action::$requests) ?: Action::request();
     $response = Action::response();
     if (!Config::get('debug')) {
         if ($exception instanceof ActionException) {
             $response->status = $exception->getStatus();
         }
     }
     $errorController = new Controller($request, $response);
     $errorController->overload('error', function ($self) {
         return $self->render();
     });
     $view = ['view' => Config::get('debug') ? 'debug_exception' : 'error', 'view_path' => 'Error'];
     $data = compact('exception');
     echo $errorController('error', $data, $view);
     $params['exit'] = true;
     return $chain->next($self, $params, $chain);
 }
コード例 #4
0
ファイル: Action.php プロジェクト: p-m-d/naf-action
 public static function dispatch()
 {
     $request = static::request();
     static::filterStaticMethod(__FUNCTION__, compact('request'), function ($self, $params) {
         extract($params);
         $filterParams = [];
         foreach ($self::$_dispatchFilters as $pattern => $filter) {
             if (preg_match($pattern, $request->url, $params)) {
                 if (is_callable($filter)) {
                     if ($_filterParams = call_user_func($filter, $request, $filterParams)) {
                         $_filterParams = Config::merge($filterParams, $filter);
                     }
                 } else {
                     $filterParams = Config::merge($filterParams, $filter);
                     array_map(function ($v, $k) use(&$filterParams) {
                         if (is_string($k)) {
                             $filterParams[$k] = $v;
                         }
                     }, $params, array_keys($params));
                 }
             }
         }
         $request->params = Config::merge($request->params, $filterParams);
         if (!($routeParams = $self::match($request, $filterParams))) {
             $message = 'Cannot route request';
             throw new NotFoundException($message);
         }
         $request->params = Config::merge($request->params, $routeParams);
         $self::$requests[] = $request;
     });
     return static::call($request);
 }