Repository.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. * @copyright Copyright (c) 2005-2015 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. require_once 'Zend/Tool/Project/Context/System/Interface.php';
  22. require_once 'Zend/Tool/Project/Context/System/TopLevelRestrictable.php';
  23. require_once 'Zend/Tool/Project/Context/System/NotOverwritable.php';
  24. /**
  25. * @category Zend
  26. * @package Zend_Tool
  27. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  28. * @license http://framework.zend.com/license/new-bsd New BSD License
  29. */
  30. class Zend_Tool_Project_Context_Repository implements Countable
  31. {
  32. protected static $_instance = null;
  33. protected static $_isInitialized = false;
  34. protected $_shortContextNames = array();
  35. protected $_contexts = array();
  36. /**
  37. * Enter description here...
  38. *
  39. * @return Zend_Tool_Project_Context_Repository
  40. */
  41. public static function getInstance()
  42. {
  43. if (self::$_instance == null) {
  44. self::$_instance = new self();
  45. }
  46. return self::$_instance;
  47. }
  48. public static function resetInstance()
  49. {
  50. self::$_instance = null;
  51. self::$_isInitialized = false;
  52. }
  53. protected function __construct()
  54. {
  55. if (self::$_isInitialized == false) {
  56. $this->addContextClass('Zend_Tool_Project_Context_System_ProjectDirectory')
  57. ->addContextClass('Zend_Tool_Project_Context_System_ProjectProfileFile')
  58. ->addContextClass('Zend_Tool_Project_Context_System_ProjectProvidersDirectory');
  59. self::$_isInitialized = true;
  60. }
  61. }
  62. public function addContextsFromDirectory($directory, $prefix)
  63. {
  64. $prefix = trim($prefix, '_') . '_';
  65. foreach (new DirectoryIterator($directory) as $directoryItem) {
  66. if ($directoryItem->isDot() || (substr($directoryItem->getFilename(), -4) !== '.php')) {
  67. continue;
  68. }
  69. $class = $prefix . substr($directoryItem->getFilename(), 0, -4);
  70. $this->addContextClass($class);
  71. }
  72. }
  73. public function addContextClass($contextClass)
  74. {
  75. if (!class_exists($contextClass)) {
  76. require_once 'Zend/Loader.php';
  77. Zend_Loader::loadClass($contextClass);
  78. }
  79. $reflectionContextClass = new ReflectionClass($contextClass);
  80. if ($reflectionContextClass->isInstantiable()) {
  81. $context = new $contextClass();
  82. return $this->addContext($context);
  83. }
  84. return $this;
  85. }
  86. /**
  87. * Enter description here...
  88. *
  89. * @param Zend_Tool_Project_Context_Interface $context
  90. * @return Zend_Tool_Project_Context_Repository
  91. */
  92. public function addContext(Zend_Tool_Project_Context_Interface $context)
  93. {
  94. $isSystem = ($context instanceof Zend_Tool_Project_Context_System_Interface);
  95. $isTopLevel = ($context instanceof Zend_Tool_Project_Context_System_TopLevelRestrictable);
  96. $isOverwritable = !($context instanceof Zend_Tool_Project_Context_System_NotOverwritable);
  97. $index = (count($this->_contexts)) ? max(array_keys($this->_contexts)) + 1 : 1;
  98. $normalName = $this->_normalizeName($context->getName());
  99. if (isset($this->_shortContextNames[$normalName]) && ($this->_contexts[$this->_shortContextNames[$normalName]]['isOverwritable'] === false) ) {
  100. require_once 'Zend/Tool/Project/Context/Exception.php';
  101. throw new Zend_Tool_Project_Context_Exception('Context ' . $context->getName() . ' is not overwriteable.');
  102. }
  103. $this->_shortContextNames[$normalName] = $index;
  104. $this->_contexts[$index] = array(
  105. 'isTopLevel' => $isTopLevel,
  106. 'isSystem' => $isSystem,
  107. 'isOverwritable' => $isOverwritable,
  108. 'normalName' => $normalName,
  109. 'context' => $context
  110. );
  111. return $this;
  112. }
  113. public function getContext($name)
  114. {
  115. if (!$this->hasContext($name)) {
  116. require_once 'Zend/Tool/Project/Context/Exception.php';
  117. throw new Zend_Tool_Project_Context_Exception('Context by name ' . $name . ' does not exist in the registry.');
  118. }
  119. $name = $this->_normalizeName($name);
  120. return clone $this->_contexts[$this->_shortContextNames[$name]]['context'];
  121. }
  122. public function hasContext($name)
  123. {
  124. $name = $this->_normalizeName($name);
  125. return (isset($this->_shortContextNames[$name]) ? true : false);
  126. }
  127. public function isSystemContext($name)
  128. {
  129. if (!$this->hasContext($name)) {
  130. return false;
  131. }
  132. $name = $this->_normalizeName($name);
  133. $index = $this->_shortContextNames[$name];
  134. return $this->_contexts[$index]['isSystemContext'];
  135. }
  136. public function isTopLevelContext($name)
  137. {
  138. if (!$this->hasContext($name)) {
  139. return false;
  140. }
  141. $name = $this->_normalizeName($name);
  142. $index = $this->_shortContextNames[$name];
  143. return $this->_contexts[$index]['isTopLevel'];
  144. }
  145. public function isOverwritableContext($name)
  146. {
  147. if (!$this->hasContext($name)) {
  148. return false;
  149. }
  150. $name = $this->_normalizeName($name);
  151. $index = $this->_shortContextNames[$name];
  152. return $this->_contexts[$index]['isOverwritable'];
  153. }
  154. public function count()
  155. {
  156. return count($this->_contexts);
  157. }
  158. protected function _normalizeName($name)
  159. {
  160. return strtolower($name);
  161. }
  162. }