Esempio n. 1
0
 /**
  * Process to get reflection from file
  *
  * @return void
  * @throws ComponentException
  */
 public function process()
 {
     // workaround for get reflection of closure function
     $bootstrap = $view = $module = $controller = null;
     /** @var \Closure|object $closure */
     $closure = (include $this->file);
     if (!is_callable($closure)) {
         throw new ComponentException("There is no callable structure in file `{$this->file}`");
     }
     if ($closure instanceof \Closure) {
         $reflection = new \ReflectionFunction($closure);
     } else {
         $reflection = new \ReflectionObject($closure);
     }
     // check and normalize params by doc comment
     $docComment = $reflection->getDocComment();
     // get all options by one regular expression
     if (preg_match_all('/\\s*\\*\\s*\\@([a-z0-9-_]+)\\s+(.*).*\\s+/i', $docComment, $matches)) {
         foreach ($matches[1] as $i => $key) {
             $this->setOption($key, $matches[2][$i]);
         }
     }
     // init routes
     $this->initRoute();
     // parameters available for Closure only
     if ($reflection instanceof \ReflectionFunction) {
         // get params and convert it to simple array
         $reflectionParams = $reflection->getParameters();
         // setup params and optional params
         foreach ($reflectionParams as $param) {
             $name = $param->getName();
             // if some function params is missed in description
             if (!isset($this->params[$name])) {
                 $this->params[$name] = null;
             }
             if ($param->isOptional()) {
                 $this->values[$name] = $param->getDefaultValue();
             }
         }
     }
 }