ReflectionService
class ReflectionService (View source)
A service for acquiring reflection based information in a performant way. This service also builds up class schema information which is used by the Flow's persistence layer.
Reflection of classes of all active packages is triggered through the bootstrap's initializeReflectionService() method. In a development context, single classes may be re-reflected once files are modified whereas in a production context reflection is done once and successive requests read from the frozen caches for performance reasons.
The list of available classes is determined by the CompiletimeObjectManager which also triggers the initial build of reflection data in this service.
The invalidation of reflection cache entries is done by the CacheManager which in turn is triggered by signals sent by the file monitor.
The internal representation of cache data is optimized for memory consumption and speed by using constants which have an integer value.
Constants
protected VISIBILITY_PRIVATE |
|
protected VISIBILITY_PROTECTED |
|
protected VISIBILITY_PUBLIC |
|
protected DATA_INTERFACE_IMPLEMENTATIONS |
|
protected DATA_CLASS_INTERFACES |
|
protected DATA_CLASS_SUBCLASSES |
|
protected DATA_CLASS_TAGS_VALUES |
|
protected DATA_CLASS_ANNOTATIONS |
|
protected DATA_CLASS_ABSTRACT |
|
protected DATA_CLASS_FINAL |
|
protected DATA_CLASS_READONLY |
|
protected DATA_CLASS_METHODS |
|
protected DATA_CLASS_PROPERTIES |
|
protected DATA_METHOD_FINAL |
|
protected DATA_METHOD_STATIC |
|
protected DATA_METHOD_VISIBILITY |
|
protected DATA_METHOD_PARAMETERS |
|
protected DATA_METHOD_DECLARED_RETURN_TYPE |
|
protected DATA_PROPERTY_TAGS_VALUES |
|
protected DATA_PROPERTY_ANNOTATIONS |
|
protected DATA_PROPERTY_VISIBILITY |
|
protected DATA_PROPERTY_TYPE |
|
protected DATA_PROPERTY_PROMOTED |
|
protected DATA_PARAMETER_POSITION |
|
protected DATA_PARAMETER_OPTIONAL |
|
protected DATA_PARAMETER_TYPE |
|
protected DATA_PARAMETER_ARRAY |
|
protected DATA_PARAMETER_CLASS |
|
protected DATA_PARAMETER_ALLOWS_NULL |
|
protected DATA_PARAMETER_DEFAULT_VALUE |
|
protected DATA_PARAMETER_BY_REFERENCE |
|
protected DATA_PARAMETER_SCALAR_DECLARATION |
|
protected DATA_PARAMETER_ANNOTATIONS |
|
Properties
protected | $annotationReader | ||
protected | $availableClassNames | ||
protected | $statusCache | ||
protected | $reflectionDataCompiletimeCache | ||
protected | $reflectionDataRuntimeCache | ||
protected | $classSchemataRuntimeCache | ||
protected | $logger | ||
protected | $packageManager | ||
protected | $environment | ||
protected | $context | ||
protected | $doctrinePhpParser | The doctrine PHP parser which can parse "use" statements. Is initialized lazily when it is first needed. |
|
protected | $useStatementsForClassCache | a cache which stores the use statements reflected for a particular class (only relevant for un-expanded "var" and "param" annotations) |
|
protected | $loadFromClassSchemaRuntimeCache | In Production context, with frozen caches, this flag will be true |
|
protected | $settings | ||
protected | $annotatedClasses | Array of annotation classnames and the names of classes which are annotated with them |
|
protected | $classesByMethodAnnotations | Array of method annotations and the classes and methods which are annotated with them |
|
protected ClassSchema|false> | $classSchemata | Schemata of all classes which can be persisted |
|
protected | $classesCurrentlyBeingForgotten | An array of class names which are currently being forgotten by forgetClass(). Acts as a safeguard against infinite loops. |
|
protected | $classReflectionData | Array with reflection information indexed by class name |
|
protected | $updatedReflectionData | Array with updated reflection information (e.g. in Development context after classes have changed) |
|
protected | $initialized | ||
protected | $methodAnnotationsRuntimeCache | A runtime cache for reflected method annotations to speed up repeating checks. |
Methods
No description
No description
No description
Initialize the reflection service lazily
Builds the reflection data cache during compile time.
Tells if the specified class is known to this reflection service and reflection information is available.
Returns the names of all classes known to this reflection service.
Searches for and returns the class name of the default implementation of the given interface name. If no class implementing the interface was found or more than one implementation was found in the package defining the interface, false is returned.
Searches for and returns all class names of implementations of the given object type (interface name). If no class implementing the interface was found, an empty array is returned.
Searches for and returns all names of classes inheriting the specified class.
Searches for and returns all names of classes which are tagged by the specified annotation. If no classes were found, an empty array is returned.
Tells if the specified class has the given annotation
Returns the specified class annotations or an empty array
Returns the specified class annotation or NULL.
Tells if the specified class implements the given interface
Tells if the specified class is abstract or not
Tells if the specified class is final or not
Tells if the specified class is readonly or not
Tells if the class is unconfigurable or not
Returns all class names of classes containing at least one method annotated with the given annotation class
Returns all names of methods of the given class that are annotated with the given annotation class
Tells if the specified method is final or not
Tells if the specified method is declared as static or not
Tells if the specified method is public
Tells if the specified method is protected
Tells if the specified method is private
Tells if the specified method is tagged with the given tag
Tells if a specific PHP attribute is to be ignored for reflection
Tells if the specified method has the given annotation
Returns the specified method annotations or an empty array
Returns the specified method annotation or NULL.
Returns the names of all properties of the specified class
Wrapper for method_exists() which tells if the given method exists.
Returns all tags and their values the specified method is tagged with
Returns an array of parameters of the given method. Each entry contains additional information about the parameter position, type hint etc.
Returns the declared return type of a method (for PHP < 7.0 this will always return null)
Searches for and returns all names of class properties which are tagged by the specified tag.
Returns all tags and their values the specified class property is tagged with
Returns the values of the specified class property tag
Returns the property type
Tells if the specified property is private
Tells if the specified property is promoted
Tells if the specified class property is tagged with the given tag
Tells if the specified property has the given annotation
Searches for and returns all names of class properties which are marked by the specified annotation. If no properties were found, an empty array is returned.
Returns the specified property annotations or an empty array
Returns the specified property annotation or NULL.
Returns the class schema for the given class
Initializes the ReflectionService, cleans the given class name and finally reflects the class if necessary.
Checks if the given class names match those which already have been reflected. If the given array contains class names not yet known to this service, these classes will be reflected.
Check if a specific annotation tag is configured to be ignored.
Reflects the given class and stores the results in this service's properties.
No description
No description
Expand shortened class names in "var" and "param" annotations, taking use statements into account.
Finds all parent classes of the given class
Builds class schemata from classes annotated as entities or value objects
No description
Adds properties of the class at hand to the class schema.
No description
Complete repository-to-entity assignments.
Assigns the repository of any aggregate root to all it's subclasses, unless they are aggregate root already.
Checks whether all aggregate roots having superclasses have a repository assigned up to the tip of their hierarchy.
Checks if the given class meets the requirements for a value object, i.e.
Converts the internal, optimized data structure of parameter information into a human-friendly array with speaking indexes.
Converts the given parameter reflection into an information array
Checks which classes lack a cache entry and removes their reflection data accordingly.
Forgets all reflection data related to the specified class
Tries to load the reflection data from the compile time cache.
Loads reflection data from the cache or reflects the class if needed.
Stores the current reflection data related to classes of the specified package in the PrecompiledReflectionData directory for the current context.
Filter an array of entries where keys are class names by being in the given package namespace.
Removes the precompiled reflection data of a frozen package
Exports the internal reflection data into the ReflectionData cache
Save reflection data to cache in Development context.
Save reflection data to cache in Production context.
Set updated reflection data to caches.
Clean a given class name from possibly prefixed backslash
Transform backslashes to underscores to provide a valid cache identifier.
Writes the given message along with the additional information into the log.
No description
No description
Details
void
setStatusCache(StringFrontend $cache)
Sets the status cache
The cache must be set before initializing the Reflection Service
void
setReflectionDataCompiletimeCache(VariableFrontend $cache)
No description
void
setReflectionDataRuntimeCache(VariableFrontend $cache)
No description
void
setClassSchemataRuntimeCache(VariableFrontend $cache)
No description
void
injectSettings(array $settings)
No description
void
injectLogger(LoggerInterface $logger)
No description
void
injectPackageManager(PackageManager $packageManager)
No description
void
injectEnvironment(Environment $environment)
No description
protected PhpParser
getDoctrinePhpParser()
No description
protected void
initialize()
Initialize the reflection service lazily
This method must be run only after all dependencies have been injected.
void
buildReflectionData(array $availableClassNames)
Builds the reflection data cache during compile time.
This method is called by the Compile Time Object Manager which also determines the list of classes to consider for reflection.
bool
isClassReflected(string $className)
Tells if the specified class is known to this reflection service and reflection information is available.
array
getAllClassNames()
Returns the names of all classes known to this reflection service.
string|bool
getDefaultImplementationClassNameForInterface(string $interfaceName)
Searches for and returns the class name of the default implementation of the given interface name. If no class implementing the interface was found or more than one implementation was found in the package defining the interface, false is returned.
array
getAllImplementationClassNamesForInterface(string $interfaceName)
Searches for and returns all class names of implementations of the given object type (interface name). If no class implementing the interface was found, an empty array is returned.
array
getAllSubClassNamesForClass(string $className)
Searches for and returns all names of classes inheriting the specified class.
If no class inheriting the given class was found, an empty array is returned.
array
getClassNamesByAnnotation(string $annotationClassName)
Searches for and returns all names of classes which are tagged by the specified annotation. If no classes were found, an empty array is returned.
bool
isClassAnnotatedWith(string $className, string $annotationClassName)
Tells if the specified class has the given annotation
array
getClassAnnotations(string $className, string|null $annotationClassName = null)
Returns the specified class annotations or an empty array
object|null
getClassAnnotation(string $className, string $annotationClassName)
Returns the specified class annotation or NULL.
If multiple annotations are set on the target you will get the first instance of them.
bool
isClassImplementationOf(string $className, string $interfaceName)
Tells if the specified class implements the given interface
bool
isClassAbstract(string $className)
Tells if the specified class is abstract or not
bool
isClassFinal(string $className)
Tells if the specified class is final or not
bool
isClassReadonly(string $className)
Tells if the specified class is readonly or not
bool
isClassUnconfigurable(string $className)
Tells if the class is unconfigurable or not
array
getClassesContainingMethodsAnnotatedWith(string $annotationClassName)
Returns all class names of classes containing at least one method annotated with the given annotation class
array
getMethodsAnnotatedWith(string $className, string $annotationClassName)
Returns all names of methods of the given class that are annotated with the given annotation class
bool
isMethodFinal(string $className, string $methodName)
Tells if the specified method is final or not
bool
isMethodStatic(string $className, string $methodName)
Tells if the specified method is declared as static or not
bool
isMethodPublic(string $className, string $methodName)
Tells if the specified method is public
bool
isMethodProtected(string $className, string $methodName)
Tells if the specified method is protected
bool
isMethodPrivate(string $className, string $methodName)
Tells if the specified method is private
bool
isMethodTaggedWith(string $className, string $methodName, string $tag)
Tells if the specified method is tagged with the given tag
bool
isAttributeIgnored(string $attributeName)
Tells if a specific PHP attribute is to be ignored for reflection
bool
isMethodAnnotatedWith(string $className, string $methodName, string $annotationClassName)
Tells if the specified method has the given annotation
array
getMethodAnnotations(string $className, string $methodName, string|null $annotationClassName = null)
Returns the specified method annotations or an empty array
object|null
getMethodAnnotation(string $className, string $methodName, string $annotationClassName)
Returns the specified method annotation or NULL.
If multiple annotations are set on the target you will get the first instance of them.
array
getClassPropertyNames(string $className)
Returns the names of all properties of the specified class
bool
hasMethod(string $className, string $methodName)
Wrapper for method_exists() which tells if the given method exists.
array
getMethodTagsValues(string $className, string $methodName)
Returns all tags and their values the specified method is tagged with
array
getMethodParameters(string $className, string $methodName)
Returns an array of parameters of the given method. Each entry contains additional information about the parameter position, type hint etc.
string|null
getMethodDeclaredReturnType(string $className, string $methodName)
Returns the declared return type of a method (for PHP < 7.0 this will always return null)
array
getPropertyNamesByTag(string $className, string $tag)
Searches for and returns all names of class properties which are tagged by the specified tag.
If no properties were found, an empty array is returned.
mixed
getPropertyTagsValues(string $className, string $propertyName)
Returns all tags and their values the specified class property is tagged with
getPropertyTagValues(string $className, string $propertyName, string $tag)
Returns the values of the specified class property tag
string|null
getPropertyType(string $className, string $propertyName)
Returns the property type
bool
isPropertyPrivate(string $className, string $propertyName)
Tells if the specified property is private
bool
isPropertyPromoted(string $className, string $propertyName)
Tells if the specified property is promoted
bool
isPropertyTaggedWith(string $className, string $propertyName, string $tag)
Tells if the specified class property is tagged with the given tag
bool
isPropertyAnnotatedWith(string $className, string $propertyName, string $annotationClassName)
Tells if the specified property has the given annotation
array
getPropertyNamesByAnnotation(string $className, string $annotationClassName)
Searches for and returns all names of class properties which are marked by the specified annotation. If no properties were found, an empty array is returned.
array
getPropertyAnnotations(string $className, string $propertyName, string $annotationClassName = null)
Returns the specified property annotations or an empty array
object|null
getPropertyAnnotation(string $className, string $propertyName, string $annotationClassName)
Returns the specified property annotation or NULL.
If multiple annotations are set on the target you will get the first instance of them.
ClassSchema|null
getClassSchema(string|object $classNameOrObject)
Returns the class schema for the given class
protected string
prepareClassReflectionForUsage(string $className)
Initializes the ReflectionService, cleans the given class name and finally reflects the class if necessary.
protected void
reflectEmergedClasses()
Checks if the given class names match those which already have been reflected. If the given array contains class names not yet known to this service, these classes will be reflected.
protected bool
isTagIgnored(string $tagName)
Check if a specific annotation tag is configured to be ignored.
protected void
reflectClass(string $className)
Reflects the given class and stores the results in this service's properties.
int
reflectClassProperty(string $className, PropertyReflection $property)
No description
protected array|null
reflectPropertyTag(string $className, PropertyReflection $property, string $tagName, array $tagValues)
No description
protected void
addParentClass(string $className, ClassReflection $parentClass)
No description
protected void
addImplementedInterface(string $className, ClassReflection $interface)
No description
protected void
reflectClassMethod(string $className, MethodReflection $method)
No description
protected void
reflectClassMethodParameter(string $className, MethodReflection $method, ParameterReflection $parameter)
No description
protected string
expandType(ClassReflection $class, string $type)
Expand shortened class names in "var" and "param" annotations, taking use statements into account.
protected array
getParentClasses(ClassReflection $class, array $parentClasses = [])
Finds all parent classes of the given class
protected void
buildClassSchemata(array $classNames)
Builds class schemata from classes annotated as entities or value objects
protected ClassSchema
buildClassSchema(string $className)
No description
protected void
addPropertiesToClassSchema(ClassSchema $classSchema)
Adds properties of the class at hand to the class schema.
Properties will be added if they have a var annotation && (!transient-annotation && !inject-annotation)
Invalid annotations will cause an exception to be thrown.
protected bool
evaluateClassPropertyAnnotationsForSchema(ClassSchema $classSchema, string $propertyName)
No description
protected void
completeRepositoryAssignments()
Complete repository-to-entity assignments.
This method looks for repositories that declare themselves responsible for a specific model and sets a repository classname on the corresponding models.
It then walks the inheritance chain for all aggregate roots and checks the subclasses for their aggregate root status - if no repository is assigned yet, that will be done.
protected void
makeChildClassesAggregateRoot(ClassSchema $classSchema)
Assigns the repository of any aggregate root to all it's subclasses, unless they are aggregate root already.
protected void
ensureAggregateRootInheritanceChainConsistency()
Checks whether all aggregate roots having superclasses have a repository assigned up to the tip of their hierarchy.
protected void
checkValueObjectRequirements(string $className)
Checks if the given class meets the requirements for a value object, i.e.
does have a constructor and does not have any setter methods.
protected array
convertParameterDataToArray(array $parametersInformation)
Converts the internal, optimized data structure of parameter information into a human-friendly array with speaking indexes.
protected array
convertParameterReflectionToArray(ParameterReflection $parameter, MethodReflection $method)
Converts the given parameter reflection into an information array
protected void
forgetChangedClasses()
Checks which classes lack a cache entry and removes their reflection data accordingly.
protected void
forgetClass((int|string) $className)
Forgets all reflection data related to the specified class
protected bool
loadClassReflectionCompiletimeCache()
Tries to load the reflection data from the compile time cache.
The compile time cache is only supported for Development context and thus this function will return in any other context.
If no reflection data was found, this method will at least load the precompiled reflection data of any possible frozen package. Even if precompiled reflection data could be loaded, false will be returned in order to signal that other packages still need to be reflected.
protected void
loadOrReflectClassIfNecessary(string $className)
Loads reflection data from the cache or reflects the class if needed.
If the class is completely unknown, this method won't try to load or reflect it. If it is known and reflection data has been loaded already, it won't be loaded again.
In Production context, with frozen caches, this method will load reflection data for the specified class from the runtime cache.
void
freezePackageReflection((int|string) $packageKey)
Stores the current reflection data related to classes of the specified package in the PrecompiledReflectionData directory for the current context.
This method is used by the package manager.
protected array
filterArrayByClassesInPackageNamespace(array $array, int|string $packageKey)
Filter an array of entries where keys are class names by being in the given package namespace.
void
unfreezePackageReflection(string $packageKey)
Removes the precompiled reflection data of a frozen package
This method is used by the package manager.
void
saveToCache()
Exports the internal reflection data into the ReflectionData cache
This method is triggered by a signal which is connected to the bootstrap's shutdown sequence.
If the reflection data has previously been loaded from the runtime cache, saving it is omitted as changes are not expected.
In Production context the whole cache is written at once and then frozen in order to be consistent. Frozen cache data in Development is only produced for classes contained in frozen packages.
protected void
saveDevelopmentData()
Save reflection data to cache in Development context.
protected void
saveProductionData()
Save reflection data to cache in Production context.
protected void
updateReflectionData()
Set updated reflection data to caches.
protected string
cleanClassName(string $className)
Clean a given class name from possibly prefixed backslash
protected string
produceCacheIdentifierFromClassName(string $className)
Transform backslashes to underscores to provide a valid cache identifier.
protected void
log(string $message, string $severity = LogLevel::INFO, array $additionalData = [])
Writes the given message along with the additional information into the log.
protected string
getPrecompiledReflectionStoragePath()
No description
protected bool
hasFrozenCacheInProduction()
No description