PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

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