class ObjectManager implements ObjectManagerInterface (View source)

Object Manager

Constants

protected KEY_INSTANCE

protected KEY_SCOPE

protected KEY_FACTORY

protected KEY_FACTORY_ARGUMENTS

protected KEY_ARGUMENT_TYPE

protected KEY_ARGUMENT_VALUE

protected KEY_CLASS_NAME

protected KEY_PACKAGE

protected KEY_LOWERCASE_NAME

Properties

protected ApplicationContext $context

The configuration context for this Flow run

protected array $allSettings

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

protected array $objects
protected DependencyProxy[] $dependencyProxies
protected array $classesBeingInstantiated
protected array $cachedLowerCasedObjectNames
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.

protected SplObjectStorage $internalShutdownObjects

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

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

T
get(T>|string $objectName, mixed ...$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|false
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|null
getInstance(string $objectName)

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

object|null
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, mixed $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.

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

T get(T>|string $objectName, mixed ...$constructorArguments)

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

Parameters

T>|string $objectName

The name of the object to return an instance of

mixed ...$constructorArguments

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

Return Value

T

The object instance

Exceptions

CannotBuildObjectException
UnknownObjectException
InvalidArgumentException
InvalidConfigurationTypeException

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|false getObjectNameByClassName(string $className)

Returns the object name corresponding to a given class name.

Parameters

string $className

The class name

Return Value

string|false

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

Objects of scope sessions are assumed to be the real session object, not the lazy loading proxy.

Parameters

string $objectName

The object name

object $instance

A prebuilt instance

Return Value

void

Exceptions

WrongScopeException
UnknownObjectException

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|null 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|null

The object instance or null

object|null 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

object|null

DependencyProxy createLazyDependency(string $hash, mixed $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

mixed $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.

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