This repository has been archived by the owner on Feb 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'ralphschindler/feature/service-manager-…
…component'
- Loading branch information
Showing
27 changed files
with
1,528 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace Zend\ServiceManager; | ||
|
||
interface AbstractFactoryInterface | ||
{ | ||
public function canCreateServiceWithName($name /*, $requestedName */); | ||
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name /*, $requestedName */); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
namespace Zend\ServiceManager; | ||
|
||
class Configuration implements ConfigurationInterface | ||
{ | ||
protected $configuration = array(); | ||
|
||
public function __construct($configuration = array()) | ||
{ | ||
$this->configuration = $configuration; | ||
} | ||
|
||
public function getFactories() | ||
{ | ||
return (isset($this->configuration['factories'])) ? $this->configuration['factories'] : array(); | ||
} | ||
|
||
public function getAbstractFactories() | ||
{ | ||
return (isset($this->configuration['abstract_factories'])) ? $this->configuration['abstract_factories'] : array(); | ||
} | ||
|
||
public function getInvokables() | ||
{ | ||
return (isset($this->configuration['invokables'])) ? $this->configuration['invokables'] : array(); | ||
} | ||
|
||
public function getServices() | ||
{ | ||
return (isset($this->configuration['services'])) ? $this->configuration['services'] : array(); | ||
} | ||
|
||
public function getAliases() | ||
{ | ||
return (isset($this->configuration['aliases'])) ? $this->configuration['aliases'] : array(); | ||
} | ||
|
||
public function getShared() | ||
{ | ||
return (isset($this->configuration['shared'])) ? $this->configuration['shared'] : array(); | ||
} | ||
|
||
public function configureServiceManager(ServiceManager $serviceManager) | ||
{ | ||
foreach ($this->getFactories() as $name => $factory) { | ||
$serviceManager->setFactory($name, $factory); | ||
} | ||
|
||
foreach ($this->getAbstractFactories() as $factory) { | ||
$serviceManager->addAbstractFactory($factory); | ||
} | ||
|
||
foreach ($this->getInvokables() as $name => $invokable) { | ||
$serviceManager->setInvokable($name, $invokable); | ||
} | ||
|
||
foreach ($this->getServices() as $name => $service) { | ||
$serviceManager->setService($name, $service); | ||
} | ||
|
||
foreach ($this->getAliases() as $alias => $nameOrAlias) { | ||
$serviceManager->setAlias($alias, $nameOrAlias); | ||
} | ||
|
||
foreach ($this->getShared() as $name => $isShared) { | ||
$serviceManager->setShared($name, $isShared); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Zend\ServiceManager; | ||
|
||
interface ConfigurationInterface | ||
{ | ||
public function configureServiceManager(ServiceManager $serviceManager); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace Zend\ServiceManager\Di; | ||
|
||
use Zend\ServiceManager\AbstractFactoryInterface, | ||
Zend\ServiceManager\ServiceLocatorInterface, | ||
Zend\Di\Di; | ||
|
||
class DiAbstractServiceFactory extends DiServiceFactory implements AbstractFactoryInterface | ||
{ | ||
|
||
/** | ||
* @param \Zend\Di\Di $di | ||
* @param null|string|\Zend\Di\InstanceManager $useServiceLocator | ||
*/ | ||
public function __construct(Di $di, $useServiceLocator = self::USE_SL_NONE) | ||
{ | ||
$this->di = $di; | ||
if (in_array($useServiceLocator, array(self::USE_SL_BEFORE_DI, self::USE_SL_AFTER_DI, self::USE_SL_NONE))) { | ||
$this->useServiceManager = $useServiceLocator; | ||
} | ||
|
||
// since we are using this in a proxy-fashion, localize state | ||
$this->definitions = $this->di->definitions; | ||
$this->instanceManager = $this->di->instanceManager; | ||
} | ||
|
||
/** | ||
* @param ServiceLocatorInterface $serviceLocator | ||
* @param $serviceName | ||
* @param null $requestedName | ||
* @return object | ||
*/ | ||
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $serviceName, $requestedName = null) | ||
{ | ||
$this->serviceLocator = $serviceLocator; | ||
if ($requestedName) { | ||
return $this->get($requestedName, array(), true); | ||
} else { | ||
return $this->get($serviceName, array(), true); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* @param $name | ||
* @return null | ||
*/ | ||
public function canCreateServiceWithName($name) | ||
{ | ||
return null; // not sure | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace Zend\ServiceManager\Di; | ||
|
||
use Zend\Di\InstanceManager as DiInstanceManager, | ||
Zend\ServiceManager\ServiceLocatorInterface; | ||
|
||
class DiInstanceManagerProxy extends DiInstanceManager | ||
{ | ||
/** | ||
* @var DiInstanceManager | ||
*/ | ||
protected $diInstanceManager = null; | ||
|
||
/** | ||
* @var ServiceLocatorInterface | ||
*/ | ||
protected $serviceLocator = null; | ||
|
||
/** | ||
* @param DiInstanceManager $diInstanceManager | ||
* @param ServiceLocatorInterface $serviceLocator | ||
*/ | ||
public function __construct(DiInstanceManager $diInstanceManager, ServiceLocatorInterface $serviceLocator) | ||
{ | ||
$this->diInstanceManager = $diInstanceManager; | ||
$this->serviceLocator = $serviceLocator; | ||
} | ||
|
||
/** | ||
* @param $classOrAlias | ||
* @return bool | ||
*/ | ||
public function hasSharedInstance($classOrAlias) | ||
{ | ||
return ($this->serviceLocator->has($classOrAlias) || $this->diInstanceManager->hasSharedInstance($classOrAlias)); | ||
} | ||
|
||
/** | ||
* @param $classOrAlias | ||
* @return mixed | ||
*/ | ||
public function getSharedInstance($classOrAlias) | ||
{ | ||
if ($this->serviceLocator->has($classOrAlias)) { | ||
return $this->serviceLocator->get($classOrAlias); | ||
} else { | ||
return $this->diInstanceManager->getSharedInstance($classOrAlias); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
|
||
namespace Zend\ServiceManager\Di; | ||
|
||
use Zend\ServiceManager\FactoryInterface, | ||
Zend\ServiceManager\ServiceLocatorInterface, | ||
Zend\ServiceManager\Exception, | ||
Zend\Di\Di, | ||
Zend\Di\Exception\ClassNotFoundException as DiClassNotFoundException; | ||
|
||
class DiServiceFactory extends Di implements FactoryInterface | ||
{ | ||
/**@#+ | ||
* constants | ||
*/ | ||
const USE_SL_BEFORE_DI = 'before'; | ||
const USE_SL_AFTER_DI = 'after'; | ||
const USE_SL_NONE = 'none'; | ||
/**@#-*/ | ||
|
||
/** | ||
* @var \Zend\Di\Di | ||
*/ | ||
protected $di = null; | ||
|
||
/** | ||
* @var \Zend\Di\InstanceManager | ||
*/ | ||
protected $name = null; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $parameters = array(); | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $useServiceLocator = self::USE_SL_AFTER_DI; | ||
|
||
/** | ||
* @var ServiceLocatorInterface | ||
*/ | ||
protected $serviceLocator = null; | ||
|
||
/** | ||
* @param \Zend\Di\Di $di | ||
* @param null|\Zend\Di\InstanceManager $name | ||
* @param array $parameters | ||
* @param string $useServiceLocator | ||
*/ | ||
public function __construct(Di $di, $name, array $parameters = array(), $useServiceLocator = self::USE_SL_NONE) | ||
{ | ||
$this->di = $di; | ||
$this->name = $name; | ||
$this->parameters = $parameters; | ||
if (in_array($useServiceLocator, array(self::USE_SL_BEFORE_DI, self::USE_SL_AFTER_DI, self::USE_SL_NONE))) { | ||
$this->useServiceLocator = $useServiceLocator; | ||
} | ||
|
||
// since we are using this in a proxy-fashion, localize state | ||
$this->definitions = $this->di->definitions; | ||
$this->instanceManager = $this->di->instanceManager; | ||
} | ||
|
||
/** | ||
* @param ServiceLocatorInterface $serviceLocator | ||
* @return object | ||
*/ | ||
public function createService(ServiceLocatorInterface $serviceLocator) | ||
{ | ||
$this->serviceLocator = $serviceLocator; | ||
return $this->get($this->name, $this->parameters, true); | ||
} | ||
|
||
/** | ||
* Override, as we want it to use the functionality defined in the proxy | ||
* | ||
* @param string $name | ||
* @param array $params | ||
* @return object | ||
* @throws Exception\InvalidServiceNameException | ||
*/ | ||
public function get($name, array $params = array()) | ||
{ | ||
// allow this di service to get dependencies from the service locator BEFORE trying di | ||
if ($this->useServiceLocator == self::USE_SL_BEFORE_DI && $this->serviceLocator->has($name)) { | ||
return $this->serviceLocator->get($name); | ||
} | ||
|
||
try { | ||
|
||
$service = parent::get($name, $params); | ||
return $service; | ||
|
||
} catch (DiClassNotFoundException $e) { | ||
|
||
// allow this di service to get dependencies from the service locator AFTER trying di | ||
if ($this->useServiceLocator == self::USE_SL_AFTER_DI && $this->serviceLocator->has($name)) { | ||
return $this->serviceLocator->get($name); | ||
} else { | ||
throw new Exception\InvalidServiceNameException( | ||
sprintf('Service %s was not found in this DI instance', $name), | ||
null, | ||
$e | ||
); | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace Zend\ServiceManager\Di; | ||
|
||
use Zend\ServiceManager\InitializerInterface, | ||
Zend\ServiceManager\ServiceLocatorInterface, | ||
Zend\ServiceManager\Exception, | ||
Zend\Di\Di, | ||
Zend\Di\Exception\ClassNotFoundException as DiClassNotFoundException; | ||
|
||
class DiServiceInitializer extends Di implements InitializerInterface | ||
{ | ||
/** | ||
* @var Di | ||
*/ | ||
protected $di = null; | ||
|
||
/** | ||
* @var DiInstanceManagerProxy | ||
*/ | ||
protected $diInstanceManagerProxy = null; | ||
|
||
/** | ||
* @var ServiceLocatorInterface | ||
*/ | ||
protected $serviceLocator = null; | ||
|
||
/** | ||
* @param \Zend\Di\Di $di | ||
* @param \Zend\ServiceManager\ServiceLocatorInterface $serviceLocator | ||
* @param null|DiInstanceManagerProxy $diImProxy | ||
*/ | ||
public function __construct(Di $di, ServiceLocatorInterface $serviceLocator, DiInstanceManagerProxy $diImProxy = null) | ||
{ | ||
$this->di = $di; | ||
$this->serviceLocator = $serviceLocator; | ||
$this->diInstanceManagerProxy = ($diImProxy) ?: new DiInstanceManagerProxy($di->instanceManager(), $serviceLocator); | ||
} | ||
|
||
/** | ||
* @param $instance | ||
*/ | ||
public function initialize($instance) | ||
{ | ||
$instanceManager = $this->di->instanceManager; | ||
$this->di->instanceManager = $this->diInstanceManagerProxy; | ||
try { | ||
$this->di->injectDependencies($instance); | ||
$this->di->instanceManager = $instanceManager; | ||
} catch (\Exception $e) { | ||
$this->di->instanceManager = $instanceManager; | ||
throw $e; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace Zend\ServiceManager; | ||
|
||
interface Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace Zend\ServiceManager\Exception; | ||
|
||
use Zend\ServiceManager\Exception; | ||
|
||
class CircularDependencyFoundException extends \RuntimeException implements Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace Zend\ServiceManager\Exception; | ||
|
||
use Zend\ServiceManager\Exception; | ||
|
||
class InvalidArgumentException extends \InvalidArgumentException implements Exception | ||
{ | ||
} |
Oops, something went wrong.