detect() public static method

Create a Context based on the debug_backtrace
public static detect ( integer $index ) : Context
$index integer
return Context
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);
     }
 }
Example #2
0
 public function testDetect()
 {
     $context = Context::detect();
     $line = __LINE__ - 1;
     $this->assertSame('ContextTest', $context->class);
     $this->assertSame('\\SwaggerTests\\ContextTest', $context->fullyQualifiedName($context->class));
     $this->assertSame('testDetect', $context->method);
     $this->assertSame(__FILE__, $context->filename);
     $this->assertSame($line, $context->line);
     $this->assertSame('SwaggerTests', $context->namespace);
     //        $this->assertCount(1, $context->uses); // Context::detect() doesn't pick up USE statements (yet)
 }
 /**
  * @param array $properties
  */
 public function __construct($properties)
 {
     if (isset($properties['_context'])) {
         $this->_context = $properties['_context'];
         unset($properties['_context']);
     } elseif (Analyser::$context) {
         $this->_context = Analyser::$context;
     } else {
         $this->_context = Context::detect(1);
     }
     if ($this->_context->is('annotations') === false) {
         $this->_context->annotations = [];
     }
     $this->_context->annotations[] = $this;
     $nestedContext = new Context(['nested' => $this], $this->_context);
     foreach ($properties as $property => $value) {
         if (property_exists($this, $property)) {
             $this->{$property} = $value;
             if (is_array($value)) {
                 foreach ($value as $key => $annotation) {
                     if (is_object($annotation) && $annotation instanceof AbstractAnnotation) {
                         $this->{$property}[$key] = $this->nested($annotation, $nestedContext);
                     }
                 }
             }
         } elseif ($property !== 'value') {
             $this->{$property} = $value;
         } elseif (is_array($value)) {
             $annotations = [];
             foreach ($value as $annotation) {
                 if (is_object($annotation) && $annotation instanceof AbstractAnnotation) {
                     $annotations[] = $annotation;
                 } else {
                     Logger::notice('Unexpected field in ' . $this->identity() . ' in ' . $this->_context);
                 }
             }
             $this->merge($annotations);
         } elseif (is_object($value)) {
             $this->merge([$value]);
         } else {
             Logger::notice('Unexpected parameter in ' . $this->identity());
         }
     }
 }
Example #4
0
 /**
  *
  * @param string $comment Contents of a comment block
  * @return AbstractAnnotation[]
  */
 protected function parseComment($comment)
 {
     $analyser = new Analyser();
     $context = Context::detect(1);
     $context->line -= 1;
     // correct generated lines: "<?php\n" and "/**\n"
     return $analyser->fromComment("<?php\n/**\n * " . implode("\n * ", explode("\n", $comment)) . "\n*/", $context);
 }
Example #5
0
 /**
  * Process a single code snippet.
  *
  * @param string $contents PHP code.
  * @param string $context The original location of the contents.
  * @return Swagger
  */
 public function examine($contents, $context = null)
 {
     if ($context === null) {
         $context = Context::detect(1);
         if ($context->filename) {
             $context->filename .= '(examined at line ' . $context->line . ')';
         }
     }
     if (strpos($contents, '<?php') === false) {
         throw new \Exception('No PHP code detected, T_OPEN_TAG("<?php") not found in ' . $context);
     }
     $parser = new Parser($this->getProcessors());
     $parser->parseContents($contents, $context);
     $this->processParser($parser);
     $this->processResults();
     return $this;
 }