PageRenderTime 25ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Tool/Framework/Manifest/Repository.php

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