|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_Tool |
|
17 * @subpackage Framework |
|
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
19 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
20 * @version $Id: Repository.php 23202 2010-10-21 15:08:15Z ralph $ |
|
21 */ |
|
22 |
|
23 /** |
|
24 * @see Zend_Tool_Framework_Provider_Signature |
|
25 */ |
|
26 require_once 'Zend/Tool/Framework/Provider/Signature.php'; |
|
27 |
|
28 /** |
|
29 * @see Zend_Tool_Framework_Registry_EnabledInterface |
|
30 */ |
|
31 require_once 'Zend/Tool/Framework/Registry/EnabledInterface.php'; |
|
32 |
|
33 /** |
|
34 * @category Zend |
|
35 * @package Zend_Tool |
|
36 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
37 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
38 */ |
|
39 class Zend_Tool_Framework_Provider_Repository |
|
40 implements Zend_Tool_Framework_Registry_EnabledInterface, IteratorAggregate, Countable |
|
41 { |
|
42 |
|
43 /** |
|
44 * @var Zend_Tool_Framework_Registry |
|
45 */ |
|
46 protected $_registry = null; |
|
47 |
|
48 /** |
|
49 * @var bool |
|
50 */ |
|
51 protected $_processOnAdd = false; |
|
52 |
|
53 /** |
|
54 * @var Zend_Tool_Framework_Provider_Interface[] |
|
55 */ |
|
56 protected $_unprocessedProviders = array(); |
|
57 |
|
58 /** |
|
59 * @var Zend_Tool_Framework_Provider_Signature[] |
|
60 */ |
|
61 protected $_providerSignatures = array(); |
|
62 |
|
63 /** |
|
64 * @var array Array of Zend_Tool_Framework_Provider_Inteface |
|
65 */ |
|
66 protected $_providers = array(); |
|
67 |
|
68 /** |
|
69 * setRegistry() |
|
70 * |
|
71 * @param Zend_Tool_Framework_Registry_Interface $registry |
|
72 * @return unknown |
|
73 */ |
|
74 public function setRegistry(Zend_Tool_Framework_Registry_Interface $registry) |
|
75 { |
|
76 $this->_registry = $registry; |
|
77 return $this; |
|
78 } |
|
79 |
|
80 /** |
|
81 * Set the ProcessOnAdd flag |
|
82 * |
|
83 * @param unknown_type $processOnAdd |
|
84 * @return unknown |
|
85 */ |
|
86 public function setProcessOnAdd($processOnAdd = true) |
|
87 { |
|
88 $this->_processOnAdd = (bool) $processOnAdd; |
|
89 return $this; |
|
90 } |
|
91 |
|
92 /** |
|
93 * Add a provider to the repository for processing |
|
94 * |
|
95 * @param Zend_Tool_Framework_Provider_Interface $provider |
|
96 * @return Zend_Tool_Framework_Provider_Repository |
|
97 */ |
|
98 public function addProvider(Zend_Tool_Framework_Provider_Interface $provider, $overwriteExistingProvider = false) |
|
99 { |
|
100 if ($provider instanceof Zend_Tool_Framework_Registry_EnabledInterface) { |
|
101 $provider->setRegistry($this->_registry); |
|
102 } |
|
103 |
|
104 if (method_exists($provider, 'getName')) { |
|
105 $providerName = $provider->getName(); |
|
106 } else { |
|
107 $providerName = $this->_parseName($provider); |
|
108 } |
|
109 |
|
110 // if a provider by the given name already exist, and its not set as overwritable, throw exception |
|
111 if (!$overwriteExistingProvider && |
|
112 (array_key_exists($providerName, $this->_unprocessedProviders) |
|
113 || array_key_exists($providerName, $this->_providers))) |
|
114 { |
|
115 require_once 'Zend/Tool/Framework/Provider/Exception.php'; |
|
116 throw new Zend_Tool_Framework_Provider_Exception('A provider by the name ' . $providerName |
|
117 . ' is already registered and $overrideExistingProvider is set to false.'); |
|
118 } |
|
119 |
|
120 $this->_unprocessedProviders[$providerName] = $provider; |
|
121 |
|
122 // if process has already been called, process immediately. |
|
123 if ($this->_processOnAdd) { |
|
124 $this->process(); |
|
125 } |
|
126 |
|
127 return $this; |
|
128 } |
|
129 |
|
130 public function hasProvider($providerOrClassName, $processedOnly = true) |
|
131 { |
|
132 if ($providerOrClassName instanceof Zend_Tool_Framework_Provider_Interface) { |
|
133 $targetProviderClassName = get_class($providerOrClassName); |
|
134 } else { |
|
135 $targetProviderClassName = (string) $providerOrClassName; |
|
136 } |
|
137 |
|
138 if (!$processedOnly) { |
|
139 foreach ($this->_unprocessedProviders as $unprocessedProvider) { |
|
140 if (get_class($unprocessedProvider) == $targetProviderClassName) { |
|
141 return true; |
|
142 } |
|
143 } |
|
144 } |
|
145 |
|
146 foreach ($this->_providers as $processedProvider) { |
|
147 if (get_class($processedProvider) == $targetProviderClassName) { |
|
148 return true; |
|
149 } |
|
150 } |
|
151 |
|
152 return false; |
|
153 } |
|
154 |
|
155 /** |
|
156 * Process all of the unprocessed providers |
|
157 * |
|
158 */ |
|
159 public function process() |
|
160 { |
|
161 |
|
162 // process all providers in the unprocessedProviders array |
|
163 //foreach ($this->_unprocessedProviders as $providerName => $provider) { |
|
164 reset($this->_unprocessedProviders); |
|
165 while ($this->_unprocessedProviders) { |
|
166 |
|
167 $providerName = key($this->_unprocessedProviders); |
|
168 $provider = array_shift($this->_unprocessedProviders); |
|
169 |
|
170 // create a signature for the provided provider |
|
171 $providerSignature = new Zend_Tool_Framework_Provider_Signature($provider); |
|
172 |
|
173 if ($providerSignature instanceof Zend_Tool_Framework_Registry_EnabledInterface) { |
|
174 $providerSignature->setRegistry($this->_registry); |
|
175 } |
|
176 |
|
177 $providerSignature->process(); |
|
178 |
|
179 // ensure the name is lowercased for easier searching |
|
180 $providerName = strtolower($providerName); |
|
181 |
|
182 // add to the appropraite place |
|
183 $this->_providerSignatures[$providerName] = $providerSignature; |
|
184 $this->_providers[$providerName] = $providerSignature->getProvider(); |
|
185 |
|
186 if ($provider instanceof Zend_Tool_Framework_Provider_Initializable) { |
|
187 $provider->initialize(); |
|
188 } |
|
189 |
|
190 } |
|
191 |
|
192 } |
|
193 |
|
194 /** |
|
195 * getProviders() Get all the providers in the repository |
|
196 * |
|
197 * @return array |
|
198 */ |
|
199 public function getProviders() |
|
200 { |
|
201 return $this->_providers; |
|
202 } |
|
203 |
|
204 /** |
|
205 * getProviderSignatures() Get all the provider signatures |
|
206 * |
|
207 * @return array |
|
208 */ |
|
209 public function getProviderSignatures() |
|
210 { |
|
211 return $this->_providerSignatures; |
|
212 } |
|
213 |
|
214 /** |
|
215 * getProvider() |
|
216 * |
|
217 * @param string $providerName |
|
218 * @return Zend_Tool_Framework_Provider_Interface |
|
219 */ |
|
220 public function getProvider($providerName) |
|
221 { |
|
222 return $this->_providers[strtolower($providerName)]; |
|
223 } |
|
224 |
|
225 /** |
|
226 * getProviderSignature() |
|
227 * |
|
228 * @param string $providerName |
|
229 * @return Zend_Tool_Framework_Provider_Signature |
|
230 */ |
|
231 public function getProviderSignature($providerName) |
|
232 { |
|
233 return $this->_providerSignatures[strtolower($providerName)]; |
|
234 } |
|
235 |
|
236 /** |
|
237 * count() - return the number of providers |
|
238 * |
|
239 * @return int |
|
240 */ |
|
241 public function count() |
|
242 { |
|
243 return count($this->_providers); |
|
244 } |
|
245 |
|
246 /** |
|
247 * getIterator() - Required by the IteratorAggregate Interface |
|
248 * |
|
249 * @return ArrayIterator |
|
250 */ |
|
251 public function getIterator() |
|
252 { |
|
253 return new ArrayIterator($this->getProviders()); |
|
254 } |
|
255 |
|
256 /** |
|
257 * _parseName - internal method to determine the name of an action when one is not explicity provided. |
|
258 * |
|
259 * @param Zend_Tool_Framework_Action_Interface $action |
|
260 * @return string |
|
261 */ |
|
262 protected function _parseName(Zend_Tool_Framework_Provider_Interface $provider) |
|
263 { |
|
264 $className = get_class($provider); |
|
265 $providerName = $className; |
|
266 if (strpos($providerName, '_') !== false) { |
|
267 $providerName = substr($providerName, strrpos($providerName, '_')+1); |
|
268 } |
|
269 if (substr($providerName, -8) == 'Provider') { |
|
270 $providerName = substr($providerName, 0, strlen($providerName)-8); |
|
271 } |
|
272 return $providerName; |
|
273 } |
|
274 |
|
275 } |