use zend router
[GitHub/Stricted/Domain-Control-Panel.git] / vendor / Zend / ServiceManager / Di / DiServiceFactory.php
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
4 *
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
9
10 namespace Zend\ServiceManager\Di;
11
12 use Zend\Di\Di;
13 use Zend\Di\Exception\ClassNotFoundException as DiClassNotFoundException;
14 use Zend\ServiceManager\Exception;
15 use Zend\ServiceManager\FactoryInterface;
16 use Zend\ServiceManager\ServiceLocatorInterface;
17
18 class DiServiceFactory extends Di implements FactoryInterface
19 {
20 /**@#+
21 * constants
22 */
23 const USE_SL_BEFORE_DI = 'before';
24 const USE_SL_AFTER_DI = 'after';
25 const USE_SL_NONE = 'none';
26 /**@#-*/
27
28 /**
29 * @var \Zend\Di\Di
30 */
31 protected $di = null;
32
33 /**
34 * @var \Zend\Di\InstanceManager
35 */
36 protected $name = null;
37
38 /**
39 * @var array
40 */
41 protected $parameters = [];
42
43 /**
44 * @var string
45 */
46 protected $useServiceLocator = self::USE_SL_AFTER_DI;
47
48 /**
49 * @var ServiceLocatorInterface
50 */
51 protected $serviceLocator = null;
52
53 /**
54 * Constructor
55 *
56 * @param \Zend\Di\Di $di
57 * @param null|\Zend\Di\InstanceManager $name
58 * @param array $parameters
59 * @param string $useServiceLocator
60 */
61 public function __construct(Di $di, $name, array $parameters = [], $useServiceLocator = self::USE_SL_NONE)
62 {
63 $this->di = $di;
64 $this->name = $name;
65 $this->parameters = $parameters;
66 if (in_array($useServiceLocator, [self::USE_SL_BEFORE_DI, self::USE_SL_AFTER_DI, self::USE_SL_NONE])) {
67 $this->useServiceLocator = $useServiceLocator;
68 }
69
70 // since we are using this in a proxy-fashion, localize state
71 $this->definitions = $this->di->definitions;
72 $this->instanceManager = $this->di->instanceManager;
73 }
74
75 /**
76 * Create service
77 *
78 * @param ServiceLocatorInterface $serviceLocator
79 * @return object
80 */
81 public function createService(ServiceLocatorInterface $serviceLocator)
82 {
83 $this->serviceLocator = $serviceLocator;
84 return $this->get($this->name, $this->parameters);
85 }
86
87 /**
88 * Override, as we want it to use the functionality defined in the proxy
89 *
90 * @param string $name
91 * @param array $params
92 * @return object
93 * @throws Exception\ServiceNotFoundException
94 */
95 public function get($name, array $params = [])
96 {
97 // allow this di service to get dependencies from the service locator BEFORE trying di
98 if ($this->useServiceLocator == self::USE_SL_BEFORE_DI && $this->serviceLocator->has($name)) {
99 return $this->serviceLocator->get($name);
100 }
101
102 try {
103 $service = parent::get($name, $params);
104 return $service;
105 } catch (DiClassNotFoundException $e) {
106 // allow this di service to get dependencies from the service locator AFTER trying di
107 if ($this->useServiceLocator == self::USE_SL_AFTER_DI && $this->serviceLocator->has($name)) {
108 return $this->serviceLocator->get($name);
109 } else {
110 throw new Exception\ServiceNotFoundException(
111 sprintf('Service %s was not found in this DI instance', $name),
112 null,
113 $e
114 );
115 }
116 }
117 }
118 }