3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Mvc\Service
;
13 use Zend\Di\Exception\ClassNotFoundException
;
14 use Zend\Mvc\Exception\DomainException
;
15 use Zend\ServiceManager\AbstractFactoryInterface
;
16 use Zend\ServiceManager\AbstractPluginManager
;
17 use Zend\ServiceManager\Exception
;
18 use Zend\ServiceManager\ServiceLocatorInterface
;
20 class DiStrictAbstractServiceFactory
extends Di
implements AbstractFactoryInterface
25 const USE_SL_BEFORE_DI
= 'before';
26 const USE_SL_AFTER_DI
= 'after';
27 const USE_SL_NONE
= 'none';
38 protected $useServiceLocator = self
::USE_SL_AFTER_DI
;
41 * @var ServiceLocatorInterface
43 protected $serviceLocator = null;
46 * @var array an array of whitelisted service names (keys are the service names)
48 protected $allowedServiceNames = array();
52 * @param string $useServiceLocator
54 public function __construct(Di
$di, $useServiceLocator = self
::USE_SL_NONE
)
56 $this->useServiceLocator
= $useServiceLocator;
57 // since we are using this in a proxy-fashion, localize state
59 $this->definitions
= $this->di
->definitions
;
60 $this->instanceManager
= $this->di
->instanceManager
;
64 * @param array $allowedServiceNames
66 public function setAllowedServiceNames(array $allowedServiceNames)
68 $this->allowedServiceNames
= array_flip(array_values($allowedServiceNames));
74 public function getAllowedServiceNames()
76 return array_keys($this->allowedServiceNames
);
82 * Allows creation of services only when in a whitelist
84 public function createServiceWithName(ServiceLocatorInterface
$serviceLocator, $serviceName, $requestedName)
86 if (!isset($this->allowedServiceNames
[$requestedName])) {
87 throw new Exception\
InvalidServiceNameException('Service "' . $requestedName . '" is not whitelisted');
91 if ($serviceLocator instanceof AbstractPluginManager
) {
92 /* @var $serviceLocator AbstractPluginManager */
93 $this->serviceLocator
= $serviceLocator->getServiceLocator();
95 $this->serviceLocator
= $serviceLocator;
98 return parent
::get($requestedName);
102 * Overrides Zend\Di to allow the given serviceLocator's services to be reused by Di itself
106 * @throws Exception\InvalidServiceNameException
108 public function get($name, array $params = array())
110 if (null === $this->serviceLocator
) {
111 throw new DomainException('No ServiceLocator defined, use `createServiceWithName` instead of `get`');
114 if (self
::USE_SL_BEFORE_DI
=== $this->useServiceLocator
&& $this->serviceLocator
->has($name)) {
115 return $this->serviceLocator
->get($name);
119 return parent
::get($name, $params);
120 } catch (ClassNotFoundException
$e) {
121 if (self
::USE_SL_AFTER_DI
=== $this->useServiceLocator
&& $this->serviceLocator
->has($name)) {
122 return $this->serviceLocator
->get($name);
125 throw new Exception\
ServiceNotFoundException(
126 sprintf('Service %s was not found in this DI instance', $name),
136 * Allows creation of services only when in a whitelist
138 public function canCreateServiceWithName(ServiceLocatorInterface
$serviceLocator, $name, $requestedName)
140 // won't check if the service exists, we are trusting the user's whitelist
141 return isset($this->allowedServiceNames
[$requestedName]);