Initial commit
This commit is contained in:
46
#pma/libraries/di/AliasItem.php
Normal file
46
#pma/libraries/di/AliasItem.php
Normal file
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
/* vim: set expandtab sw=4 ts=4 sts=4: */
|
||||
/**
|
||||
* Holds the PMA\libraries\di\AliasItem class
|
||||
*
|
||||
* @package PMA
|
||||
*/
|
||||
namespace PMA\libraries\di;
|
||||
|
||||
/**
|
||||
* Class AliasItem
|
||||
*
|
||||
* @package PMA\libraries\di
|
||||
*/
|
||||
class AliasItem implements Item
|
||||
{
|
||||
|
||||
/** @var Container */
|
||||
protected $container;
|
||||
|
||||
/** @var string */
|
||||
protected $target;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param Container $container Container
|
||||
* @param string $target Target
|
||||
*/
|
||||
public function __construct(Container $container, $target)
|
||||
{
|
||||
$this->container = $container;
|
||||
$this->target = $target;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the target item
|
||||
*
|
||||
* @param array $params Parameters
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($params = array())
|
||||
{
|
||||
return $this->container->get($this->target, $params);
|
||||
}
|
||||
}
|
167
#pma/libraries/di/Container.php
Normal file
167
#pma/libraries/di/Container.php
Normal file
@ -0,0 +1,167 @@
|
||||
<?php
|
||||
/* vim: set expandtab sw=4 ts=4 sts=4: */
|
||||
/**
|
||||
* Holds the PMA\libraries\di\Container class
|
||||
*
|
||||
* @package PMA
|
||||
*/
|
||||
namespace PMA\libraries\di;
|
||||
|
||||
/**
|
||||
* Class Container
|
||||
*
|
||||
* @package PMA\libraries\di
|
||||
*/
|
||||
class Container
|
||||
{
|
||||
|
||||
/**
|
||||
* @var Item[] $content
|
||||
*/
|
||||
protected $content = array();
|
||||
|
||||
/**
|
||||
* @var Container
|
||||
*/
|
||||
protected static $defaultContainer;
|
||||
|
||||
/**
|
||||
* Create a dependency injection container
|
||||
*
|
||||
* @param Container $base Container
|
||||
*/
|
||||
public function __construct(Container $base = null)
|
||||
{
|
||||
if (isset($base)) {
|
||||
$this->content = $base->content;
|
||||
} else {
|
||||
$this->alias('container', 'Container');
|
||||
}
|
||||
$this->set('Container', $this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an object with given name and parameters
|
||||
*
|
||||
* @param string $name Name
|
||||
* @param array $params Paramters
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($name, $params = array())
|
||||
{
|
||||
if (isset($this->content[$name])) {
|
||||
return $this->content[$name]->get($params);
|
||||
}
|
||||
|
||||
if (isset($GLOBALS[$name])) {
|
||||
return $GLOBALS[$name];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove an object from container
|
||||
*
|
||||
* @param string $name Name
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function remove($name)
|
||||
{
|
||||
unset($this->content[$name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rename an object in container
|
||||
*
|
||||
* @param string $name Name
|
||||
* @param string $newName New name
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function rename($name, $newName)
|
||||
{
|
||||
$this->content[$newName] = $this->content[$name];
|
||||
$this->remove($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set values in the container
|
||||
*
|
||||
* @param string|array $name Name
|
||||
* @param mixed $value Value
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function set($name, $value = null)
|
||||
{
|
||||
if (is_array($name)) {
|
||||
foreach ($name as $key => $val) {
|
||||
$this->set($key, $val);
|
||||
}
|
||||
return;
|
||||
}
|
||||
$this->content[$name] = new ValueItem($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a service in the container
|
||||
*
|
||||
* @param string $name Name
|
||||
* @param mixed $service Service
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function service($name, $service = null)
|
||||
{
|
||||
if (!isset($service)) {
|
||||
$service = $name;
|
||||
}
|
||||
$this->content[$name] = new ServiceItem($this, $service);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a factory in the container
|
||||
*
|
||||
* @param string $name Name
|
||||
* @param mixed $factory Factory
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function factory($name, $factory = null)
|
||||
{
|
||||
if (!isset($factory)) {
|
||||
$factory = $name;
|
||||
}
|
||||
$this->content[$name] = new FactoryItem($this, $factory);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register an alias in the container
|
||||
*
|
||||
* @param string $name Name
|
||||
* @param string $target Target
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function alias($name, $target)
|
||||
{
|
||||
// The target may be not defined yet
|
||||
$this->content[$name] = new AliasItem($this, $target);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the global default container
|
||||
*
|
||||
* @return Container
|
||||
*/
|
||||
public static function getDefaultContainer()
|
||||
{
|
||||
if (!isset(static::$defaultContainer)) {
|
||||
static::$defaultContainer = new Container();
|
||||
}
|
||||
return static::$defaultContainer;
|
||||
}
|
||||
}
|
29
#pma/libraries/di/FactoryItem.php
Normal file
29
#pma/libraries/di/FactoryItem.php
Normal file
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
/* vim: set expandtab sw=4 ts=4 sts=4: */
|
||||
/**
|
||||
* Holds the PMA\libraries\di\FactoryItem class
|
||||
*
|
||||
* @package PMA
|
||||
*/
|
||||
namespace PMA\libraries\di;
|
||||
|
||||
/**
|
||||
* Factory manager
|
||||
*
|
||||
* @package PMA\libraries\di
|
||||
*/
|
||||
class FactoryItem extends ReflectorItem
|
||||
{
|
||||
|
||||
/**
|
||||
* Construct an instance
|
||||
*
|
||||
* @param array $params Parameters
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($params = array())
|
||||
{
|
||||
return $this->invoke($params);
|
||||
}
|
||||
}
|
25
#pma/libraries/di/Item.php
Normal file
25
#pma/libraries/di/Item.php
Normal file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
/* vim: set expandtab sw=4 ts=4 sts=4: */
|
||||
/**
|
||||
* Holds the PMA\libraries\di\Item class
|
||||
*
|
||||
* @package PMA
|
||||
*/
|
||||
namespace PMA\libraries\di;
|
||||
|
||||
/**
|
||||
* Interface Item
|
||||
*
|
||||
* @package PMA\libraries\di
|
||||
*/
|
||||
interface Item
|
||||
{
|
||||
|
||||
/**
|
||||
* Get a value from the item
|
||||
*
|
||||
* @param array $params Parameters
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($params = array());
|
||||
}
|
124
#pma/libraries/di/ReflectorItem.php
Normal file
124
#pma/libraries/di/ReflectorItem.php
Normal file
@ -0,0 +1,124 @@
|
||||
<?php
|
||||
/* vim: set expandtab sw=4 ts=4 sts=4: */
|
||||
/**
|
||||
* Holds the PMA\libraries\di\ReflectorItem class
|
||||
*
|
||||
* @package PMA
|
||||
*/
|
||||
namespace PMA\libraries\di;
|
||||
|
||||
/**
|
||||
* Reflector manager
|
||||
*
|
||||
* @package PMA\libraries\di
|
||||
*/
|
||||
abstract class ReflectorItem implements Item
|
||||
{
|
||||
|
||||
/** @var Container */
|
||||
private $_container;
|
||||
|
||||
/** @var \Reflector */
|
||||
private $_reflector;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param Container $container Container
|
||||
* @param mixed $definition Definition
|
||||
*/
|
||||
public function __construct(Container $container, $definition)
|
||||
{
|
||||
$this->_container = $container;
|
||||
$this->_reflector = self::_resolveReflector($definition);
|
||||
}
|
||||
|
||||
/**
|
||||
* Invoke the reflector with given parameters
|
||||
*
|
||||
* @param array $params Parameters
|
||||
* @return mixed
|
||||
*/
|
||||
protected function invoke($params = array())
|
||||
{
|
||||
$args = array();
|
||||
$reflector = $this->_reflector;
|
||||
if ($reflector instanceof \ReflectionClass) {
|
||||
$constructor = $reflector->getConstructor();
|
||||
if (isset($constructor)) {
|
||||
$args = $this->_resolveArgs(
|
||||
$constructor->getParameters(),
|
||||
$params
|
||||
);
|
||||
}
|
||||
return $reflector->newInstanceArgs($args);
|
||||
}
|
||||
/** @var \ReflectionFunctionAbstract $reflector */
|
||||
$args = $this->_resolveArgs(
|
||||
$reflector->getParameters(),
|
||||
$params
|
||||
);
|
||||
if ($reflector instanceof \ReflectionMethod) {
|
||||
/** @var \ReflectionMethod $reflector */
|
||||
return $reflector->invokeArgs(null, $args);
|
||||
}
|
||||
/** @var \ReflectionFunction $reflector */
|
||||
return $reflector->invokeArgs($args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Getting required arguments with given parameters
|
||||
*
|
||||
* @param \ReflectionParameter[] $required Arguments
|
||||
* @param array $params Parameters
|
||||
*
|
||||
*@return array
|
||||
*/
|
||||
private function _resolveArgs($required, $params = array())
|
||||
{
|
||||
$args = array();
|
||||
foreach ($required as $param) {
|
||||
$name = $param->getName();
|
||||
$type = $param->getClass();
|
||||
if (isset($type)) {
|
||||
$type = $type->getName();
|
||||
}
|
||||
if (isset($params[$name])) {
|
||||
$args[] = $params[$name];
|
||||
} elseif (is_string($type) && isset($params[$type])) {
|
||||
$args[] = $params[$type];
|
||||
} else {
|
||||
$content = $this->_container->get($name);
|
||||
if (isset($content)) {
|
||||
$args[] = $content;
|
||||
} elseif (is_string($type)) {
|
||||
$args[] = $this->_container->get($type);
|
||||
} else {
|
||||
$args[] = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $args;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the reflection
|
||||
*
|
||||
* @param mixed $definition Definition
|
||||
*
|
||||
* @return \Reflector
|
||||
*/
|
||||
private static function _resolveReflector($definition)
|
||||
{
|
||||
if (function_exists($definition)) {
|
||||
return new \ReflectionFunction($definition);
|
||||
}
|
||||
if (is_string($definition)) {
|
||||
$definition = explode('::', $definition);
|
||||
}
|
||||
if (!isset($definition[1])) {
|
||||
return new \ReflectionClass($definition[0]);
|
||||
}
|
||||
return new \ReflectionMethod($definition[0], $definition[1]);
|
||||
}
|
||||
}
|
34
#pma/libraries/di/ServiceItem.php
Normal file
34
#pma/libraries/di/ServiceItem.php
Normal file
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
/* vim: set expandtab sw=4 ts=4 sts=4: */
|
||||
/**
|
||||
* Holds the PMA\libraries\di\ServiceItem class
|
||||
*
|
||||
* @package PMA
|
||||
*/
|
||||
namespace PMA\libraries\di;
|
||||
|
||||
/**
|
||||
* Service manager
|
||||
*
|
||||
* @package PMA\libraries\di
|
||||
*/
|
||||
class ServiceItem extends ReflectorItem
|
||||
{
|
||||
|
||||
/** @var mixed */
|
||||
protected $instance;
|
||||
|
||||
/**
|
||||
* Get the instance of the service
|
||||
*
|
||||
* @param array $params Parameters
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($params = array())
|
||||
{
|
||||
if (!isset($this->instance)) {
|
||||
$this->instance = $this->invoke();
|
||||
}
|
||||
return $this->instance;
|
||||
}
|
||||
}
|
41
#pma/libraries/di/ValueItem.php
Normal file
41
#pma/libraries/di/ValueItem.php
Normal file
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
/* vim: set expandtab sw=4 ts=4 sts=4: */
|
||||
/**
|
||||
* Holds the PMA\libraries\di\ValueItem class
|
||||
*
|
||||
* @package PMA
|
||||
*/
|
||||
namespace PMA\libraries\di;
|
||||
|
||||
/**
|
||||
* Value manager
|
||||
*
|
||||
* @package PMA\libraries\di
|
||||
*/
|
||||
class ValueItem implements Item
|
||||
{
|
||||
|
||||
/** @var mixed */
|
||||
protected $value;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param mixed $value Value
|
||||
*/
|
||||
public function __construct($value)
|
||||
{
|
||||
$this->value = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value
|
||||
*
|
||||
* @param array $params Parameters
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($params = array())
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user