Swagger\Context::detect PHP Method

detect() public static method

Create a Context based on the debug_backtrace
public static detect ( integer $index ) : Context
$index integer
return Context
    public static function detect($index = 0)
    {
        $context = new Context();
        $backtrace = debug_backtrace();
        $position = $backtrace[$index];
        if (isset($position['file'])) {
            $context->filename = $position['file'];
        }
        if (isset($position['line'])) {
            $context->line = $position['line'];
        }
        $caller = isset($backtrace[$index + 1]) ? $backtrace[$index + 1] : null;
        if (isset($caller['function'])) {
            $context->method = $caller['function'];
            if (isset($caller['type']) && $caller['type'] === '::') {
                $context->static = true;
            }
        }
        if (isset($caller['class'])) {
            $fqn = explode('\\', $caller['class']);
            $context->class = array_pop($fqn);
            if (count($fqn)) {
                $context->namespace = implode('\\', $fqn);
            }
        }
        // @todo extract namespaces and use statements
        return $context;
    }

Usage Example

Example #1
0
 /**
  * @param array $annotations
  * @param null  $context
  */
 public function __construct($annotations = [], $context = null)
 {
     $this->annotations = new SplObjectStorage();
     if (count($annotations) !== 0) {
         if ($context === null) {
             $context = Context::detect(1);
         }
         $this->addAnnotations($annotations, $context);
     }
 }
All Usage Examples Of Swagger\Context::detect