PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/rsilveira1987/Expresso
PHP | 300 lines | 138 code | 40 blank | 122 comment | 16 complexity | eb8af6057e01e82449d3c592ee8c079f 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-2009 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 10020 2009-08-18 14:34:09Z j.fischer@metaways.de $
  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-2009 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 instanceof Zend_Tool_Framework_Provider_Interface) {
  87. require_once 'Zend/Tool/Framework/Manifest/Exception.php';
  88. throw new Zend_Tool_Framework_Manifest_Exception(
  89. 'A provider provided by the ' . get_class($manifest)
  90. . ' does not implement Zend_Tool_Framework_Provider_Interface'
  91. );
  92. }
  93. if (!$providerRepository->hasProvider($provider, false)) {
  94. $providerRepository->addProvider($provider);
  95. }
  96. }
  97. }
  98. // load actions if interface supports that method
  99. if ($manifest instanceof Zend_Tool_Framework_Manifest_ActionManifestable) {
  100. $actions = $manifest->getActions();
  101. if (!is_array($actions)) {
  102. $actions = array($actions);
  103. }
  104. foreach ($actions as $action) {
  105. if (is_string($action)) {
  106. $action = new Zend_Tool_Framework_Action_Base($action);
  107. }
  108. $actionRepository->addAction($action);
  109. }
  110. }
  111. // should we detect collisions here? does it even matter?
  112. $this->_manifests[$index] = $manifest;
  113. ksort($this->_manifests);
  114. return $this;
  115. }
  116. /**
  117. * getManifests()
  118. *
  119. * @return Zend_Tool_Framework_Manifest_Interface[]
  120. */
  121. public function getManifests()
  122. {
  123. return $this->_manifests;
  124. }
  125. /**
  126. * addMetadata() - add a metadata peice by peice
  127. *
  128. * @param Zend_Tool_Framework_Manifest_Metadata $metadata
  129. * @return Zend_Tool_Framework_Manifest_Repository
  130. */
  131. public function addMetadata(Zend_Tool_Framework_Metadata_Interface $metadata)
  132. {
  133. $this->_metadatas[] = $metadata;
  134. return $this;
  135. }
  136. /**
  137. * process() - Process is expected to be called at the end of client construction time.
  138. * By this time, the loader has run and loaded any found manifests into the repository
  139. * for loading
  140. *
  141. * @return Zend_Tool_Framework_Manifest_Repository
  142. */
  143. public function process()
  144. {
  145. foreach ($this->_manifests as $manifest) {
  146. if ($manifest instanceof Zend_Tool_Framework_Manifest_MetadataManifestable) {
  147. $metadatas = $manifest->getMetadata();
  148. if (!is_array($metadatas)) {
  149. $metadatas = array($metadatas);
  150. }
  151. foreach ($metadatas as $metadata) {
  152. if (!$metadata instanceof Zend_Tool_Framework_Metadata_Interface) {
  153. require_once 'Zend/Tool/Framework/Manifest/Exception.php';
  154. throw new Zend_Tool_Framework_Manifest_Exception(
  155. 'A Zend_Tool_Framework_Metadata_Interface object was not found in manifest ' . get_class($manifest)
  156. );
  157. }
  158. $this->addMetadata($metadata);
  159. }
  160. }
  161. }
  162. return $this;
  163. }
  164. /**
  165. * getMetadatas() - This is the main search function for the repository.
  166. *
  167. * @example This will retrieve all metadata that matches the following criteria
  168. * $manifestRepo->getMetadatas(array(
  169. * 'providerName' => 'Version',
  170. * 'actionName' => 'show'
  171. * ));
  172. *
  173. * @param array $searchProperties
  174. * @param bool $includeNonExistentProperties
  175. * @return Zend_Tool_Framework_Manifest_Metadata[]
  176. */
  177. public function getMetadatas(Array $searchProperties = array(), $includeNonExistentProperties = true)
  178. {
  179. $returnMetadatas = array();
  180. // loop through the metadatas so that we can search each individual one
  181. foreach ($this->_metadatas as $metadata) {
  182. // each value will be retrieved from the metadata, each metadata should
  183. // implement a getter method to retrieve the value
  184. foreach ($searchProperties as $searchPropertyName => $searchPropertyValue) {
  185. if (method_exists($metadata, 'get' . $searchPropertyName)) {
  186. if ($metadata->{'get' . $searchPropertyName}() != $searchPropertyValue) {
  187. // if the metadata supports a specific property but the value does not
  188. // match, move on
  189. continue 2;
  190. }
  191. } elseif (!$includeNonExistentProperties) {
  192. // if the option $includeNonExitentProperties is false, then move on as
  193. // we dont want to include this metadata if non existent
  194. // search properties are not inside the target (current) metadata
  195. continue 2;
  196. }
  197. }
  198. // all searching has been accounted for, if we reach this point, then the metadata
  199. // is good and we can return it
  200. $returnMetadatas[] = $metadata;
  201. }
  202. return $returnMetadatas;
  203. }
  204. /**
  205. * getMetadata() - This will proxy to getMetadatas(), but will only return a single metadata. This method
  206. * should be used in situations where the search criteria is known to only find a single metadata object
  207. *
  208. * @param array $searchProperties
  209. * @param bool $includeNonExistentProperties
  210. * @return Zend_Tool_Framework_Manifest_Metadata
  211. */
  212. public function getMetadata(Array $searchProperties = array(), $includeNonExistentProperties = true)
  213. {
  214. $metadatas = $this->getMetadatas($searchProperties, $includeNonExistentProperties);
  215. return array_shift($metadatas);
  216. }
  217. /**
  218. * __toString() - cast to string
  219. *
  220. * @return string
  221. */
  222. public function __toString()
  223. {
  224. $metadatasByType = array();
  225. foreach ($this->_metadatas as $metadata) {
  226. if (!array_key_exists($metadata->getType(), $metadatasByType)) {
  227. $metadatasByType[$metadata->getType()] = array();
  228. }
  229. $metadatasByType[$metadata->getType()][] = $metadata;
  230. }
  231. $string = '';
  232. foreach ($metadatasByType as $type => $metadatas) {
  233. $string .= $type . PHP_EOL;
  234. foreach ($metadatas as $metadata) {
  235. $metadataString = ' ' . $metadata->__toString() . PHP_EOL;
  236. //$metadataString = str_replace(PHP_EOL, PHP_EOL . ' ', $metadataString);
  237. $string .= $metadataString;
  238. }
  239. }
  240. return $string;
  241. }
  242. /**
  243. * count() - required by the Countable Interface
  244. *
  245. * @return int
  246. */
  247. public function count()
  248. {
  249. return count($this->_metadatas);
  250. }
  251. /**
  252. * getIterator() - required by the IteratorAggregate interface
  253. *
  254. * @return ArrayIterator
  255. */
  256. public function getIterator()
  257. {
  258. return new ArrayIterator($this->_metadatas);
  259. }
  260. }