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
VISIBILITY_PRIVATE |
|
VISIBILITY_PROTECTED |
|
VISIBILITY_PUBLIC |
|
DATA_INTERFACE_IMPLEMENTATIONS |
|
DATA_CLASS_INTERFACES |
|
DATA_CLASS_SUBCLASSES |
|
DATA_CLASS_TAGS_VALUES |
|
DATA_CLASS_ANNOTATIONS |
|
DATA_CLASS_ABSTRACT |
|
DATA_CLASS_FINAL |
|
DATA_CLASS_METHODS |
|
DATA_CLASS_PROPERTIES |
|
DATA_METHOD_FINAL |
|
DATA_METHOD_STATIC |
|
DATA_METHOD_VISIBILITY |
|
DATA_METHOD_PARAMETERS |
|
DATA_METHOD_DECLARED_RETURN_TYPE |
|
DATA_PROPERTY_TAGS_VALUES |
|
DATA_PROPERTY_ANNOTATIONS |
|
DATA_PROPERTY_VISIBILITY |
|
DATA_PROPERTY_TYPE |
|
DATA_PARAMETER_POSITION |
|
DATA_PARAMETER_OPTIONAL |
|
DATA_PARAMETER_TYPE |
|
DATA_PARAMETER_ARRAY |
|
DATA_PARAMETER_CLASS |
|
DATA_PARAMETER_ALLOWS_NULL |
|
DATA_PARAMETER_DEFAULT_VALUE |
|
DATA_PARAMETER_BY_REFERENCE |
|
DATA_PARAMETER_SCALAR_DECLARATION |
|
Properties
protected Reader | $annotationReader | ||
protected array | $availableClassNames | ||
protected StringFrontend | $statusCache | ||
protected VariableFrontend | $reflectionDataCompiletimeCache | ||
protected VariableFrontend | $reflectionDataRuntimeCache | ||
protected VariableFrontend | $classSchemataRuntimeCache | ||
protected LoggerInterface | $logger | ||
protected PackageManager | $packageManager | ||
protected Environment | $environment | ||
protected ApplicationContext | $context | ||
protected PhpParser | $doctrinePhpParser | The doctrine PHP parser which can parse "use" statements. Is initialized lazily when it is first needed. |
|
protected array | $useStatementsForClassCache | a cache which stores the use statements reflected for a particular class (only relevant for un-expanded "var" and "param" annotations) |
|
protected bool | $loadFromClassSchemaRuntimeCache | In Production context, with frozen caches, this flag will be true |
|
protected array | $settings | ||
protected array | $annotatedClasses | Array of annotation classnames and the names of classes which are annotated with them |
|
protected array | $classesByMethodAnnotations | Array of method annotations and the classes and methods which are annotated with them |
|
protected ClassSchema[] | $classSchemata | Schemata of all classes which can be persisted |
|
protected array | $classesCurrentlyBeingForgotten | An array of class names which are currently being forgotten by forgetClass(). Acts as a safeguard against infinite loops. |
|
protected array | $classReflectionData | Array with reflection information indexed by class name |
|
protected array | $updatedReflectionData | Array with updated reflection information (e.g. in Development context after classes have changed) |
|
protected bool | $initialized | ||
protected array | $methodAnnotationsRuntimeCache | A runtime cache for reflected method annotations to speed up repeating checks. |
Methods
Sets the dedicated class schema cache for runtime purposes
No description
Injects the (system) logger based on PSR-3.
Retrieves a singleton instance of the Doctrine PhpParser
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 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 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
Builds a class schema for the given class name.
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 were 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 an valid cache identifier.
Writes the given message along with the additional information into the log.
Determines the path to the precompiled reflection data.
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)
Sets the compile-time data cache
void
setReflectionDataRuntimeCache(VariableFrontend $cache)
Sets the runtime data cache
void
setClassSchemataRuntimeCache(VariableFrontend $cache)
Sets the dedicated class schema cache for runtime purposes
void
injectSettings(array $settings)
No description
void
injectLogger(LoggerInterface $logger)
Injects the (system) logger based on PSR-3.
void
injectPackageManager(PackageManager $packageManager)
No description
void
injectEnvironment(Environment $environment)
No description
protected PhpParser
getDoctrinePhpParser()
Retrieves a singleton instance of the Doctrine PhpParser
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 CompiletimeObjectManager 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.
mixed
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
object[]
getClassAnnotations(string $className, string $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
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
object[]
getMethodAnnotations(string $className, string $methodName, string $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
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.
array
getPropertyTagsValues(string $className, string $propertyName)
Returns all tags and their values the specified class property is tagged with
array
getPropertyTagValues(string $className, string $propertyName, string $tag)
Returns the values of the specified class property tag
string|null
getPropertyType($className, $propertyName)
Returns the property type
bool
isPropertyPrivate(string $className, string $propertyName)
Tells if the specified property is private
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.
object[]
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
getClassSchema(mixed $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
reflectPropertyTag(string $className, PropertyReflection $property, string $tagName, array $tagValues)
No description
protected void
addParentClass(string $className, ClassReflection $parentClass)
No description
protected
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 ClassReflection[]
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)
Builds a class schema for the given class name.
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(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(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, string $packageKey)
Filter an array of entries were 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
saveDevelopmentData()
Save reflection data to cache in Development context.
protected
saveProductionData()
Save reflection data to cache in Production context.
protected
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 an 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()
Determines the path to the precompiled reflection data.
protected bool
hasFrozenCacheInProduction()
No description