forked from Wavyzz/dolibarr
swiftmailer
This commit is contained in:
@@ -11,7 +11,7 @@
|
||||
/**
|
||||
* Dependency Injection container.
|
||||
*
|
||||
* @author Chris Corbyn
|
||||
* @author Chris Corbyn
|
||||
*/
|
||||
class Swift_DependencyContainer
|
||||
{
|
||||
@@ -28,13 +28,13 @@ class Swift_DependencyContainer
|
||||
const TYPE_ALIAS = 0x1000;
|
||||
|
||||
/** Singleton instance */
|
||||
private static $_instance = null;
|
||||
private static $instance = null;
|
||||
|
||||
/** The data container */
|
||||
private $_store = array();
|
||||
private $store = array();
|
||||
|
||||
/** The current endpoint in the data container */
|
||||
private $_endPoint;
|
||||
private $endPoint;
|
||||
|
||||
/**
|
||||
* Constructor should not be used.
|
||||
@@ -48,15 +48,15 @@ class Swift_DependencyContainer
|
||||
/**
|
||||
* Returns a singleton of the DependencyContainer.
|
||||
*
|
||||
* @return Swift_DependencyContainer
|
||||
* @return self
|
||||
*/
|
||||
public static function getInstance()
|
||||
{
|
||||
if (!isset(self::$_instance)) {
|
||||
self::$_instance = new self();
|
||||
if (!isset(self::$instance)) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
|
||||
return self::$_instance;
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -66,7 +66,7 @@ class Swift_DependencyContainer
|
||||
*/
|
||||
public function listItems()
|
||||
{
|
||||
return array_keys($this->_store);
|
||||
return array_keys($this->store);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -80,8 +80,8 @@ class Swift_DependencyContainer
|
||||
*/
|
||||
public function has($itemName)
|
||||
{
|
||||
return array_key_exists($itemName, $this->_store)
|
||||
&& isset($this->_store[$itemName]['lookupType']);
|
||||
return array_key_exists($itemName, $this->store)
|
||||
&& isset($this->store[$itemName]['lookupType']);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -91,9 +91,9 @@ class Swift_DependencyContainer
|
||||
*
|
||||
* @param string $itemName
|
||||
*
|
||||
* @throws Swift_DependencyException If the dependency is not found
|
||||
*
|
||||
* @return mixed
|
||||
*
|
||||
* @throws Swift_DependencyException If the dependency is not found
|
||||
*/
|
||||
public function lookup($itemName)
|
||||
{
|
||||
@@ -103,15 +103,15 @@ class Swift_DependencyContainer
|
||||
);
|
||||
}
|
||||
|
||||
switch ($this->_store[$itemName]['lookupType']) {
|
||||
switch ($this->store[$itemName]['lookupType']) {
|
||||
case self::TYPE_ALIAS:
|
||||
return $this->_createAlias($itemName);
|
||||
return $this->createAlias($itemName);
|
||||
case self::TYPE_VALUE:
|
||||
return $this->_getValue($itemName);
|
||||
return $this->getValue($itemName);
|
||||
case self::TYPE_INSTANCE:
|
||||
return $this->_createNewInstance($itemName);
|
||||
return $this->createNewInstance($itemName);
|
||||
case self::TYPE_SHARED:
|
||||
return $this->_createSharedInstance($itemName);
|
||||
return $this->createSharedInstance($itemName);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -125,8 +125,8 @@ class Swift_DependencyContainer
|
||||
public function createDependenciesFor($itemName)
|
||||
{
|
||||
$args = array();
|
||||
if (isset($this->_store[$itemName]['args'])) {
|
||||
$args = $this->_resolveArgs($this->_store[$itemName]['args']);
|
||||
if (isset($this->store[$itemName]['args'])) {
|
||||
$args = $this->resolveArgs($this->store[$itemName]['args']);
|
||||
}
|
||||
|
||||
return $args;
|
||||
@@ -143,12 +143,12 @@ class Swift_DependencyContainer
|
||||
*
|
||||
* @param string $itemName
|
||||
*
|
||||
* @return Swift_DependencyContainer
|
||||
* @return $this
|
||||
*/
|
||||
public function register($itemName)
|
||||
{
|
||||
$this->_store[$itemName] = array();
|
||||
$this->_endPoint = &$this->_store[$itemName];
|
||||
$this->store[$itemName] = array();
|
||||
$this->endPoint = &$this->store[$itemName];
|
||||
|
||||
return $this;
|
||||
}
|
||||
@@ -160,11 +160,11 @@ class Swift_DependencyContainer
|
||||
*
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return Swift_DependencyContainer
|
||||
* @return $this
|
||||
*/
|
||||
public function asValue($value)
|
||||
{
|
||||
$endPoint = &$this->_getEndPoint();
|
||||
$endPoint = &$this->getEndPoint();
|
||||
$endPoint['lookupType'] = self::TYPE_VALUE;
|
||||
$endPoint['value'] = $value;
|
||||
|
||||
@@ -176,11 +176,11 @@ class Swift_DependencyContainer
|
||||
*
|
||||
* @param string $lookup
|
||||
*
|
||||
* @return Swift_DependencyContainer
|
||||
* @return $this
|
||||
*/
|
||||
public function asAliasOf($lookup)
|
||||
{
|
||||
$endPoint = &$this->_getEndPoint();
|
||||
$endPoint = &$this->getEndPoint();
|
||||
$endPoint['lookupType'] = self::TYPE_ALIAS;
|
||||
$endPoint['ref'] = $lookup;
|
||||
|
||||
@@ -198,11 +198,11 @@ class Swift_DependencyContainer
|
||||
*
|
||||
* @param string $className
|
||||
*
|
||||
* @return Swift_DependencyContainer
|
||||
* @return $this
|
||||
*/
|
||||
public function asNewInstanceOf($className)
|
||||
{
|
||||
$endPoint = &$this->_getEndPoint();
|
||||
$endPoint = &$this->getEndPoint();
|
||||
$endPoint['lookupType'] = self::TYPE_INSTANCE;
|
||||
$endPoint['className'] = $className;
|
||||
|
||||
@@ -216,11 +216,11 @@ class Swift_DependencyContainer
|
||||
*
|
||||
* @param string $className
|
||||
*
|
||||
* @return Swift_DependencyContainer
|
||||
* @return $this
|
||||
*/
|
||||
public function asSharedInstanceOf($className)
|
||||
{
|
||||
$endPoint = &$this->_getEndPoint();
|
||||
$endPoint = &$this->getEndPoint();
|
||||
$endPoint['lookupType'] = self::TYPE_SHARED;
|
||||
$endPoint['className'] = $className;
|
||||
|
||||
@@ -236,11 +236,11 @@ class Swift_DependencyContainer
|
||||
*
|
||||
* @param array $lookups
|
||||
*
|
||||
* @return Swift_DependencyContainer
|
||||
* @return $this
|
||||
*/
|
||||
public function withDependencies(array $lookups)
|
||||
{
|
||||
$endPoint = &$this->_getEndPoint();
|
||||
$endPoint = &$this->getEndPoint();
|
||||
$endPoint['args'] = array();
|
||||
foreach ($lookups as $lookup) {
|
||||
$this->addConstructorLookup($lookup);
|
||||
@@ -257,11 +257,11 @@ class Swift_DependencyContainer
|
||||
*
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return Swift_DependencyContainer
|
||||
* @return $this
|
||||
*/
|
||||
public function addConstructorValue($value)
|
||||
{
|
||||
$endPoint = &$this->_getEndPoint();
|
||||
$endPoint = &$this->getEndPoint();
|
||||
if (!isset($endPoint['args'])) {
|
||||
$endPoint['args'] = array();
|
||||
}
|
||||
@@ -278,12 +278,12 @@ class Swift_DependencyContainer
|
||||
*
|
||||
* @param string $lookup
|
||||
*
|
||||
* @return Swift_DependencyContainer
|
||||
* @return $this
|
||||
*/
|
||||
public function addConstructorLookup($lookup)
|
||||
{
|
||||
$endPoint = &$this->_getEndPoint();
|
||||
if (!isset($this->_endPoint['args'])) {
|
||||
$endPoint = &$this->getEndPoint();
|
||||
if (!isset($this->endPoint['args'])) {
|
||||
$endPoint['args'] = array();
|
||||
}
|
||||
$endPoint['args'][] = array('type' => 'lookup', 'item' => $lookup);
|
||||
@@ -292,21 +292,21 @@ class Swift_DependencyContainer
|
||||
}
|
||||
|
||||
/** Get the literal value with $itemName */
|
||||
private function _getValue($itemName)
|
||||
private function getValue($itemName)
|
||||
{
|
||||
return $this->_store[$itemName]['value'];
|
||||
return $this->store[$itemName]['value'];
|
||||
}
|
||||
|
||||
/** Resolve an alias to another item */
|
||||
private function _createAlias($itemName)
|
||||
private function createAlias($itemName)
|
||||
{
|
||||
return $this->lookup($this->_store[$itemName]['ref']);
|
||||
return $this->lookup($this->store[$itemName]['ref']);
|
||||
}
|
||||
|
||||
/** Create a fresh instance of $itemName */
|
||||
private function _createNewInstance($itemName)
|
||||
private function createNewInstance($itemName)
|
||||
{
|
||||
$reflector = new ReflectionClass($this->_store[$itemName]['className']);
|
||||
$reflector = new ReflectionClass($this->store[$itemName]['className']);
|
||||
if ($reflector->getConstructor()) {
|
||||
return $reflector->newInstanceArgs(
|
||||
$this->createDependenciesFor($itemName)
|
||||
@@ -317,35 +317,35 @@ class Swift_DependencyContainer
|
||||
}
|
||||
|
||||
/** Create and register a shared instance of $itemName */
|
||||
private function _createSharedInstance($itemName)
|
||||
private function createSharedInstance($itemName)
|
||||
{
|
||||
if (!isset($this->_store[$itemName]['instance'])) {
|
||||
$this->_store[$itemName]['instance'] = $this->_createNewInstance($itemName);
|
||||
if (!isset($this->store[$itemName]['instance'])) {
|
||||
$this->store[$itemName]['instance'] = $this->createNewInstance($itemName);
|
||||
}
|
||||
|
||||
return $this->_store[$itemName]['instance'];
|
||||
return $this->store[$itemName]['instance'];
|
||||
}
|
||||
|
||||
/** Get the current endpoint in the store */
|
||||
private function &_getEndPoint()
|
||||
private function &getEndPoint()
|
||||
{
|
||||
if (!isset($this->_endPoint)) {
|
||||
if (!isset($this->endPoint)) {
|
||||
throw new BadMethodCallException(
|
||||
'Component must first be registered by calling register()'
|
||||
);
|
||||
}
|
||||
|
||||
return $this->_endPoint;
|
||||
return $this->endPoint;
|
||||
}
|
||||
|
||||
/** Get an argument list with dependencies resolved */
|
||||
private function _resolveArgs(array $args)
|
||||
private function resolveArgs(array $args)
|
||||
{
|
||||
$resolved = array();
|
||||
foreach ($args as $argDefinition) {
|
||||
switch ($argDefinition['type']) {
|
||||
case 'lookup':
|
||||
$resolved[] = $this->_lookupRecursive($argDefinition['item']);
|
||||
$resolved[] = $this->lookupRecursive($argDefinition['item']);
|
||||
break;
|
||||
case 'value':
|
||||
$resolved[] = $argDefinition['item'];
|
||||
@@ -357,12 +357,12 @@ class Swift_DependencyContainer
|
||||
}
|
||||
|
||||
/** Resolve a single dependency with an collections */
|
||||
private function _lookupRecursive($item)
|
||||
private function lookupRecursive($item)
|
||||
{
|
||||
if (is_array($item)) {
|
||||
$collection = array();
|
||||
foreach ($item as $k => $v) {
|
||||
$collection[$k] = $this->_lookupRecursive($v);
|
||||
$collection[$k] = $this->lookupRecursive($v);
|
||||
}
|
||||
|
||||
return $collection;
|
||||
|
||||
Reference in New Issue
Block a user