|
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 20096 2010-01-06 02:05:09Z bkarwin $ |
|
21 */ |
|
22 |
|
23 /** |
|
24 * @see Zend_Tool_Framework_Registry_EnabledInterface |
|
25 */ |
|
26 require_once 'Zend/Tool/Framework/Registry/EnabledInterface.php'; |
|
27 |
|
28 /** |
|
29 * @category Zend |
|
30 * @package Zend_Tool |
|
31 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
32 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
33 */ |
|
34 class Zend_Tool_Framework_Manifest_Repository |
|
35 implements Zend_Tool_Framework_Registry_EnabledInterface, IteratorAggregate, Countable |
|
36 { |
|
37 |
|
38 /** |
|
39 * @var Zend_Tool_Framework_Provider_Registry_Interface |
|
40 */ |
|
41 protected $_registry = null; |
|
42 |
|
43 /** |
|
44 * @var array |
|
45 */ |
|
46 protected $_manifests = array(); |
|
47 |
|
48 /** |
|
49 * @var array Array of Zend_Tool_Framework_Metadata_Interface |
|
50 */ |
|
51 protected $_metadatas = array(); |
|
52 |
|
53 /** |
|
54 * setRegistry() |
|
55 * |
|
56 * @param Zend_Tool_Framework_Registry_Interface $registry |
|
57 * @return unknown |
|
58 */ |
|
59 public function setRegistry(Zend_Tool_Framework_Registry_Interface $registry) |
|
60 { |
|
61 $this->_registry = $registry; |
|
62 return $this; |
|
63 } |
|
64 |
|
65 /** |
|
66 * addManifest() - Add a manifest for later processing |
|
67 * |
|
68 * @param Zend_Tool_Framework_Manifest_Interface $manifest |
|
69 * @return Zend_Tool_Framework_Manifest_Repository |
|
70 */ |
|
71 public function addManifest(Zend_Tool_Framework_Manifest_Interface $manifest) |
|
72 { |
|
73 // we need to get an index number so that manifests with |
|
74 // higher indexes have priority over others |
|
75 $index = count($this->_manifests); |
|
76 |
|
77 if ($manifest instanceof Zend_Tool_Framework_Registry_EnabledInterface) { |
|
78 $manifest->setRegistry($this->_registry); |
|
79 } |
|
80 |
|
81 // if the manifest supplies a getIndex() method, use it |
|
82 if ($manifest instanceof Zend_Tool_Framework_Manifest_Indexable) { |
|
83 $index = $manifest->getIndex(); |
|
84 } |
|
85 |
|
86 // get the required objects from the framework registry |
|
87 $actionRepository = $this->_registry->getActionRepository(); |
|
88 $providerRepository = $this->_registry->getProviderRepository(); |
|
89 |
|
90 // load providers if interface supports that method |
|
91 if ($manifest instanceof Zend_Tool_Framework_Manifest_ProviderManifestable) { |
|
92 $providers = $manifest->getProviders(); |
|
93 if (!is_array($providers)) { |
|
94 $providers = array($providers); |
|
95 } |
|
96 |
|
97 foreach ($providers as $provider) { |
|
98 |
|
99 // if provider is a string, try and load it as an object |
|
100 if (is_string($provider)) { |
|
101 $provider = new $provider(); |
|
102 } |
|
103 |
|
104 if (!$provider instanceof Zend_Tool_Framework_Provider_Interface) { |
|
105 require_once 'Zend/Tool/Framework/Manifest/Exception.php'; |
|
106 throw new Zend_Tool_Framework_Manifest_Exception( |
|
107 'A provider provided by the ' . get_class($manifest) |
|
108 . ' does not implement Zend_Tool_Framework_Provider_Interface' |
|
109 ); |
|
110 } |
|
111 if (!$providerRepository->hasProvider($provider, false)) { |
|
112 $providerRepository->addProvider($provider); |
|
113 } |
|
114 } |
|
115 |
|
116 } |
|
117 |
|
118 // load actions if interface supports that method |
|
119 if ($manifest instanceof Zend_Tool_Framework_Manifest_ActionManifestable) { |
|
120 $actions = $manifest->getActions(); |
|
121 if (!is_array($actions)) { |
|
122 $actions = array($actions); |
|
123 } |
|
124 |
|
125 foreach ($actions as $action) { |
|
126 if (is_string($action)) { |
|
127 $action = new Zend_Tool_Framework_Action_Base($action); |
|
128 } |
|
129 $actionRepository->addAction($action); |
|
130 } |
|
131 } |
|
132 |
|
133 // should we detect collisions here? does it even matter? |
|
134 $this->_manifests[$index] = $manifest; |
|
135 ksort($this->_manifests); |
|
136 |
|
137 return $this; |
|
138 } |
|
139 |
|
140 /** |
|
141 * getManifests() |
|
142 * |
|
143 * @return Zend_Tool_Framework_Manifest_Interface[] |
|
144 */ |
|
145 public function getManifests() |
|
146 { |
|
147 return $this->_manifests; |
|
148 } |
|
149 |
|
150 /** |
|
151 * addMetadata() - add a metadata peice by peice |
|
152 * |
|
153 * @param Zend_Tool_Framework_Manifest_Metadata $metadata |
|
154 * @return Zend_Tool_Framework_Manifest_Repository |
|
155 */ |
|
156 public function addMetadata(Zend_Tool_Framework_Metadata_Interface $metadata) |
|
157 { |
|
158 $this->_metadatas[] = $metadata; |
|
159 return $this; |
|
160 } |
|
161 |
|
162 /** |
|
163 * process() - Process is expected to be called at the end of client construction time. |
|
164 * By this time, the loader has run and loaded any found manifests into the repository |
|
165 * for loading |
|
166 * |
|
167 * @return Zend_Tool_Framework_Manifest_Repository |
|
168 */ |
|
169 public function process() |
|
170 { |
|
171 |
|
172 foreach ($this->_manifests as $manifest) { |
|
173 if ($manifest instanceof Zend_Tool_Framework_Manifest_MetadataManifestable) { |
|
174 $metadatas = $manifest->getMetadata(); |
|
175 if (!is_array($metadatas)) { |
|
176 $metadatas = array($metadatas); |
|
177 } |
|
178 |
|
179 foreach ($metadatas as $metadata) { |
|
180 if (is_array($metadata)) { |
|
181 if (!class_exists('Zend_Tool_Framework_Metadata_Dynamic')) { |
|
182 require_once 'Zend/Tool/Framework/Metadata/Dynamic.php'; |
|
183 } |
|
184 $metadata = new Zend_Tool_Framework_Metadata_Dynamic($metadata); |
|
185 } |
|
186 |
|
187 if (!$metadata instanceof Zend_Tool_Framework_Metadata_Interface) { |
|
188 require_once 'Zend/Tool/Framework/Manifest/Exception.php'; |
|
189 throw new Zend_Tool_Framework_Manifest_Exception( |
|
190 'A Zend_Tool_Framework_Metadata_Interface object was not found in manifest ' . get_class($manifest) |
|
191 ); |
|
192 } |
|
193 |
|
194 $this->addMetadata($metadata); |
|
195 } |
|
196 |
|
197 } |
|
198 } |
|
199 |
|
200 return $this; |
|
201 } |
|
202 |
|
203 /** |
|
204 * getMetadatas() - This is the main search function for the repository. |
|
205 * |
|
206 * example: This will retrieve all metadata that matches the following criteria |
|
207 * $manifestRepo->getMetadatas(array( |
|
208 * 'providerName' => 'Version', |
|
209 * 'actionName' => 'show' |
|
210 * )); |
|
211 * |
|
212 * @param array $searchProperties |
|
213 * @param bool $includeNonExistentProperties |
|
214 * @return Zend_Tool_Framework_Manifest_Metadata[] |
|
215 */ |
|
216 public function getMetadatas(Array $searchProperties = array(), $includeNonExistentProperties = true) |
|
217 { |
|
218 |
|
219 $returnMetadatas = array(); |
|
220 |
|
221 // loop through the metadatas so that we can search each individual one |
|
222 foreach ($this->_metadatas as $metadata) { |
|
223 |
|
224 // each value will be retrieved from the metadata, each metadata should |
|
225 // implement a getter method to retrieve the value |
|
226 foreach ($searchProperties as $searchPropertyName => $searchPropertyValue) { |
|
227 if (method_exists($metadata, 'get' . $searchPropertyName)) { |
|
228 if ($metadata->{'get' . $searchPropertyName}() != $searchPropertyValue) { |
|
229 // if the metadata supports a specific property but the value does not |
|
230 // match, move on |
|
231 continue 2; |
|
232 } |
|
233 } elseif (!$includeNonExistentProperties) { |
|
234 // if the option $includeNonExitentProperties is false, then move on as |
|
235 // we dont want to include this metadata if non existent |
|
236 // search properties are not inside the target (current) metadata |
|
237 continue 2; |
|
238 } |
|
239 } |
|
240 |
|
241 // all searching has been accounted for, if we reach this point, then the metadata |
|
242 // is good and we can return it |
|
243 $returnMetadatas[] = $metadata; |
|
244 |
|
245 } |
|
246 |
|
247 return $returnMetadatas; |
|
248 } |
|
249 |
|
250 /** |
|
251 * getMetadata() - This will proxy to getMetadatas(), but will only return a single metadata. This method |
|
252 * should be used in situations where the search criteria is known to only find a single metadata object |
|
253 * |
|
254 * @param array $searchProperties |
|
255 * @param bool $includeNonExistentProperties |
|
256 * @return Zend_Tool_Framework_Manifest_Metadata |
|
257 */ |
|
258 public function getMetadata(Array $searchProperties = array(), $includeNonExistentProperties = true) |
|
259 { |
|
260 $metadatas = $this->getMetadatas($searchProperties, $includeNonExistentProperties); |
|
261 return array_shift($metadatas); |
|
262 } |
|
263 |
|
264 /** |
|
265 * __toString() - cast to string |
|
266 * |
|
267 * @return string |
|
268 */ |
|
269 public function __toString() |
|
270 { |
|
271 $metadatasByType = array(); |
|
272 |
|
273 foreach ($this->_metadatas as $metadata) { |
|
274 if (!array_key_exists($metadata->getType(), $metadatasByType)) { |
|
275 $metadatasByType[$metadata->getType()] = array(); |
|
276 } |
|
277 $metadatasByType[$metadata->getType()][] = $metadata; |
|
278 } |
|
279 |
|
280 $string = ''; |
|
281 foreach ($metadatasByType as $type => $metadatas) { |
|
282 $string .= $type . PHP_EOL; |
|
283 foreach ($metadatas as $metadata) { |
|
284 $metadataString = ' ' . $metadata->__toString() . PHP_EOL; |
|
285 //$metadataString = str_replace(PHP_EOL, PHP_EOL . ' ', $metadataString); |
|
286 $string .= $metadataString; |
|
287 } |
|
288 } |
|
289 |
|
290 return $string; |
|
291 } |
|
292 |
|
293 /** |
|
294 * count() - required by the Countable Interface |
|
295 * |
|
296 * @return int |
|
297 */ |
|
298 public function count() |
|
299 { |
|
300 return count($this->_metadatas); |
|
301 } |
|
302 |
|
303 /** |
|
304 * getIterator() - required by the IteratorAggregate interface |
|
305 * |
|
306 * @return ArrayIterator |
|
307 */ |
|
308 public function getIterator() |
|
309 { |
|
310 return new ArrayIterator($this->_metadatas); |
|
311 } |
|
312 |
|
313 } |