PageRenderTime 24ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/standard/incubator/library/Zend/Build/Manifest.php

https://github.com/bhaumik25/zend-framework
PHP | 294 lines | 124 code | 29 blank | 141 comment | 24 complexity | 8b4f93cc905e8909e990f97447a9694f 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_Build
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: $
  20. */
  21. /*
  22. * This is a singleton class which provides access to all the manifest files found on the classpath via
  23. * a constant-time-read data structure.
  24. *
  25. * @category Zend
  26. * @package Zend_Build
  27. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  28. * @license http://framework.zend.com/license/new-bsd New BSD License
  29. */
  30. class Zend_Build_Manifest
  31. {
  32. /**
  33. * @constant string
  34. */
  35. const MANIFEST_FILE_PATTERN = '/^[A-Z][0-9a-z].+-ZFManifest(....)$/';
  36. /**
  37. * @constant string
  38. */
  39. const ZEND_CONFIG_PACKAGE = 'Zend_Config_';
  40. /**
  41. * @constant string
  42. */
  43. const CONSOLE_CONTEXT_CONFIG_NAME = 'context';
  44. /**
  45. * @var Object
  46. */
  47. private static $_instance = null;
  48. /**
  49. * Serves as a simple index in to the config array for this manifest instance.
  50. *
  51. * @var array
  52. */
  53. private $_configIndex = array();
  54. /**
  55. * Serves as a simple index in to the config array for this manifest instance.
  56. *
  57. * @var array
  58. */
  59. private $_configArray = array();
  60. /**
  61. * Constructor
  62. *
  63. * @return void
  64. */
  65. private function __construct() {}
  66. /**
  67. * __clone
  68. *
  69. * @return void
  70. */
  71. private function __clone() {}
  72. /**
  73. * getInstance
  74. *
  75. * @return Zend_Build_Manifest
  76. */
  77. public function getInstance()
  78. {
  79. if(!isset(self::$_instance)) {
  80. self::$_instance = new self;
  81. }
  82. return self::$_instance;
  83. }
  84. /**
  85. * init
  86. *
  87. * @param array $files
  88. * @return void
  89. */
  90. public function init(array $files = null)
  91. {
  92. // If files isn't specified, look for all valid ZFManifest files on the include path.
  93. if (!isset($files)) {
  94. $files = array();
  95. $includeDirs = explode(PATH_SEPARATOR, get_include_path());
  96. foreach ($includeDirs as $dir) {
  97. $files = array_merge($files, $this->_recursiveSearch(self::MANIFEST_FILE_PATTERN, $dir));
  98. }
  99. }
  100. // Now load all the manifest files
  101. foreach ($files as $file) {
  102. try {
  103. $this->_loadManifestFile($file);
  104. } catch(Zend_Build_Manifest_NameConflictException $e) {
  105. // Oftentimes include paths can overlap, so this should issue a warning and continue.
  106. continue;
  107. }
  108. }
  109. }
  110. /**
  111. * getContext
  112. *
  113. * @param string $type
  114. * @param string $name
  115. * @return mixed|null
  116. */
  117. public function getContext($type, $name)
  118. {
  119. if (array_key_exists($type, $this->_configIndex) && array_key_exists($name, $this->_configIndex[$type])) {
  120. return $this->_configIndex[$type][$name];
  121. } else {
  122. return null;
  123. }
  124. }
  125. /**
  126. * toConfig
  127. *
  128. * @param boolean $allowModifications
  129. * @return Zend_Config
  130. */
  131. public function toConfig($allowModifications = true)
  132. {
  133. /**
  134. * @see Zend_Config
  135. */
  136. require_once('Zend/Config.php');
  137. return new Zend_Config($this->toArray(), $allowModifications);
  138. }
  139. /**
  140. * toArray
  141. *
  142. * @return array
  143. */
  144. public function toArray()
  145. {
  146. return $this->_configArray;
  147. }
  148. /**
  149. * _recursiveSearch
  150. *
  151. * @param string $pattern
  152. * @param string $dir
  153. * @return array
  154. */
  155. private function _recursiveSearch($pattern, $dir)
  156. {
  157. $contents = scandir($dir);
  158. $matches = array();
  159. foreach($contents as $content) {
  160. // Skip any directory that starts with a dot
  161. if(strpos($content, '.') === 0) {
  162. continue;
  163. }
  164. $fullPathContent = $dir . DIRECTORY_SEPARATOR . $content;
  165. if(is_dir($fullPathContent)) {
  166. $matches = array_merge($matches, $this->_recursiveSearch($pattern, $fullPathContent));
  167. } elseif(is_file($fullPathContent) && preg_match($pattern, $content)) {
  168. $matches[] = $fullPathContent;
  169. } else {
  170. // We'll skip links and any other file system objects
  171. }
  172. }
  173. return $matches;
  174. }
  175. /**
  176. * _loadManifestFile
  177. *
  178. * @param string $file
  179. * @throws Zend_Build_Exception
  180. * @throws Zend_Build_Manifest_NameConflictException
  181. * @return void
  182. */
  183. private function _loadManifestFile($file)
  184. {
  185. // Figure out which config class to use and load it
  186. $extension = substr(strrchr($file, "."), 1);
  187. if(strtolower($extension) != 'xml') {
  188. /**
  189. * @see Zend_Build_Exception
  190. */
  191. require_once 'Zend/Build/Exception.php';
  192. throw new Zend_Build_Exception("Currently XML is the only config format supported for manifest files." .
  193. "Extension '$extension' is invalid.");
  194. }
  195. $configClass = self::ZEND_CONFIG_PACKAGE . ucfirst(strtolower($extension));
  196. try {
  197. /**
  198. * @see Zend_Loader
  199. */
  200. require_once 'Zend/Loader.php';
  201. Zend_Loader::loadClass($configClass, explode(PATH_SEPARATOR, get_include_path()));
  202. } catch(Zend_Exception $e) {
  203. // Problem with loading the config class
  204. /**
  205. * @see Zend_Build_Exception
  206. */
  207. require_once 'Zend/Build/Exception.php';
  208. throw new Zend_Build_Exception("Config class '$configClass' could not be found.");
  209. }
  210. $configs = new $configClass($file);
  211. foreach($configs as $key => $value) {
  212. if($key != 'context') {
  213. continue;
  214. } else {
  215. $consoleContext = $value;
  216. }
  217. // Check that 'name' is set first
  218. $name = $consoleContext->name;
  219. if(!isset($name)) {
  220. /**
  221. * @see Zend_Build_Exception
  222. */
  223. require_once 'Zend/Build/Exception.php';
  224. throw new Zend_Build_Exception("Console context in '$file' does not have required 'name' attribute.");
  225. }
  226. // Create separate sections in the index so that different kinds of contexts can live
  227. // in different namespaces and still be efficiently accessed.
  228. $type = $consoleContext->type;
  229. //Index it under key for 'name' after testing it to see if we've already added it
  230. if (isset($this->_configIndex[$type])) {
  231. if (isset($this->_configIndex[$type][$name])) {
  232. // Problem with loading the class
  233. /**
  234. * @see Zend_Build_Manifest_NameConflictException
  235. */
  236. require_once 'Zend/Build/Manifest/NameConflictException.php';
  237. throw new Zend_Build_Manifest_NameConflictException(
  238. "Manifest already contains a context with name '$name' and type '$type'."
  239. );
  240. } else {
  241. // Do nothing. Type already maps to an array in the index.
  242. }
  243. } else {
  244. // Need to create an array for type to map to.
  245. $this->_configIndex[$type] = array();
  246. }
  247. $this->_configArray[] = $consoleContext;
  248. $this->_configIndex[$type][$name] = $consoleContext;
  249. $alias = $consoleContext->alias;
  250. // Also index it under 'alias' for instant access
  251. if(isset($alias)) {
  252. if (isset($this->_configIndex[$type]) && isset($this->_configIndex[$type][$alias])) {
  253. /**
  254. * @see Zend_Build_Exception
  255. */
  256. require_once 'Zend/Build/Exception.php';
  257. throw new Zend_Build_Exception("Manifest already contains a console context with alias '$alias'");
  258. } else {
  259. // Type must map to an array after handling the name above.
  260. $this->_configIndex[$type][$alias] = $consoleContext;
  261. }
  262. }
  263. }
  264. }
  265. }