Repository.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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-2009 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-2009 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. $context = new $contextClass();
  80. return $this->addContext($context);
  81. }
  82. /**
  83. * Enter description here...
  84. *
  85. * @param Zend_Tool_Project_Context_Interface $context
  86. * @return Zend_Tool_Project_Context_Repository
  87. */
  88. public function addContext(Zend_Tool_Project_Context_Interface $context)
  89. {
  90. $isSystem = ($context instanceof Zend_Tool_Project_Context_System_Interface);
  91. $isTopLevel = ($context instanceof Zend_Tool_Project_Context_System_TopLevelRestrictable);
  92. $isOverwritable = !($context instanceof Zend_Tool_Project_Context_System_NotOverwritable);
  93. $index = (count($this->_contexts)) ? max(array_keys($this->_contexts)) + 1 : 1;
  94. $normalName = $this->_normalizeName($context->getName());
  95. if (isset($this->_shortContextNames[$normalName]) && ($this->_contexts[$this->_shortContextNames[$normalName]]['isOverwritable'] === false) ) {
  96. require_once 'Zend/Tool/Project/Context/Exception.php';
  97. throw new Zend_Tool_Project_Context_Exception('Context ' . $context->getName() . ' is not overwriteable.');
  98. }
  99. $this->_shortContextNames[$normalName] = $index;
  100. $this->_contexts[$index] = array(
  101. 'isTopLevel' => $isTopLevel,
  102. 'isSystem' => $isSystem,
  103. 'isOverwritable' => $isOverwritable,
  104. 'normalName' => $normalName,
  105. 'context' => $context
  106. );
  107. return $this;
  108. }
  109. public function getContext($name)
  110. {
  111. if (!$this->hasContext($name)) {
  112. require_once 'Zend/Tool/Project/Context/Exception.php';
  113. throw new Zend_Tool_Project_Context_Exception('Context by name ' . $name . ' does not exist in the registry.');
  114. }
  115. $name = $this->_normalizeName($name);
  116. return clone $this->_contexts[$this->_shortContextNames[$name]]['context'];
  117. }
  118. public function hasContext($name)
  119. {
  120. $name = $this->_normalizeName($name);
  121. return (isset($this->_shortContextNames[$name]) ? true : false);
  122. }
  123. public function isSystemContext($name)
  124. {
  125. if (!$this->hasContext($name)) {
  126. return false;
  127. }
  128. $name = $this->_normalizeName($name);
  129. $index = $this->_shortContextNames[$name];
  130. return $this->_contexts[$index]['isSystemContext'];
  131. }
  132. public function isTopLevelContext($name)
  133. {
  134. if (!$this->hasContext($name)) {
  135. return false;
  136. }
  137. $name = $this->_normalizeName($name);
  138. $index = $this->_shortContextNames[$name];
  139. return $this->_contexts[$index]['isTopLevel'];
  140. }
  141. public function isOverwritableContext($name)
  142. {
  143. if (!$this->hasContext($name)) {
  144. return false;
  145. }
  146. $name = $this->_normalizeName($name);
  147. $index = $this->_shortContextNames[$name];
  148. return $this->_contexts[$index]['isOverwritable'];
  149. }
  150. public function count()
  151. {
  152. return count($this->_contexts);
  153. }
  154. protected function _normalizeName($name)
  155. {
  156. return strtolower($name);
  157. }
  158. }