/**
  * Gets an annotation.
  *
  * @param  mixed  $from
  * @param  string $name
  * @return Annotation
  */
 protected function getAnnotation($from, $name)
 {
     if (!$this->reader) {
         $this->reader = new SimpleAnnotationReader();
         $this->reader->addNamespace($this->namespace);
     }
     $name = "{$this->namespace}\\{$name}";
     if ($from instanceof \ReflectionClass) {
         $annotation = $this->reader->getClassAnnotation($from, $name);
     } elseif ($from instanceof \ReflectionMethod) {
         $annotation = $this->reader->getMethodAnnotation($from, $name);
     } elseif ($from instanceof \ReflectionProperty) {
         $annotation = $this->reader->getPropertyAnnotation($from, $name);
     }
     return $annotation;
 }
示例#2
0
 public function createContextFromFilename($filename)
 {
     $tokens = $this->getFileTokens($filename);
     $class = $this->getNamespaceFromToken($tokens);
     $reader = new SimpleAnnotationReader();
     $reflClass = new \ReflectionClass($class);
     $entityAnnotation = $reader->getClassAnnotation($reflClass, Entity::class);
     if (!$entityAnnotation) {
         return;
     }
     $context = new Context();
     $context->setFile($filename);
     $context->setEntity($entityAnnotation);
     $propertyAnnotations = [];
     foreach ($reflClass->getProperties() as $property) {
         $annotation = $reader->getPropertyAnnotation($property, Property::class);
         if ($annotation && !$annotation->name) {
             $annotation->name = $property->getName();
             $context->addProperty($annotation);
         }
     }
     return $context;
 }