Detailed Description
DefaultFactory is used to create service instances.
The concrete service implementations and building parameters are described in the given configuration. Dependencies are injected in the following ways:
- Constructor injection: Each constructor parameter is resolved either from the instance configuration or, if not specified, from an existing class that is of the same type as defined in the type hinting.
- Setter injection: Each parameter of the instance configuration, that is not part of the constructor is injected through an appropriate setter method, if existing.
To ensure that an instance implements a certain interface, the interface name may be passed together with the instance name to the DefaultFactory::addInterfaces method. The interfaces required by framework classes are already defined internally (see DefaultFactory::$requiredInterfaces).
The following configuration shows the definition of the View instance:
In this example views are instances of SmartyView. They are not shared, meaning that clients get a fresh instance each time they request a view. The parameters compileCheck, caching, ... are either defined in the constructor of SmartyView or there is an appropriate setter method (e.g. SmartyView::setCompileCheck). Any other constructor parameter of SmartyView is tried to resolve from either another instance defined in the configuration or an existing class. HtmlView is defined as an alias for View.
Definition at line 60 of file DefaultFactory.php.
Public Member Functions | |
__construct (Configuration $configuration) | |
getInstance ($name, $dynamicConfiguration=[]) | |
getNewInstance ($name, $dynamicConfiguration=[]) | |
getInstanceOf ($class, $dynamicConfiguration=[]) | |
registerInstance ($name, $instance) | |
addInterfaces (array $interfaces) | |
clear () | |
Protected Member Functions | |
getSetterName ($property) | |
createInstance ($name, $configuration, $instanceKey) | |
resolveValue ($value) | |
getInterface ($name) | |
Protected Attributes | |
$requiredInterfaces | |
$instances = [] | |
$configuration = null | |
$currentStack = [] | |
Constructor & Destructor Documentation
◆ __construct()
__construct | ( | Configuration | $configuration | ) |
Constructor.
- Parameters
-
$configuration Configuration instance used to construct instances.
Definition at line 108 of file DefaultFactory.php.
Member Function Documentation
◆ getInstance()
getInstance | ( | $name, | |
$dynamicConfiguration = [] |
|||
) |
- See also
- Factory::getInstance()
Implements Factory.
Definition at line 116 of file DefaultFactory.php.
◆ getNewInstance()
getNewInstance | ( | $name, | |
$dynamicConfiguration = [] |
|||
) |
- See also
- Factory::getNewInstance()
Implements Factory.
Definition at line 144 of file DefaultFactory.php.
◆ getInstanceOf()
getInstanceOf | ( | $class, | |
$dynamicConfiguration = [] |
|||
) |
- See also
- Factory::getInstanceOf()
Implements Factory.
Definition at line 156 of file DefaultFactory.php.
◆ registerInstance()
registerInstance | ( | $name, | |
$instance | |||
) |
- See also
- Factory::registerInstance()
Implements Factory.
Definition at line 169 of file DefaultFactory.php.
◆ addInterfaces()
addInterfaces | ( | array | $interfaces | ) |
- See also
- Factory::addInterfaces()
Implements Factory.
Definition at line 177 of file DefaultFactory.php.
◆ clear()
clear | ( | ) |
◆ getSetterName()
|
protected |
Get the setter method name for a property.
- Parameters
-
$property
- Returns
- String
Definition at line 193 of file DefaultFactory.php.
◆ createInstance()
|
protected |
Create an instance from the given configuration array.
- Parameters
-
$name The name by which the instance may be retrieved later using Factory::getInstance(). The name is also used to check the interface (see Factory::addInterfaces()) $configuration Associative array with key value pairs for instance properties passed to the constructor, setters or public properties and the special keys: - '__class': optional, denotes the instance class name
- '__shared' optional, if true, the instance may be reused using Factory::getInstance())
- '__ref' optional, defines this instance as an alias of the referenced instance
$instanceKey The name under which the instance is registered for later retrieval
- Returns
- Object
Definition at line 210 of file DefaultFactory.php.
◆ resolveValue()
|
protected |
Resolve a configuration value into a parameter.
- Parameters
-
$value
- Returns
- Mixed
Definition at line 331 of file DefaultFactory.php.
◆ getInterface()
|
protected |
Get the interface that is required to be implemented by the given instance.
- Parameters
-
$name The name of the instance
- Returns
- Interface or null, if undefined
Definition at line 376 of file DefaultFactory.php.
Member Data Documentation
◆ $requiredInterfaces
|
protected |
Interfaces that must be implemented by the given instances used in the framework.
Definition at line 66 of file DefaultFactory.php.
◆ $instances
|
protected |
The registry for already created instances.
Definition at line 95 of file DefaultFactory.php.
◆ $configuration
|
protected |
The Configuration instance that holds the instance definitions.
Definition at line 100 of file DefaultFactory.php.
◆ $currentStack
|
protected |
Definition at line 102 of file DefaultFactory.php.