class CompileTimeObjectManager extends ObjectManager (View source)

A specialized Object Manager which is able to do some basic dependency injection for singleton scoped objects. This Object Manager is used during compile time when the proxy class based DI mechanism is not yet available.

Properties

protected ApplicationContext $context

The configuration context for this Flow run

from  ObjectManager
protected array $allSettings

An array of settings of all packages, indexed by package key

from  ObjectManager
protected array $objects from  ObjectManager
protected DependencyProxy[] $dependencyProxies from  ObjectManager
protected array $classesBeingInstantiated from  ObjectManager
protected array $cachedLowerCasedObjectNames from  ObjectManager
protected SplObjectStorage $shutdownObjects

A SplObjectStorage containing those objects which need to be shutdown when the container shuts down. Each value of each entry is the respective shutdown method name.

from  ObjectManager
protected SplObjectStorage $internalShutdownObjects

A SplObjectStorage containing only those shutdown objects which have been registered for Flow.

from  ObjectManager
protected VariableFrontend $configurationCache
protected ReflectionService $reflectionService
protected ConfigurationManager $configurationManager
protected LoggerInterface $logger
protected array $objectConfigurations
protected array $registeredClassNames

A list of all class names known to the Object Manager

protected array $objectNameBuildStack
protected array $cachedClassNamesByScope

Methods

__construct(ApplicationContext $context)

Constructor for this Object Container

void
setObjects(array $objects)

Sets the objects array

void
injectAllSettings(array $settings)

Injects the global settings array, indexed by package key.

getContext()

Returns the context Flow is running in.

bool
isRegistered(string $objectName)

Returns true if an object with the given name is registered

bool
has(string $objectName)

Returns true if the container can return an entry for the given identifier.

void
registerShutdownObject(object $object, string $shutdownLifecycleMethodName)

Registers the passed shutdown lifecycle method for the given object

object
get(string $objectName, array ...$constructorArguments)

Returns a fresh or existing instance of the object specified by $objectName.

int
getScope(string $objectName)

Returns the scope of the specified object.

string|null
getCaseSensitiveObjectName(string $caseInsensitiveObjectName)

Returns the case sensitive object name of an object specified by a case insensitive object name. If no object of that name exists, false is returned.

string
getObjectNameByClassName(string $className)

Returns the object name corresponding to a given class name.

string
getClassNameByObjectName(string $objectName)

Returns the implementation class name for the specified object

string
getPackageKeyByObjectName(string $objectName)

Returns the key of the package the specified object is contained in.

void
setInstance(string $objectName, object $instance)

Sets the instance of the given object

bool
hasInstance(string $objectName)

Returns true if this object manager already has an instance for the specified object.

object
getInstance(string $objectName)

Returns the instance of the specified object or NULL if no instance has been registered yet.

mixed
getLazyDependencyByHash(string $hash, mixed $propertyReferenceVariable)

This method is used internally to retrieve either an actual (singleton) instance of the specified dependency or, if no instance exists yet, a Dependency Proxy object which automatically triggers the creation of an instance as soon as it is used the first time.

createLazyDependency(string $hash, string $propertyReferenceVariable, string $className, Closure $builder)

Creates a new DependencyProxy class for a dependency built through code identified through "hash" for a dependency of class $className. The closure in $builder contains code for actually creating the dependency instance once it needs to be materialized.

void
forgetInstance(string $objectName)

Unsets the instance of the given object

array
getSessionInstances()

Returns all instances of objects with scope session

void
shutdown()

Shuts down this Object Container by calling the shutdown methods of all object instances which were configured to be shut down.

array
getAllObjectConfigurations()

Returns all current object configurations.

object
buildObjectByFactory(string $objectName)

Invokes the Factory defined in the object configuration of the specified object in order to build an instance. Arguments which were defined in the object configuration are passed to the factory method.

object
instantiateClass(string $className, array $arguments)

Speed optimized alternative to ReflectionClass::newInstanceArgs()

void
callShutdownMethods(SplObjectStorage $shutdownObjects)

Executes the methods of the provided objects.

void
injectReflectionService(ReflectionService $reflectionService)

No description

void
injectConfigurationManager(ConfigurationManager $configurationManager)

No description

void
injectConfigurationCache(VariableFrontend $configurationCache)

Injects the configuration cache of the Object Framework

void
injectLogger(LoggerInterface $logger)

Injects the (system) logger based on PSR-3.

void
initialize(array $packages)

Initializes the the object configurations and some other parts of this Object Manager.

array
getRegisteredClassNames()

Returns a list of all class names, grouped by package key, which were registered by registerClassFiles()

array
getClassNamesByScope(int $scope)

Returns a list of class names, which are configured with the given scope

array
registerClassFiles(array $packages)

Traverses through all class files of the active packages and registers collects the class names as "all available class names". If the respective Flow settings say so, also function test classes are registered.

array
filterClassNamesFromConfiguration(array $classNames, array $includeClassesConfiguration)

Given an array of class names by package key this filters out classes that have been configured to be included by object management.

array
applyClassFilterConfiguration(array $classNames, array $filterConfiguration)

Filters the classnames available for object management by filter expressions that includes classes.

array
buildObjectsArray()

Builds the objects array which contains information about the registered objects, their scope, class, built method etc.

array
getObjectConfigurations()

Returns object configurations which were previously built by the ConfigurationBuilder.

Details

__construct(ApplicationContext $context)

Constructor for this Object Container

Parameters

ApplicationContext $context

The configuration context for this Flow run

void setObjects(array $objects)

Sets the objects array

Parameters

array $objects

An array of object names and some information about each registered object (scope, lower cased name etc.)

Return Value

void

void injectAllSettings(array $settings)

Injects the global settings array, indexed by package key.

Parameters

array $settings

The global settings

Return Value

void

ApplicationContext getContext()

Returns the context Flow is running in.

Return Value

ApplicationContext

the current context

bool isRegistered(string $objectName)

Returns true if an object with the given name is registered

Parameters

string $objectName

Name of the object

Return Value

bool

true if the object has been registered, otherwise false

Exceptions

InvalidArgumentException

bool has(string $objectName)

Returns true if the container can return an entry for the given identifier.

Returns false otherwise.

Parameters

string $objectName

Return Value

bool

void registerShutdownObject(object $object, string $shutdownLifecycleMethodName)

Registers the passed shutdown lifecycle method for the given object

Parameters

object $object

The object to register the shutdown method for

string $shutdownLifecycleMethodName

The method name of the shutdown method to be called

Return Value

void

object get(string $objectName, array ...$constructorArguments)

Returns a fresh or existing instance of the object specified by $objectName.

This specialized get() method is able to do setter injection for properties defined in the object configuration of the specified object.

Parameters

string $objectName

The name of the object to return an instance of

array ...$constructorArguments

Any number of arguments that should be passed to the constructor of the object

Return Value

object

The object instance

Exceptions

CannotBuildObjectException
UnresolvedDependenciesException
UnknownObjectException

int getScope(string $objectName)

Returns the scope of the specified object.

Parameters

string $objectName

The object name

Return Value

int

One of the Configuration::SCOPE_ constants

Exceptions

UnknownObjectException

string|null getCaseSensitiveObjectName(string $caseInsensitiveObjectName)

internal  
 

Returns the case sensitive object name of an object specified by a case insensitive object name. If no object of that name exists, false is returned.

In general, the case sensitive variant is used everywhere in Flow, however there might be special situations in which the case sensitive name is not available. This method helps you in these rare cases.

Parameters

string $caseInsensitiveObjectName

The object name in lower-, upper- or mixed case

Return Value

string|null

Either the mixed case object name or false if no object of that name was found.

string getObjectNameByClassName(string $className)

Returns the object name corresponding to a given class name.

Parameters

string $className

The class name

Return Value

string

The object name corresponding to the given class name

Exceptions

InvalidArgumentException

string getClassNameByObjectName(string $objectName)

Returns the implementation class name for the specified object

Parameters

string $objectName

The object name

Return Value

string

The class name corresponding to the given object name or false if no such object is registered

string getPackageKeyByObjectName(string $objectName)

internal  
 

Returns the key of the package the specified object is contained in.

Parameters

string $objectName

The object name

Return Value

string

The package key or false if no such object exists

void setInstance(string $objectName, object $instance)

Sets the instance of the given object

In the Compile Time Object Manager it is even allowed to set instances of not-yet-known objects as long as the Object Manager is not initialized, because some few parts need an object registry even before the Object Manager is fully functional.

Parameters

string $objectName

The object name

object $instance

A prebuilt instance

Return Value

void

bool hasInstance(string $objectName)

Returns true if this object manager already has an instance for the specified object.

Parameters

string $objectName

The object name

Return Value

bool

true if an instance already exists

object getInstance(string $objectName)

Returns the instance of the specified object or NULL if no instance has been registered yet.

Parameters

string $objectName

The object name

Return Value

object

The object or NULL

mixed getLazyDependencyByHash(string $hash, mixed $propertyReferenceVariable)

This method is used internally to retrieve either an actual (singleton) instance of the specified dependency or, if no instance exists yet, a Dependency Proxy object which automatically triggers the creation of an instance as soon as it is used the first time.

Internally used by the injectProperties method of generated proxy classes.

Parameters

string $hash
mixed $propertyReferenceVariable

Reference of the variable to inject into once the proxy is activated

Return Value

mixed

DependencyProxy createLazyDependency(string $hash, string $propertyReferenceVariable, string $className, Closure $builder)

Creates a new DependencyProxy class for a dependency built through code identified through "hash" for a dependency of class $className. The closure in $builder contains code for actually creating the dependency instance once it needs to be materialized.

Internally used by the injectProperties method of generated proxy classes.

Parameters

string $hash

An md5 hash over the code needed to actually build the dependency instance

string $propertyReferenceVariable

A first variable where the dependency needs to be injected into

string $className

Name of the class of the dependency which eventually will be instantiated

Closure $builder

An anonymous function which creates the instance to be injected

Return Value

DependencyProxy

void forgetInstance(string $objectName)

Unsets the instance of the given object

If run during standard runtime, the whole application might become unstable because certain parts might already use an instance of this object. Therefore this method should only be used in a setUp() method of a functional test case.

Parameters

string $objectName

The object name

Return Value

void

array getSessionInstances()

Returns all instances of objects with scope session

Return Value

array

void shutdown()

Shuts down this Object Container by calling the shutdown methods of all object instances which were configured to be shut down.

Return Value

void

array getAllObjectConfigurations()

Returns all current object configurations.

For internal use in bootstrap only. Can change anytime.

Return Value

array

protected object buildObjectByFactory(string $objectName)

Invokes the Factory defined in the object configuration of the specified object in order to build an instance. Arguments which were defined in the object configuration are passed to the factory method.

Parameters

string $objectName

Name of the object to build

Return Value

object

The built object

Exceptions

UnknownObjectException
InvalidConfigurationTypeException
CannotBuildObjectException

protected object instantiateClass(string $className, array $arguments)

Speed optimized alternative to ReflectionClass::newInstanceArgs()

Parameters

string $className

Name of the class to instantiate

array $arguments

Arguments to pass to the constructor

Return Value

object

The object

Exceptions

CannotBuildObjectException
Exception

protected void callShutdownMethods(SplObjectStorage $shutdownObjects)

Executes the methods of the provided objects.

Parameters

SplObjectStorage $shutdownObjects

Return Value

void

void injectReflectionService(ReflectionService $reflectionService)

No description

Parameters

ReflectionService $reflectionService

Return Value

void

void injectConfigurationManager(ConfigurationManager $configurationManager)

No description

Parameters

ConfigurationManager $configurationManager

Return Value

void

void injectConfigurationCache(VariableFrontend $configurationCache)

Injects the configuration cache of the Object Framework

Parameters

VariableFrontend $configurationCache

Return Value

void

void injectLogger(LoggerInterface $logger)

Injects the (system) logger based on PSR-3.

Parameters

LoggerInterface $logger

Return Value

void

void initialize(array $packages)

Initializes the the object configurations and some other parts of this Object Manager.

Parameters

array $packages

An array of active packages to consider

Return Value

void

array getRegisteredClassNames()

Returns a list of all class names, grouped by package key, which were registered by registerClassFiles()

Return Value

array

array getClassNamesByScope(int $scope)

Returns a list of class names, which are configured with the given scope

Parameters

int $scope

One of the ObjectConfiguration::SCOPE_ constants

Return Value

array

An array of class names configured with the given scope

protected array registerClassFiles(array $packages)

Traverses through all class files of the active packages and registers collects the class names as "all available class names". If the respective Flow settings say so, also function test classes are registered.

For performance reasons this function ignores classes whose name ends with "Exception".

Parameters

array $packages

A list of packages to consider

Return Value

array

A list of class names which were discovered in the given packages

Exceptions

InvalidConfigurationTypeException

protected array filterClassNamesFromConfiguration(array $classNames, array $includeClassesConfiguration)

Given an array of class names by package key this filters out classes that have been configured to be included by object management.

Parameters

array $classNames

2-level array - key of first level is package key, value of second level is classname (FQN)

array $includeClassesConfiguration

array of includeClasses configurations

Return Value

array

The input array with all configured to be included in object management added in

Exceptions

InvalidConfigurationTypeException
NoSuchOptionException

protected array applyClassFilterConfiguration(array $classNames, array $filterConfiguration)

Filters the classnames available for object management by filter expressions that includes classes.

Parameters

array $classNames

All classnames per package

array $filterConfiguration

The filter configuration to apply

Return Value

array

the remaining class

Exceptions

InvalidConfigurationTypeException

protected array buildObjectsArray()

Builds the objects array which contains information about the registered objects, their scope, class, built method etc.

Return Value

array

array getObjectConfigurations()

Returns object configurations which were previously built by the ConfigurationBuilder.

Return Value

array