Bluz\Controller\Reflection::process PHP Method

process() public method

Process to get reflection from file
public process ( ) : void
return void
    public function process()
    {
        /** @var \Closure|object $closure */
        $closure = (include $this->file);
        if (!is_callable($closure)) {
            throw new ComponentException("There is no callable structure in file `{$this->file}`");
        }
        $reflection = new \ReflectionFunction($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();
                }
            }
        }
    }

Usage Example

Example #1
0
 /**
  * Retrieve reflection for anonymous function
  * @param string $file
  * @throws ApplicationException
  * @return Reflection
  */
 public function reflection($file)
 {
     // cache for reflection data
     if (!($reflection = Cache::get('reflection:' . $file))) {
         $reflection = new Reflection($file);
         $reflection->process();
         Cache::set('reflection:' . $file, $reflection);
         Cache::addTag('reflection:' . $file, 'reflection');
     }
     return $reflection;
 }
All Usage Examples Of Bluz\Controller\Reflection::process