PIFlex DependencyInjection
Very simple Dependency injection component for PHP Applications.
Source code: PIFlexDependencyInjection
Usage
The component is designed with ease of use in mind. It is as simple as:
<?php
// Creating container instance
$container = new PIFlex\DependencyInjection\DependencyContainer();
// Adding parameters
$container["some_parameter"] = "parameter value";
// Adding objects
$container["object"] = new MyClass(); // Any object
Installation
Composer
Add "piflex/dependencyinjection": "dev-master" to your composer.json file and run composer update
Manual installation
Just copy the library to location you want to. Have in mind that this way you have to set up autoloader on your own.
Structure and detailed usage
Injection
If object added implements ContainerAwareInterface, dependency container will automatically
inject itself into it. The easiest way to implement it is by simply extending
PIFlex\DependencyInjection\ContainerAware class.
<?php
// Container aware class
class ImAwareOfContainer extends PIFlex\DepdendencyInjection\ContainerAware
{
// Return MyClass instance injected in block above
public function getMyClass()
{
return $this->dependencyContainer["object"];
}
}
// Container automatically injects itself into it
$awareClass = new ImAwareOfContainer();
$container["aware_class"] = $awareClass;
// myClass will now be referenced to MyClass instance
$myClass = $awareClass->getMyClass();
Configurable
Dependency container has one awesome feature built-in: to load configuration file and pass it to objects
that require it. To each his own. Object can simply extend Configurable class (which itself extends)
ContainerAware. Configuration file must be in JSON format. It was chosen as PHP has built-in support
to decode it and it has a lot better readability than XML.
Example configuration project would look like this:
Config file
{
"class1": {
"setting1": "value 1",
"setting2": "value 2"
},
"class2": {
"setting1": "value 3",
"etc" : "something"
}
}
Example classes
<?php
use PIFlex\DependencyInjection\Configurable;
class ClassOne extends Configurable
{
// Abstract function that must be defined
// Defines which root config index belongs to
// this class
public function getConfigIndex()
{
return "class1";
}
public function getSetting1()
{
return $this->config["setting1"];
}
}
class ClassTwo extends Configurable
{
public function getConfigIndex()
{
return "class2";
}
}
$container = new PIFlex\DependencyInjection\DependencyContainer("VALID/CONFIG/FILE/PATH.json");
$container["class1"] = new ClassOne();
$container["class2"] = new ClassTwo();
$setting1 = $container["class1"]->getSetting1();
// $setting1 now equals "value1"
Advanced configuration
You will sometimes want to use more than one configuration file. Most obvious example is when your project has default configuration but you want to allow end-developer to change these settings without actually modifying that file. This is done by providing a second configuration file which will override settings defined in first one and add new settings if they exist.
Use is also very simple:
<?php
$dependencyContainer = new PIFlex\DependencyInjection\DependencyContainer("DEFAULT/CONFIG/FILE.json");
$dependencyContainer->loadConfig("ADDITIONAL/CONFIG/FILE.json");
$dependencyContainer->laodConfig("YET/ANOTHER/CONFIG/FILE.json");
Have in mind that configurations passed to objects are NOT passed by reference and therefore objects need to be added to container AFTER additional configuration files are loaded. If for some reason this isn't possible, objects still have access to up-to-date global configuration array.
Getting up-to-date configuration directly from container
<?php
class ConfigurableObject extends Configurable
{
public function getConfigIndex()
{
return "my_config_index";
}
private function getGlobalConfig()
{
$this->dependencyContainer->getConfig();
// OR
$this->dependencyContainer["config"];
}
private function getMyConfig()
{
$this->dependencyContainer->getConfig($this->getConfigIndex());
// OR
$this->dependencyContainer["config"][$this->getConfigIndex()];
}
}