Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: Type declaration support for property injection #2782

Merged
merged 2 commits into from
Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -561,8 +561,18 @@ protected function autowireProperties(array &$objectConfigurations)
if (!array_key_exists($propertyName, $properties)) {
/** @var Inject $injectAnnotation */
$injectAnnotation = $this->reflectionService->getPropertyAnnotation($className, $propertyName, Inject::class);
$objectName = $injectAnnotation->name !== null ? $injectAnnotation->name : trim(implode('', $this->reflectionService->getPropertyTagValues($className, $propertyName, 'var')), ' \\');
$configurationProperty = new ConfigurationProperty($propertyName, $objectName, ConfigurationProperty::PROPERTY_TYPES_OBJECT, null, $injectAnnotation->lazy);
$enableLazyInjection = $injectAnnotation->lazy;
$objectName = $injectAnnotation->name;
if ($objectName === null) {
$objectName = $this->reflectionService->getPropertyType($className, $propertyName);
if ($objectName !== null) {
$enableLazyInjection = false; # See: https://github.com/neos/flow-development-collection/issues/2114
}
}
if ($objectName === null) {
$objectName = trim(implode('', $this->reflectionService->getPropertyTagValues($className, $propertyName, 'var')), ' \\');
}
$configurationProperty = new ConfigurationProperty($propertyName, $objectName, ConfigurationProperty::PROPERTY_TYPES_OBJECT, null, $enableLazyInjection);
$properties[$propertyName] = $configurationProperty;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ public function buildPropertyInjectionCodeByString(Configuration $objectConfigur
if (!isset($this->objectConfigurations[$propertyObjectName])) {
$configurationSource = $objectConfiguration->getConfigurationSourceHint();
if (!isset($propertyObjectName[0])) {
throw new ObjectException\UnknownObjectException('Malformed DocComent block for a property in class "' . $className . '".', 1360171313);
throw new ObjectException\UnknownObjectException(sprintf('Malformed DocComment block for property %s in class %s.', $propertyName, $className), 1360171313);
}
if ($propertyObjectName[0] === '\\') {
throw new ObjectException\UnknownObjectException('The object name "' . $propertyObjectName . '" which was specified as a property in the object configuration of object "' . $objectConfiguration->getObjectName() . '" (' . $configurationSource . ') starts with a leading backslash.', 1277827579);
Expand All @@ -466,6 +466,7 @@ public function buildPropertyInjectionCodeByString(Configuration $objectConfigur
return $result;
}

# Disable lazy property injection, see https://github.com/neos/flow-development-collection/issues/2114
if ($propertyConfiguration->isLazyLoading() && $this->objectConfigurations[$propertyObjectName]->getScope() !== Configuration::SCOPE_PROTOTYPE) {
return $this->buildLazyPropertyInjectionCode($propertyObjectName, $propertyClassName, $propertyName, $preparedSetterArgument);
} else {
Expand Down
18 changes: 17 additions & 1 deletion Neos.Flow/Classes/Reflection/ReflectionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class ReflectionService
const DATA_PROPERTY_TAGS_VALUES = 14;
const DATA_PROPERTY_ANNOTATIONS = 15;
const DATA_PROPERTY_VISIBILITY = 24;
const DATA_PROPERTY_TYPE = 26;
const DATA_PARAMETER_POSITION = 16;
const DATA_PARAMETER_OPTIONAL = 17;
const DATA_PARAMETER_TYPE = 18;
Expand Down Expand Up @@ -996,6 +997,18 @@ public function getPropertyTagValues($className, $propertyName, $tag)
return (isset($this->classReflectionData[$className][self::DATA_CLASS_PROPERTIES][$propertyName][self::DATA_PROPERTY_TAGS_VALUES][$tag])) ? $this->classReflectionData[$className][self::DATA_CLASS_PROPERTIES][$propertyName][self::DATA_PROPERTY_TAGS_VALUES][$tag] : [];
}

/**
* Returns the property type
*
* @param $className
* @param $propertyName
* @return string|null
*/
public function getPropertyType($className, $propertyName)
kdambekalns marked this conversation as resolved.
Show resolved Hide resolved
{
return $this->classReflectionData[$className][self::DATA_CLASS_PROPERTIES][$propertyName][self::DATA_PROPERTY_TYPE] ?? null;
}

/**
* Tells if the specified property is private
*
Expand Down Expand Up @@ -1303,6 +1316,9 @@ public function reflectClassProperty($className, PropertyReflection $property)
{
$propertyName = $property->getName();
$this->classReflectionData[$className][self::DATA_CLASS_PROPERTIES][$propertyName] = [];
if ($property->hasType()) {
$this->classReflectionData[$className][self::DATA_CLASS_PROPERTIES][$propertyName][self::DATA_PROPERTY_TYPE] = trim((string)$property->getType(), '?');
}

$visibility = $property->isPublic() ? self::VISIBILITY_PUBLIC : ($property->isProtected() ? self::VISIBILITY_PROTECTED : self::VISIBILITY_PRIVATE);
$this->classReflectionData[$className][self::DATA_CLASS_PROPERTIES][$propertyName][self::DATA_PROPERTY_VISIBILITY] = $visibility;
Expand All @@ -1315,7 +1331,7 @@ public function reflectClassProperty($className, PropertyReflection $property)
$this->classReflectionData[$className][self::DATA_CLASS_PROPERTIES][$propertyName][self::DATA_PROPERTY_TAGS_VALUES][$tagName] = $tagValues;
}

foreach ($this->annotationReader->getPropertyAnnotations($property, $propertyName) as $annotation) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice one… that parameter was passed since 2013 but not used? Ever? 🤪

foreach ($this->annotationReader->getPropertyAnnotations($property) as $annotation) {
$this->classReflectionData[$className][self::DATA_CLASS_PROPERTIES][$propertyName][self::DATA_PROPERTY_ANNOTATIONS][get_class($annotation)][] = $annotation;
}
if (PHP_MAJOR_VERSION >= 8) {
Expand Down