/library/Zend/Tool/Project/Context/Repository.php

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