dolibarr/htdocs/includes/swiftmailer/lib/classes/Swift/DependencyContainer.php

374 lines
9.3 KiB
PHP
Raw Normal View History

2016-04-16 18:15:03 +02:00
<?php
/*
* This file is part of SwiftMailer.
* (c) 2004-2009 Chris Corbyn
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Dependency Injection container.
*
2018-01-21 15:55:56 +01:00
* @author Chris Corbyn
2016-04-16 18:15:03 +02:00
*/
class Swift_DependencyContainer
{
/** Constant for literal value types */
const TYPE_VALUE = 0x0001;
/** Constant for new instance types */
const TYPE_INSTANCE = 0x0010;
/** Constant for shared instance types */
const TYPE_SHARED = 0x0100;
/** Constant for aliases */
const TYPE_ALIAS = 0x1000;
/** Singleton instance */
2018-01-21 15:55:56 +01:00
private static $instance = null;
2016-04-16 18:15:03 +02:00
/** The data container */
2018-01-21 15:55:56 +01:00
private $store = array();
2016-04-16 18:15:03 +02:00
/** The current endpoint in the data container */
2018-01-21 15:55:56 +01:00
private $endPoint;
2016-04-16 18:15:03 +02:00
/**
* Constructor should not be used.
*
* Use {@link getInstance()} instead.
*/
public function __construct()
{
}
/**
* Returns a singleton of the DependencyContainer.
*
2018-01-21 15:55:56 +01:00
* @return self
2016-04-16 18:15:03 +02:00
*/
public static function getInstance()
{
2018-01-21 15:55:56 +01:00
if (!isset(self::$instance)) {
self::$instance = new self();
2016-04-16 18:15:03 +02:00
}
2018-01-21 15:55:56 +01:00
return self::$instance;
2016-04-16 18:15:03 +02:00
}
/**
* List the names of all items stored in the Container.
*
* @return array
*/
public function listItems()
{
2018-01-21 15:55:56 +01:00
return array_keys($this->store);
2016-04-16 18:15:03 +02:00
}
/**
* Test if an item is registered in this container with the given name.
*
* @see register()
*
* @param string $itemName
*
* @return bool
*/
public function has($itemName)
{
2018-01-21 15:55:56 +01:00
return array_key_exists($itemName, $this->store)
&& isset($this->store[$itemName]['lookupType']);
2016-04-16 18:15:03 +02:00
}
/**
* Lookup the item with the given $itemName.
*
* @see register()
*
* @param string $itemName
*
* @return mixed
2018-01-21 15:55:56 +01:00
*
* @throws Swift_DependencyException If the dependency is not found
2016-04-16 18:15:03 +02:00
*/
public function lookup($itemName)
{
if (!$this->has($itemName)) {
throw new Swift_DependencyException(
'Cannot lookup dependency "'.$itemName.'" since it is not registered.'
);
}
2018-01-21 15:55:56 +01:00
switch ($this->store[$itemName]['lookupType']) {
2016-04-16 18:15:03 +02:00
case self::TYPE_ALIAS:
2018-01-21 15:55:56 +01:00
return $this->createAlias($itemName);
2016-04-16 18:15:03 +02:00
case self::TYPE_VALUE:
2018-01-21 15:55:56 +01:00
return $this->getValue($itemName);
2016-04-16 18:15:03 +02:00
case self::TYPE_INSTANCE:
2018-01-21 15:55:56 +01:00
return $this->createNewInstance($itemName);
2016-04-16 18:15:03 +02:00
case self::TYPE_SHARED:
2018-01-21 15:55:56 +01:00
return $this->createSharedInstance($itemName);
2016-04-16 18:15:03 +02:00
}
}
/**
* Create an array of arguments passed to the constructor of $itemName.
*
* @param string $itemName
*
* @return array
*/
public function createDependenciesFor($itemName)
{
$args = array();
2018-01-21 15:55:56 +01:00
if (isset($this->store[$itemName]['args'])) {
$args = $this->resolveArgs($this->store[$itemName]['args']);
2016-04-16 18:15:03 +02:00
}
return $args;
}
/**
* Register a new dependency with $itemName.
*
* This method returns the current DependencyContainer instance because it
* requires the use of the fluid interface to set the specific details for the
* dependency.
*
* @see asNewInstanceOf(), asSharedInstanceOf(), asValue()
*
* @param string $itemName
*
2018-01-21 15:55:56 +01:00
* @return $this
2016-04-16 18:15:03 +02:00
*/
public function register($itemName)
{
2018-01-21 15:55:56 +01:00
$this->store[$itemName] = array();
$this->endPoint = &$this->store[$itemName];
2016-04-16 18:15:03 +02:00
return $this;
}
/**
* Specify the previously registered item as a literal value.
*
* {@link register()} must be called before this will work.
*
* @param mixed $value
*
2018-01-21 15:55:56 +01:00
* @return $this
2016-04-16 18:15:03 +02:00
*/
public function asValue($value)
{
2018-01-21 15:55:56 +01:00
$endPoint = &$this->getEndPoint();
2016-04-16 18:15:03 +02:00
$endPoint['lookupType'] = self::TYPE_VALUE;
$endPoint['value'] = $value;
return $this;
}
/**
* Specify the previously registered item as an alias of another item.
*
* @param string $lookup
*
2018-01-21 15:55:56 +01:00
* @return $this
2016-04-16 18:15:03 +02:00
*/
public function asAliasOf($lookup)
{
2018-01-21 15:55:56 +01:00
$endPoint = &$this->getEndPoint();
2016-04-16 18:15:03 +02:00
$endPoint['lookupType'] = self::TYPE_ALIAS;
$endPoint['ref'] = $lookup;
return $this;
}
/**
* Specify the previously registered item as a new instance of $className.
*
* {@link register()} must be called before this will work.
* Any arguments can be set with {@link withDependencies()},
* {@link addConstructorValue()} or {@link addConstructorLookup()}.
*
* @see withDependencies(), addConstructorValue(), addConstructorLookup()
*
* @param string $className
*
2018-01-21 15:55:56 +01:00
* @return $this
2016-04-16 18:15:03 +02:00
*/
public function asNewInstanceOf($className)
{
2018-01-21 15:55:56 +01:00
$endPoint = &$this->getEndPoint();
2016-04-16 18:15:03 +02:00
$endPoint['lookupType'] = self::TYPE_INSTANCE;
$endPoint['className'] = $className;
return $this;
}
/**
* Specify the previously registered item as a shared instance of $className.
*
* {@link register()} must be called before this will work.
*
* @param string $className
*
2018-01-21 15:55:56 +01:00
* @return $this
2016-04-16 18:15:03 +02:00
*/
public function asSharedInstanceOf($className)
{
2018-01-21 15:55:56 +01:00
$endPoint = &$this->getEndPoint();
2016-04-16 18:15:03 +02:00
$endPoint['lookupType'] = self::TYPE_SHARED;
$endPoint['className'] = $className;
return $this;
}
/**
* Specify a list of injected dependencies for the previously registered item.
*
* This method takes an array of lookup names.
*
* @see addConstructorValue(), addConstructorLookup()
*
* @param array $lookups
*
2018-01-21 15:55:56 +01:00
* @return $this
2016-04-16 18:15:03 +02:00
*/
public function withDependencies(array $lookups)
{
2018-01-21 15:55:56 +01:00
$endPoint = &$this->getEndPoint();
2016-04-16 18:15:03 +02:00
$endPoint['args'] = array();
foreach ($lookups as $lookup) {
$this->addConstructorLookup($lookup);
}
return $this;
}
/**
* Specify a literal (non looked up) value for the constructor of the
* previously registered item.
*
* @see withDependencies(), addConstructorLookup()
*
* @param mixed $value
*
2018-01-21 15:55:56 +01:00
* @return $this
2016-04-16 18:15:03 +02:00
*/
public function addConstructorValue($value)
{
2018-01-21 15:55:56 +01:00
$endPoint = &$this->getEndPoint();
2016-04-16 18:15:03 +02:00
if (!isset($endPoint['args'])) {
$endPoint['args'] = array();
}
$endPoint['args'][] = array('type' => 'value', 'item' => $value);
return $this;
}
/**
* Specify a dependency lookup for the constructor of the previously
* registered item.
*
* @see withDependencies(), addConstructorValue()
*
* @param string $lookup
*
2018-01-21 15:55:56 +01:00
* @return $this
2016-04-16 18:15:03 +02:00
*/
public function addConstructorLookup($lookup)
{
2018-01-21 15:55:56 +01:00
$endPoint = &$this->getEndPoint();
if (!isset($this->endPoint['args'])) {
2016-04-16 18:15:03 +02:00
$endPoint['args'] = array();
}
$endPoint['args'][] = array('type' => 'lookup', 'item' => $lookup);
return $this;
}
/** Get the literal value with $itemName */
2018-01-21 15:55:56 +01:00
private function getValue($itemName)
2016-04-16 18:15:03 +02:00
{
2018-01-21 15:55:56 +01:00
return $this->store[$itemName]['value'];
2016-04-16 18:15:03 +02:00
}
/** Resolve an alias to another item */
2018-01-21 15:55:56 +01:00
private function createAlias($itemName)
2016-04-16 18:15:03 +02:00
{
2018-01-21 15:55:56 +01:00
return $this->lookup($this->store[$itemName]['ref']);
2016-04-16 18:15:03 +02:00
}
/** Create a fresh instance of $itemName */
2018-01-21 15:55:56 +01:00
private function createNewInstance($itemName)
2016-04-16 18:15:03 +02:00
{
2018-01-21 15:55:56 +01:00
$reflector = new ReflectionClass($this->store[$itemName]['className']);
2016-04-16 18:15:03 +02:00
if ($reflector->getConstructor()) {
return $reflector->newInstanceArgs(
$this->createDependenciesFor($itemName)
);
}
return $reflector->newInstance();
}
/** Create and register a shared instance of $itemName */
2018-01-21 15:55:56 +01:00
private function createSharedInstance($itemName)
2016-04-16 18:15:03 +02:00
{
2018-01-21 15:55:56 +01:00
if (!isset($this->store[$itemName]['instance'])) {
$this->store[$itemName]['instance'] = $this->createNewInstance($itemName);
2016-04-16 18:15:03 +02:00
}
2018-01-21 15:55:56 +01:00
return $this->store[$itemName]['instance'];
2016-04-16 18:15:03 +02:00
}
/** Get the current endpoint in the store */
2018-01-21 15:55:56 +01:00
private function &getEndPoint()
2016-04-16 18:15:03 +02:00
{
2018-01-21 15:55:56 +01:00
if (!isset($this->endPoint)) {
2016-04-16 18:15:03 +02:00
throw new BadMethodCallException(
'Component must first be registered by calling register()'
);
}
2018-01-21 15:55:56 +01:00
return $this->endPoint;
2016-04-16 18:15:03 +02:00
}
/** Get an argument list with dependencies resolved */
2018-01-21 15:55:56 +01:00
private function resolveArgs(array $args)
2016-04-16 18:15:03 +02:00
{
$resolved = array();
foreach ($args as $argDefinition) {
switch ($argDefinition['type']) {
case 'lookup':
2018-01-21 15:55:56 +01:00
$resolved[] = $this->lookupRecursive($argDefinition['item']);
2016-04-16 18:15:03 +02:00
break;
case 'value':
$resolved[] = $argDefinition['item'];
break;
}
}
return $resolved;
}
/** Resolve a single dependency with an collections */
2018-01-21 15:55:56 +01:00
private function lookupRecursive($item)
2016-04-16 18:15:03 +02:00
{
if (is_array($item)) {
$collection = array();
foreach ($item as $k => $v) {
2018-01-21 15:55:56 +01:00
$collection[$k] = $this->lookupRecursive($v);
2016-04-16 18:15:03 +02:00
}
return $collection;
}
return $this->lookup($item);
}
}