Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Mvc / Service / DiStrictAbstractServiceFactory.php
blob1d043be806c2023c98504b36ce276fcab707add8
1 <?php
2 /**
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
8 */
10 namespace Zend\Mvc\Service;
12 use Zend\Di\Di;
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
22 /**@#+
23 * constants
25 const USE_SL_BEFORE_DI = 'before';
26 const USE_SL_AFTER_DI = 'after';
27 const USE_SL_NONE = 'none';
28 /**@#-*/
30 /**
31 * @var Di
33 protected $di = null;
35 /**
36 * @var string
38 protected $useServiceLocator = self::USE_SL_AFTER_DI;
40 /**
41 * @var ServiceLocatorInterface
43 protected $serviceLocator = null;
45 /**
46 * @var array an array of whitelisted service names (keys are the service names)
48 protected $allowedServiceNames = array();
50 /**
51 * @param Di $di
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
58 $this->di = $di;
59 $this->definitions = $this->di->definitions;
60 $this->instanceManager = $this->di->instanceManager;
63 /**
64 * @param array $allowedServiceNames
66 public function setAllowedServiceNames(array $allowedServiceNames)
68 $this->allowedServiceNames = array_flip(array_values($allowedServiceNames));
71 /**
72 * @return array
74 public function getAllowedServiceNames()
76 return array_keys($this->allowedServiceNames);
79 /**
80 * {@inheritDoc}
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();
94 } else {
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
104 * {@inheritDoc}
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);
118 try {
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),
127 null,
134 * {@inheritDoc}
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]);