1 | <?php |
||
15 | class ServiceProvider extends Di |
||
16 | { |
||
17 | |||
18 | /** The default mode. Retrieve services from an explicit list. */ |
||
19 | const PROVISIONING_MODE_SERVICE_LIST = 1; |
||
20 | /** The mode for retrieving services from a list of namespaces. */ |
||
21 | const PROVISIONING_MODE_NAMESPACES = 2; |
||
22 | /** The mode for retrieving services from a folder recursively. */ |
||
23 | const PROVISIONING_MODE_FOLDERS = 3; |
||
24 | |||
25 | /** The Di key for storing the list of registered namespaces */ |
||
26 | const KEY_NAMESPACES = 'serviceNamespaces'; |
||
27 | /** The Di key for storing the list of folders to scan for controllers */ |
||
28 | const KEY_FOLDERS = 'serviceFolders'; |
||
29 | |||
30 | // the private provisioning mode |
||
31 | private $provisioningMode = self::PROVISIONING_MODE_SERVICE_LIST; |
||
32 | |||
33 | // a cache of service instances |
||
34 | private $instanceCache = array(); |
||
35 | |||
36 | /** |
||
37 | * Returns the array of all services. |
||
38 | * @return The array of all services. |
||
39 | */ |
||
40 | 1 | public function getServices() |
|
44 | |||
45 | /** |
||
46 | * Returns the specified service path for the given key. |
||
47 | * @param string $key The key to lookup. |
||
48 | * @return Returns the path to the specified service for the given key. |
||
49 | * @throws ServiceNotFoundForKeyException Throws this exception if the key isn't associated |
||
50 | * with any registered service. |
||
51 | */ |
||
52 | 1 | public function getService($key) |
|
56 | |||
57 | /** |
||
58 | * Specifies the mapping between the given key and service. |
||
59 | * @param string $key The key to assign. |
||
60 | * @param mixed $service The service to be assigned to the key. |
||
61 | * @return Returns $this. |
||
62 | */ |
||
63 | 1 | public function setService($key, $service) |
|
69 | |||
70 | /** |
||
71 | * Returns an instance of the specified service. |
||
72 | * @param string $key The key to lookup. |
||
73 | * @param boolean $useCache An optional flag indicating whether we should |
||
74 | * use the cache. True by default. |
||
75 | * @return AbstractController Returns an instance of the specified service. |
||
76 | * @throws ServiceNotFoundForKeyException Throws this exception if the key isn't associated |
||
77 | * with any registered service. |
||
78 | */ |
||
79 | 24 | public function getServiceInstance($key, $useCache = true) |
|
80 | { |
||
81 | // retrieve the service from the instance cache if it exists |
||
82 | 24 | if ($useCache && isset($this->instanceCache[$key])) { |
|
83 | 3 | return $this->instanceCache[$key]; |
|
84 | } |
||
85 | |||
86 | // retrieve the given controller from the key using the proper |
||
87 | // provisioning mode |
||
88 | 24 | switch ($this->provisioningMode) { |
|
89 | 24 | case self::PROVISIONING_MODE_NAMESPACES: |
|
90 | 3 | $this->instanceCache[$key] = $this->getServiceFromNamespaces($key); |
|
91 | 2 | break; |
|
92 | 21 | case self::PROVISIONING_MODE_FOLDERS: |
|
93 | 3 | $this->instanceCache[$key] = $this->getServiceFromFolder($key); |
|
94 | 2 | break; |
|
95 | default: |
||
96 | 18 | $this->instanceCache[$key] = $this->getServiceFromServiceList($key); |
|
97 | } |
||
98 | 20 | return $this->instanceCache[$key]; |
|
99 | } |
||
100 | |||
101 | /** |
||
102 | * Sets the list of namespaces and switches to namespace provisioning mode. |
||
103 | * @param array $namespaces An array of namespaces. |
||
104 | * @return ServiceProvider Returns $this. |
||
105 | */ |
||
106 | 3 | public function setNamespaces($namespaces) |
|
112 | |||
113 | /** |
||
114 | * Sets the list of folders and switches to folder provisioning mode. |
||
115 | * @param array $folders An array of folders. |
||
116 | * @return ServiceProvider Returns $this. |
||
117 | */ |
||
118 | 3 | public function setFolders($folders) |
|
124 | |||
125 | /** |
||
126 | * Returns an instance of the specified controller from the list of |
||
127 | * namespaces. |
||
128 | */ |
||
129 | 3 | private function getServiceFromNamespaces($controllerClass) |
|
139 | |||
140 | /** |
||
141 | * Returns an instance of the specified controller from the list of |
||
142 | * namespaces. |
||
143 | * @param string $controllerClass The controller class file we are looking for. |
||
144 | * @return AbstractController Returns an instance of the controller. |
||
145 | */ |
||
146 | 3 | private function getServiceFromFolder($controllerClass) |
|
157 | |||
158 | /** |
||
159 | * Returns an instance of the specified controller using the existing the |
||
160 | * explicitly specified services list. |
||
161 | * @param string $controllerClass The controller class we are resolving. |
||
162 | * @return AbstractController Returns an instance of the controller. |
||
163 | */ |
||
164 | 18 | private function getServiceFromServiceList($controllerClass) |
|
181 | |||
182 | /** |
||
183 | * Scan for the specific file recursively. |
||
184 | * @param string $file The file to search for. |
||
185 | * @param string $folder The folder to search inside. |
||
186 | * @return mixed Returns either the full path string to the file or false |
||
187 | * if the file was not found. |
||
188 | */ |
||
189 | 3 | private function findFileInFolderRecursively($file, $folder) |
|
208 | } |
||
209 |