The PropertyInfo Component
Note
If you install this component outside of a Symfony application, you mustrequire the file in your code to enable the classautoloading mechanism provided by Composer. Readthis article for more details.
Additional dependencies may be required for some of the.
Usage
To use this component, create a new instance andprovide it with a set of information extractors:
- use Example\Namespace\YourAwesomeCoolClass;
- use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
- use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
- use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
- // a full list of extractors is shown further below
- $phpDocExtractor = new PhpDocExtractor();
- $reflectionExtractor = new ReflectionExtractor();
- // list of PropertyListExtractorInterface (any iterable)
- $listExtractors = [$reflectionExtractor];
- // list of PropertyTypeExtractorInterface (any iterable)
- $typeExtractors = [$phpDocExtractor, $reflectionExtractor];
- // list of PropertyDescriptionExtractorInterface (any iterable)
- $descriptionExtractors = [$phpDocExtractor];
- // list of PropertyAccessExtractorInterface (any iterable)
- $accessExtractors = [$reflectionExtractor];
- // list of PropertyInitializableExtractorInterface (any iterable)
- $propertyInitializableExtractors = [$reflectionExtractor];
- $propertyInfo = new PropertyInfoExtractor(
- $listExtractors,
- $typeExtractors,
- $descriptionExtractors,
- $accessExtractors,
- $propertyInitializableExtractors
- );
- // see below for more examples
- $class = YourAwesomeCoolClass::class;
- $properties = $propertyInfo->getProperties($class);
The order of extractor instances within an array matters: the first non-nullresult will be returned. That is why you must provide each category of extractorsas a separate array, even if an extractor provides information for more thanone category.
For example, while the ReflectionExtractor
and both provide list and type information it is probably better that:
The
ReflectionExtractor
has priority for list information so that all properties in a class (notjust mapped properties) are returned.The
has priority for type information so that entity metadata is used insteadof type-hinting to provide more accurate type information:
- use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
- use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
- $reflectionExtractor = new ReflectionExtractor();
- $doctrineExtractor = new DoctrineExtractor(/* ... */);
- $propertyInfo = new PropertyInfoExtractor(
- // List extractors
- [
- $reflectionExtractor,
- $doctrineExtractor
- ],
- // Type extractors
- [
- $doctrineExtractor,
- $reflectionExtractor
- ]
- );
The PropertyInfoExtractor
class exposes public methods to extract several types of information:
- :
getProperties()()
- :
getTypes()()
- :
getShortDescription()()
and - Property access details:
and
isWritable()()
- :
isInitializable()()
Note
Be sure to pass a class name, not an object to the extractor methods:
- // bad! It may work, but not with all extractors
- $propertyInfo->getProperties($awesomeObject);
- // Good!
- $propertyInfo->getProperties(get_class($awesomeObject));
- $propertyInfo->getProperties('Example\Namespace\YourAwesomeClass');
- $propertyInfo->getProperties(YourAwesomeClass::class);
List Information
Extractors that implement PropertyListExtractorInterface
provide the list of properties that are available on a class as an arraycontaining each property name as a string:
Type Information
Extractors that implement PropertyTypeExtractorInterface
provide for a property:
- $types = $propertyInfo->getTypes($class, $property);
- /*
- Example Result
- --------------
- array(1) {
- [0] =>
- class Symfony\Component\PropertyInfo\Type (6) {
- private $builtinType => string(6) "string"
- private $nullable => bool(false)
- private $class => NULL
- private $collection => bool(false)
- private $collectionKeyType => NULL
- private $collectionValueType => NULL
- }
- }
- */
See Type Objects for info about the Type
class.
Description Information
Extractors that implement PropertyDescriptionExtractorInterface
provide long and short descriptions from a properties annotations asstrings:
- $title = $propertyInfo->getShortDescription($class, $property);
- /*
- Example Result
- --------------
- string(41) "This is the first line of the DocComment."
- */
- $paragraph = $propertyInfo->getLongDescription($class, $property);
- /*
- Example Result
- --------------
- string(79):
- It can span multiple lines.
- */
Extractors that implement provide whether properties are readable or writable as booleans:
- $propertyInfo->isReadable($class, $property);
- // Example Result: bool(true)
- $propertyInfo->isWritable($class, $property);
- // Example Result: bool(false)
The ReflectionExtractor
looksfor getter/isser/setter/hasser method in addition to whether or not a property is publicto determine if it's accessible. This based on how the works.
Property Initializable Information
Extractors that implement provide whether properties are initializable through the class's constructor as booleans:
isInitializable()
returns true
if a constructor's parameter of the given class matches thegiven property name.
Tip
The main class implements all interfaces, delegating the extraction of propertyinformation to the extractors that have been registered with it.
This means that any method available on each of the extractors is alsoavailable on the main PropertyInfoExtractor
class.
Compared to the other extractors, type information extractors provide muchmore information than can be represented as simple scalar values. Becauseof this, type extractors return an array of objects for each type that the property supports.
For example, if a property supports both and string
(viathe @return int|string
annotation),PropertyInfoExtractor::getTypes()
will return an array containing two instances of the class.
Note
Most extractors will return only one Type
instance. The is currently the only extractor that returns multiple instances in the array.
Each object will provide 6 attributes, available in the 6 methods:
Type::getBuiltInType()
The method returns the built-in PHP data type, which can be one of thesestring values:
array
, bool
, callable
, float
, int
,iterable
, null
, object
, resource
or string
.
Constants inside the Type
class, in the form Type::BUILTINTYPE*
, are provided for convenience.
Type::isNullable()
The Type::isNullable()
method will return a boolean value indicating whether the property parametercan be set to null
.
Type::getClassName()
If the built-in PHP data typeis object
, the method will return the fully-qualified class or interface name accepted.
The Type::isCollection()
method will return a boolean value indicating if the property parameter isa collection - a non-scalar value capable of containing other values. Currentlythis returns true
if:
- The is ;
- The mutator method the property is derived from has a prefix of
add
orremove
(which are defined as the list of array mutator prefixes); - The phpDocumentor annotation is of type "collection" (e.g.
@var SomeClass<DateTime>
,@var SomeClass<integer,string>
,@var Doctrine\Common\Collections\Collection<App\Entity\SomeEntity>
, etc.)
New in version 4.2: The support of phpDocumentor collection types was introduced in Symfony 4.2.
Type::getCollectionKeyType() & Type::getCollectionValueType()
If the property is a collection, additional type objects may be returnedfor both the key and value types of the collection (if the information isavailable), via the Type::getCollectionKeyType()
and methods.
Extractors
The williterate over the relevant extractor classes in the order they were set, callthe appropriate method and return the first result that is not
null
.
While you can create your own extractors, the following are already availableto cover most use-cases:
ReflectionExtractor
Using PHP reflection, the provides list, type and access information from setter and accessor methods.It can also give the type of a property (even extracting it from the constructorarguments), and if it is initializable through the constructor. It supportsreturn and scalar types for PHP 7:
- use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
- $reflectionExtractor = new ReflectionExtractor();
- // List information.
- $reflectionExtractor->getProperties($class);
- // Type information.
- $reflectionExtractor->getTypes($class, $property);
- // Access information.
- $reflectionExtractor->isReadable($class, $property);
- $reflectionExtractor->isWritable($class, $property);
- // Initializable information
- $reflectionExtractor->isInitializable($class, $property);
New in version 4.1: The feature to extract the property types from constructor arguments wasintroduced in Symfony 4.1.
Note
When using the Symfony framework, this service is automatically registeredwhen the property_info
feature is enabled:
- # config/packages/framework.yaml
- framework:
- property_info:
- enabled: true
PhpDocExtractor
Note
This extractor depends on the library.
Using phpDocumentor Reflection to parse property and method annotations,the provides type and description information. This extractor is automaticallyregistered with the
propertyinfo
in the Symfony Framework _if the dependentlibrary is present:
- use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
- $phpDocExtractor = new PhpDocExtractor();
- // Type information.
- $phpDocExtractor->getTypes($class, $property);
- // Description information.
- $phpDocExtractor->getShortDescription($class, $property);
- $phpDocExtractor->getLongDescription($class, $property);
Note
This extractor depends on the symfony/serializer library.
Using from the Serializer component,the provides list information. This extractor is not registered automaticallywith the
property_info
service in the Symfony Framework:
DoctrineExtractor
Note
This extractor depends on the and doctrine/ormlibraries.
Using entity mapping data from , theDoctrineExtractor
provides list and type information. This extractor is not registered automaticallywith the property_info
service in the Symfony Framework:
- use Doctrine\ORM\EntityManager;
- use Doctrine\ORM\Tools\Setup;
- use Symfony\Bridge\Doctrine\PropertyInfo\DoctrineExtractor;
- $config = Setup::createAnnotationMetadataConfiguration([__DIR__], true);
- $entityManager = EntityManager::create([
- 'driver' => 'pdo_sqlite',
- // ...
- ], $config);
- $doctrineExtractor = new DoctrineExtractor($entityManager);
- // List information.
- $doctrineExtractor->getProperties($class);
- // Type information.
- $doctrineExtractor->getTypes($class, $property);
If you have enabled the PropertyInfo component with the FrameworkBundle,you can automatically register your extractor class with the propertyinfo
service by defining it as a service with one or more of the following[_tags]():
property_info.list_extractor
if it provides list information.property_info.type_extractor
if it provides type information.- if it provides description information.
property_info.access_extractor
if it provides access information.