Expands class name into full name.
public static expandClassName ( $name, ReflectionClass $rc ) : string | ||
$rc | ReflectionClass | |
return | string | full name |
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); } } }
/** * 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; }
/** @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, '\\'); } }
/** * 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; }
/** privates */ private function autowireProps() { $rc = $this->getReflection(); foreach ($rc->getProperties() as $property) { $annotation = $property->getAnnotation('prop'); if ($annotation) { $type = (string) $property->getAnnotation('var'); $propName = isset($annotation['name']) ? $annotation['name'] : $property->name; //todo: implement @prop(type=specifictype) $value = $this->props->get($propName); if (in_array($type, ['array', 'int', 'numeric', 'string'])) { if (!Validators::is($value, $type)) { throw new PropValidationException("Prop {$propName} is not of required type {$type}but " . $this->getObjectType($value)); } } else { $type = PhpReflection::expandClassName($type, $rc); if (!$value instanceof $type) { throw new PropValidationException("Prop {$propName} is not of required type {$type}, but " . $this->getObjectType($value)); } } $this->{$property->name} = $value; } } }