-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[PropertyInfo] Add PropertyAttributesExtractor
#57601
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
base: 7.4
Are you sure you want to change the base?
Changes from all commits
5e667cf
aaa35ac
8c72472
3928042
5001a53
80e402b
450cabb
ceafe5d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -12,6 +12,7 @@ | |||||||||||||||||||||
namespace Symfony\Component\PropertyInfo\Extractor; | ||||||||||||||||||||||
|
||||||||||||||||||||||
use Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface; | ||||||||||||||||||||||
use Symfony\Component\PropertyInfo\PropertyAttributesExtractorInterface; | ||||||||||||||||||||||
use Symfony\Component\PropertyInfo\PropertyInitializableExtractorInterface; | ||||||||||||||||||||||
use Symfony\Component\PropertyInfo\PropertyListExtractorInterface; | ||||||||||||||||||||||
use Symfony\Component\PropertyInfo\PropertyReadInfo; | ||||||||||||||||||||||
|
@@ -36,7 +37,7 @@ | |||||||||||||||||||||
* | ||||||||||||||||||||||
* @final | ||||||||||||||||||||||
*/ | ||||||||||||||||||||||
class ReflectionExtractor implements PropertyListExtractorInterface, PropertyTypeExtractorInterface, PropertyAccessExtractorInterface, PropertyInitializableExtractorInterface, PropertyReadInfoExtractorInterface, PropertyWriteInfoExtractorInterface, ConstructorArgumentTypeExtractorInterface | ||||||||||||||||||||||
class ReflectionExtractor implements PropertyListExtractorInterface, PropertyTypeExtractorInterface, PropertyAccessExtractorInterface, PropertyInitializableExtractorInterface, PropertyReadInfoExtractorInterface, PropertyWriteInfoExtractorInterface, ConstructorArgumentTypeExtractorInterface, PropertyAttributesExtractorInterface | ||||||||||||||||||||||
{ | ||||||||||||||||||||||
/** | ||||||||||||||||||||||
* @internal | ||||||||||||||||||||||
|
@@ -507,6 +508,30 @@ public function getWriteInfo(string $class, string $property, array $context = [ | |||||||||||||||||||||
return $noneProperty; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
public function getAttributes(string $class, string $property, array $context = []): ?array | ||||||||||||||||||||||
{ | ||||||||||||||||||||||
try { | ||||||||||||||||||||||
$reflClass = new \ReflectionClass($class); | ||||||||||||||||||||||
} catch (\ReflectionException) { | ||||||||||||||||||||||
return null; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
if (!$reflClass->hasProperty($property)) { | ||||||||||||||||||||||
return null; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
$attributes = []; | ||||||||||||||||||||||
$reflProperty = $reflClass->getProperty($property); | ||||||||||||||||||||||
foreach ($reflProperty->getAttributes() as $attribute) { | ||||||||||||||||||||||
$attributes[] = [ | ||||||||||||||||||||||
'name' => $attribute->getName(), | ||||||||||||||||||||||
'arguments' => $attribute->getArguments(), | ||||||||||||||||||||||
]; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
return $attributes; | ||||||||||||||||||||||
Comment on lines
+525
to
+532
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Why not returning attributes themselves? So that we can have all the attribute data (repeated, etc) and use the If it was because you want to decouple |
||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
/** | ||||||||||||||||||||||
* @return LegacyType[]|null | ||||||||||||||||||||||
*/ | ||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,29 @@ | ||||||
<?php | ||||||
|
||||||
/* | ||||||
* This file is part of the Symfony package. | ||||||
* | ||||||
* (c) Fabien Potencier <fabien@symfony.com> | ||||||
* | ||||||
* For the full copyright and license information, please view the LICENSE | ||||||
* file that was distributed with this source code. | ||||||
*/ | ||||||
|
||||||
namespace Symfony\Component\PropertyInfo; | ||||||
|
||||||
/** | ||||||
* @author Andrew Alyamovsky <andrew.alyamovsky@gmail.com> | ||||||
*/ | ||||||
interface PropertyAttributesExtractorInterface | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
To be consistent with other extractors. For example, even if |
||||||
{ | ||||||
/** | ||||||
* Gets the attributes of the property. | ||||||
* | ||||||
* Returns an array of attributes, each attribute is an associative array with the following keys: | ||||||
* - name: The fully-qualified class name of the attribute | ||||||
* - arguments: An associative array of attribute arguments if present | ||||||
Comment on lines
+23
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the |
||||||
* | ||||||
* @return array<int, array{name: string, arguments: array<array-key, mixed>}>|null | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
*/ | ||||||
public function getAttributes(string $class, string $property, array $context = []): ?array; | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\PropertyInfo\Tests\Fixtures; | ||
|
||
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_PROPERTY)] | ||
class DummyAttribute | ||
{ | ||
public function __construct( | ||
public string $type, | ||
public string $name, | ||
public int $version, | ||
) { | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be consistent? (same in
FrameworkExtension
andPropertyInfoPass
)