public
|
#
__construct( string $instanceId = EmbeDi::DefaultInstanceId, string $presetId = null, array $config = [] )
Create container with provided id
Create container with provided id
Parameters
- $instanceId
- $presetId
- If set will lookup configuration in depper array level
- $config
- Configuration of EmbeDi
|
public
|
|
public
|
|
public static
Maslosoft\EmbeDi\EmbeDi
|
#
fly( string $instanceId = EmbeDi::DefaultInstanceId, $presetId = null )
Get flyweight instance of embedi.
This will create instance only if $instanceId insntace id does not exists.
If named instance exists, or was ever create - existing instance will be used.
Use this function especially when require many EmbeDi calls,
for instance when creating EmbeDi in loops:
foreach($configs as $config)
{
(new EmbeDi)->apply($config);
}
In abowe example at each loop iteration new EmbeDi instance is created.
While it is still lightweight, it's unnessesary overhead.
Get flyweight instance of embedi.
This will create instance only if $instanceId insntace id does not exists.
If named instance exists, or was ever create - existing instance will be used.
Use this function especially when require many EmbeDi calls,
for instance when creating EmbeDi in loops:
foreach($configs as $config)
{
(new EmbeDi)->apply($config);
}
In abowe example at each loop iteration new EmbeDi instance is created.
While it is still lightweight, it's unnessesary overhead.
This can be made in slightly more optimized way by using fly function:
foreach($configs as $config)
{
EmbeDi::fly()->apply($config);
}
In above example only one instance of EmbeDi is used.
Parameters
Returns
|
public
|
|
public
|
#
setAdapters( $adapters )
TODO Create AdaptersManager
TODO Create AdaptersManager
|
public
|
|
public
|
#
addConfig( mixed[] $source )
Add configuration source for later use
Config should have keys of component id and values of config.
Example:
[
'logger' => [
'class' => Monolog\Logger\Logger,
],
'mangan' => [
'@logger' => 'logger'
]
]
Attributes starting with @ denotes that link to other
config component should be used. In example above, mangan field logger
will be configured with monolog logger.
Add configuration source for later use
Config should have keys of component id and values of config.
Example:
[
'logger' => [
'class' => Monolog\Logger\Logger,
],
'mangan' => [
'@logger' => 'logger'
]
]
Attributes starting with @ denotes that link to other
config component should be used. In example above, mangan field logger
will be configured with monolog logger.
Deprecated
Use Maslosoft\EmbeDi\Adapters\ArrayAdapter instead
Parameters
|
public
boolean
|
#
isStored( $object )
Check whenever current configuration is stored.
Check whenever current configuration is stored.
Returns
boolean
|
public
object
|
#
configure( object $object )
Configure existing object from previously stored configuration.
Typically this will will be called in your class constructor.
Will try to find configuration in adapters if it's not stored.
TODO Use SourceManager here, before adapters
TODO Create AdaptersManager and use here
Configure existing object from previously stored configuration.
Typically this will will be called in your class constructor.
Will try to find configuration in adapters if it's not stored.
TODO Use SourceManager here, before adapters
TODO Create AdaptersManager and use here
Parameters
Returns
object
|
public
object
|
#
apply( string|mixed[][] $configuration, object $object = null )
Apply configuration to object from array.
Apply configuration to object from array.
This can also create object if passed configuration array have class field.
Example of creating object:
$config = [
'class' => Vendor\Component::class,
'title' => 'bar'
];
(new Embedi)->apply($config);
Example of applying config to existing object:
$config = [
'title' => 'bar'
];
(new Embedi)->apply($config, new Vendor\Component);
If $configuration arguments is string, it will simply instantiate class:
(new Embedi)->apply('Vendor\Package\Component');
Parameters
- $configuration
- $object
- Object to configure, set to null to create new one
Returns
object
|
public
mixed[][]
|
#
export( object $object, string[] $fields = [] )
Export object configuration to array
Export object configuration to array
Parameters
Returns
mixed[][]
|
public
mixed[]
|
#
store( object $object, string[] $fields = [], boolean $update = false )
Store object configuration.
Store object configuration.
This will be typically called in init method of your component.
After storing config, configuration will be available in configure method.
configure method should be called in your class constructor.
If you store config and have configure method call,
after subsequent creations of your component will be configured by EmbeDi.
Both methods could be called in constructor, if you don't need additional
initialization code after configuring object.
Example workflow:
class Component
{
public $title = '';
public function __construct()
{
(new EmbeDi)->configure($this);
}
public function init()
{
(new EmbeDi)->store($this);
}
}
$c1 = new Component();
$c1->title = 'foo';
$c1->init();
$c2 = new Component();
echo $c2->title;
Parameter $fields tell's EmbeDi to store only subset of class fields.
Example:
(new EmbeDi)->store($this, ['title']);
Parameter $update tell's EmbeDi to update existing configuration.
By default configuration is not ovveriden on subsequent store calls.
This is done on purpose, to not mess basic configuration.
Parameters
- $object
- Object to store
- $fields
- Fields to store
- $update
- Whenever to update existing configuration
Returns
mixed[] Stored data
|