Repository.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. require_once 'Zend/Tool/Project/Context/System/Interface.php';
  3. require_once 'Zend/Tool/Project/Context/System/TopLevelRestrictable.php';
  4. require_once 'Zend/Tool/Project/Context/System/NotOverwritable.php';
  5. class Zend_Tool_Project_Context_Repository implements Countable
  6. {
  7. protected static $_instance = null;
  8. protected static $_isInitialized = false;
  9. protected $_shortContextNames = array();
  10. protected $_contexts = array();
  11. /**
  12. * Enter description here...
  13. *
  14. * @return Zend_Tool_Project_Context_Repository
  15. */
  16. public static function getInstance()
  17. {
  18. if (self::$_instance == null) {
  19. self::$_instance = new self();
  20. }
  21. return self::$_instance;
  22. }
  23. public static function resetInstance()
  24. {
  25. self::$_instance = null;
  26. self::$_isInitialized = false;
  27. }
  28. protected function __construct()
  29. {
  30. if (self::$_isInitialized == false) {
  31. $this->addContextClass('Zend_Tool_Project_Context_System_ProjectDirectory')
  32. ->addContextClass('Zend_Tool_Project_Context_System_ProjectProfileFile')
  33. ->addContextClass('Zend_Tool_Project_Context_System_ProjectProvidersDirectory');
  34. self::$_isInitialized = true;
  35. }
  36. }
  37. public function addContextsFromDirectory($directory, $prefix)
  38. {
  39. $prefix = trim($prefix, '_') . '_';
  40. foreach (new DirectoryIterator($directory) as $directoryItem) {
  41. if ($directoryItem->isDot() || (substr($directoryItem->getFilename(), -4) !== '.php')) {
  42. continue;
  43. }
  44. $class = $prefix . substr($directoryItem->getFilename(), 0, -4);
  45. $this->addContextClass($class);
  46. }
  47. }
  48. public function addContextClass($contextClass)
  49. {
  50. if (!class_exists($contextClass)) {
  51. require_once 'Zend/Loader.php';
  52. Zend_Loader::loadClass($contextClass);
  53. }
  54. $context = new $contextClass();
  55. return $this->addContext($context);
  56. }
  57. /**
  58. * Enter description here...
  59. *
  60. * @param Zend_Tool_Project_Context_Interface $context
  61. * @return Zend_Tool_Project_Context_Repository
  62. */
  63. public function addContext(Zend_Tool_Project_Context_Interface $context)
  64. {
  65. $isSystem = ($context instanceof Zend_Tool_Project_Context_System_Interface);
  66. $isTopLevel = ($context instanceof Zend_Tool_Project_Context_System_TopLevelRestrictable);
  67. $isOverwritable = !($context instanceof Zend_Tool_Project_Context_System_NotOverwritable);
  68. $index = (count($this->_contexts)) ? max(array_keys($this->_contexts)) + 1 : 1;
  69. $normalName = $this->_normalizeName($context->getName());
  70. if (isset($this->_shortContextNames[$normalName]) && ($this->_contexts[$this->_shortContextNames[$normalName]]['isOverwritable'] === false) ) {
  71. require_once 'Zend/Tool/Project/Context/Exception.php';
  72. throw new Zend_Tool_Project_Context_Exception('Context ' . $context->getName() . ' is not overwriteable.');
  73. }
  74. $this->_shortContextNames[$normalName] = $index;
  75. $this->_contexts[$index] = array(
  76. 'isTopLevel' => $isTopLevel,
  77. 'isSystem' => $isSystem,
  78. 'isOverwritable' => $isOverwritable,
  79. 'normalName' => $normalName,
  80. 'context' => $context
  81. );
  82. return $this;
  83. }
  84. public function getContext($name)
  85. {
  86. if (!$this->hasContext($name)) {
  87. require_once 'Zend/Tool/Project/Context/Exception.php';
  88. throw new Zend_Tool_Project_Context_Exception('Context by name ' . $name . ' does not exist in the registry.');
  89. }
  90. $name = $this->_normalizeName($name);
  91. return clone $this->_contexts[$this->_shortContextNames[$name]]['context'];
  92. }
  93. public function hasContext($name)
  94. {
  95. $name = $this->_normalizeName($name);
  96. return (isset($this->_shortContextNames[$name]) ? true : false);
  97. }
  98. public function isSystemContext($name)
  99. {
  100. if (!$this->hasContext($name)) {
  101. return false;
  102. }
  103. $name = $this->_normalizeName($name);
  104. $index = $this->_shortContextNames[$name];
  105. return $this->_contexts[$index]['isSystemContext'];
  106. }
  107. public function isTopLevelContext($name)
  108. {
  109. if (!$this->hasContext($name)) {
  110. return false;
  111. }
  112. $name = $this->_normalizeName($name);
  113. $index = $this->_shortContextNames[$name];
  114. return $this->_contexts[$index]['isTopLevel'];
  115. }
  116. public function isOverwritableContext($name)
  117. {
  118. if (!$this->hasContext($name)) {
  119. return false;
  120. }
  121. $name = $this->_normalizeName($name);
  122. $index = $this->_shortContextNames[$name];
  123. return $this->_contexts[$index]['isOverwritable'];
  124. }
  125. public function count()
  126. {
  127. return count($this->_contexts);
  128. }
  129. protected function _normalizeName($name)
  130. {
  131. return strtolower($name);
  132. }
  133. }