parseAnnotation() публичный статический Метод

Returns an annotation value.
public static parseAnnotation ( Reflector $ref, $name ) : string | null
$ref Reflector
Результат string | null
Пример #1
0
 private static function mockInjectedProperties()
 {
     /** @var \ReflectionProperty $property */
     foreach (self::$reflectedClass->getProperties() as $property) {
         if (Nette\DI\PhpReflection::parseAnnotation($property, 'inject') !== NULL || Nette\DI\PhpReflection::parseAnnotation($property, 'autowire') !== NULL) {
             if ($mockedParameterClass = Nette\DI\PhpReflection::parseAnnotation($property, 'var')) {
                 $mockedParameterClass = Nette\DI\PhpReflection::expandClassName($mockedParameterClass, Nette\DI\PhpReflection::getDeclaringClass($property));
             }
             self::setProperty($mockedParameterClass, $property);
         }
     }
 }
Пример #2
0
 /**
  * Generates list of properties with annotation @inject.
  * @return array
  * @internal
  */
 public static function getInjectProperties($class)
 {
     $res = array();
     foreach (get_class_vars($class) as $name => $foo) {
         $rp = new \ReflectionProperty($class, $name);
         if (PhpReflection::parseAnnotation($rp, 'inject') !== NULL) {
             if ($type = PhpReflection::parseAnnotation($rp, 'var')) {
                 $type = PhpReflection::expandClassName($type, PhpReflection::getDeclaringClass($rp));
             }
             $res[$name] = $type;
         }
     }
     return $res;
 }
Пример #3
0
 /** @return string|NULL */
 private function resolveEntityClass($entity, $recursive = array())
 {
     $entity = $this->normalizeEntity($entity instanceof Statement ? $entity->getEntity() : $entity);
     if (is_array($entity)) {
         if (($service = $this->getServiceName($entity[0])) || $entity[0] instanceof Statement) {
             $entity[0] = $this->resolveEntityClass($entity[0], $recursive);
             if (!$entity[0]) {
                 return;
             } elseif (isset($this->definitions[$service]) && $this->definitions[$service]->getImplement()) {
                 // @Implement::create
                 return $entity[1] === 'create' ? $this->resolveServiceClass($service, $recursive) : NULL;
             }
         }
         try {
             $reflection = Nette\Utils\Callback::toReflection($entity[0] === '' ? $entity[1] : $entity);
             $refClass = $reflection instanceof \ReflectionMethod ? $reflection->getDeclaringClass() : NULL;
         } catch (\ReflectionException $e) {
         }
         if (isset($e) || $refClass && (!$reflection->isPublic() || PHP_VERSION_ID >= 50400 && $refClass->isTrait() && !$reflection->isStatic())) {
             $name = array_slice(array_keys($recursive), -1);
             throw new ServiceCreationException(sprintf("Factory '%s' used in service '%s' is not callable.", Nette\Utils\Callback::toString($entity), $name[0]));
         }
         $class = preg_replace('#[|\\s].*#', '', PhpReflection::parseAnnotation($reflection, 'return'));
         if ($class) {
             $class = $refClass ? PhpReflection::expandClassName($class, $refClass) : ltrim($class, '\\');
         }
         return $class;
     } elseif ($service = $this->getServiceName($entity)) {
         // alias or factory
         if (Strings::contains($service, '\\')) {
             // @\Class
             return ltrim($service, '\\');
         }
         return $this->definitions[$service]->getImplement() ?: $this->resolveServiceClass($service, $recursive);
     } elseif (is_string($entity)) {
         if (!class_exists($entity) || !($rc = new ReflectionClass($entity)) || !$rc->isInstantiable()) {
             $name = array_slice(array_keys($recursive), -1);
             throw new ServiceCreationException("Class {$entity} used in service '{$name['0']}' not found or is not instantiable.");
         }
         return ltrim($entity, '\\');
     }
 }
Пример #4
0
 /**
  * Generates list of properties with annotation @inject.
  * @return array
  */
 public static function getInjectProperties(\ReflectionClass $class, $container = NULL)
 {
     $res = array();
     foreach ($class->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) {
         $type = PhpReflection::parseAnnotation($property, 'var');
         if (PhpReflection::parseAnnotation($property, 'inject') === NULL) {
             continue;
         } elseif (!$type) {
             throw new Nette\InvalidStateException("Property {$property} has no @var annotation.");
         }
         $type = PhpReflection::expandClassName($type, PhpReflection::getDeclaringClass($property));
         if (!class_exists($type) && !interface_exists($type)) {
             throw new Nette\InvalidStateException("Class or interface '{$type}' used in @var annotation at {$property} not found. Check annotation and 'use' statements.");
         } elseif ($container && !$container->getByType($type, FALSE)) {
             throw new ServiceCreationException("Service of type {$type} used in @var annotation at {$property} not found. Did you register it in configuration file?");
         }
         $res[$property->getName()] = $type;
     }
     return $res;
 }