Repository.php 5.1 KB

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