File indexing completed on 2024-09-22 05:21:26

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Tool
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 // require_once 'Zend/Tool/Project/Context/System/Interface.php';
0023 // require_once 'Zend/Tool/Project/Context/System/TopLevelRestrictable.php';
0024 // require_once 'Zend/Tool/Project/Context/System/NotOverwritable.php';
0025 
0026 /**
0027  * @category   Zend
0028  * @package    Zend_Tool
0029  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0030  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0031  */
0032 class Zend_Tool_Project_Context_Repository implements Countable
0033 {
0034 
0035     protected static $_instance = null;
0036     protected static $_isInitialized = false;
0037 
0038     protected $_shortContextNames = array();
0039     protected $_contexts          = array();
0040 
0041     /**
0042      * Enter description here...
0043      *
0044      * @return Zend_Tool_Project_Context_Repository
0045      */
0046     public static function getInstance()
0047     {
0048         if (self::$_instance == null) {
0049             self::$_instance = new self();
0050         }
0051 
0052         return self::$_instance;
0053     }
0054 
0055     public static function resetInstance()
0056     {
0057         self::$_instance = null;
0058         self::$_isInitialized = false;
0059     }
0060 
0061     protected function __construct()
0062     {
0063         if (self::$_isInitialized == false) {
0064             $this->addContextClass('Zend_Tool_Project_Context_System_ProjectDirectory')
0065                  ->addContextClass('Zend_Tool_Project_Context_System_ProjectProfileFile')
0066                  ->addContextClass('Zend_Tool_Project_Context_System_ProjectProvidersDirectory');
0067             self::$_isInitialized = true;
0068         }
0069     }
0070 
0071     public function addContextsFromDirectory($directory, $prefix)
0072     {
0073         $prefix = trim($prefix, '_') . '_';
0074         foreach (new DirectoryIterator($directory) as $directoryItem) {
0075             if ($directoryItem->isDot() || (substr($directoryItem->getFilename(), -4) !== '.php')) {
0076                 continue;
0077             }
0078             $class = $prefix . substr($directoryItem->getFilename(), 0, -4);
0079             $this->addContextClass($class);
0080         }
0081     }
0082 
0083 
0084     public function addContextClass($contextClass)
0085     {
0086         if (!class_exists($contextClass)) {
0087             // require_once 'Zend/Loader.php';
0088             Zend_Loader::loadClass($contextClass);
0089         }
0090         $reflectionContextClass = new ReflectionClass($contextClass);
0091         if ($reflectionContextClass->isInstantiable()) {
0092             $context = new $contextClass();
0093             return $this->addContext($context);
0094         }
0095         return $this;
0096     }
0097 
0098     /**
0099      * Enter description here...
0100      *
0101      * @param Zend_Tool_Project_Context_Interface $context
0102      * @return Zend_Tool_Project_Context_Repository
0103      */
0104     public function addContext(Zend_Tool_Project_Context_Interface $context)
0105     {
0106         $isSystem       = ($context instanceof Zend_Tool_Project_Context_System_Interface);
0107         $isTopLevel     = ($context instanceof Zend_Tool_Project_Context_System_TopLevelRestrictable);
0108         $isOverwritable = !($context instanceof Zend_Tool_Project_Context_System_NotOverwritable);
0109 
0110         $index = (count($this->_contexts)) ? max(array_keys($this->_contexts)) + 1 : 1;
0111 
0112         $normalName = $this->_normalizeName($context->getName());
0113 
0114         if (isset($this->_shortContextNames[$normalName]) && ($this->_contexts[$this->_shortContextNames[$normalName]]['isOverwritable'] === false) ) {
0115             // require_once 'Zend/Tool/Project/Context/Exception.php';
0116             throw new Zend_Tool_Project_Context_Exception('Context ' . $context->getName() . ' is not overwriteable.');
0117         }
0118 
0119         $this->_shortContextNames[$normalName] = $index;
0120         $this->_contexts[$index] = array(
0121             'isTopLevel'     => $isTopLevel,
0122             'isSystem'       => $isSystem,
0123             'isOverwritable' => $isOverwritable,
0124             'normalName'     => $normalName,
0125             'context'        => $context
0126             );
0127 
0128         return $this;
0129     }
0130 
0131     public function getContext($name)
0132     {
0133         if (!$this->hasContext($name)) {
0134             // require_once 'Zend/Tool/Project/Context/Exception.php';
0135             throw new Zend_Tool_Project_Context_Exception('Context by name ' . $name . ' does not exist in the registry.');
0136         }
0137 
0138         $name = $this->_normalizeName($name);
0139         return clone $this->_contexts[$this->_shortContextNames[$name]]['context'];
0140     }
0141 
0142     public function hasContext($name)
0143     {
0144         $name = $this->_normalizeName($name);
0145         return (isset($this->_shortContextNames[$name]) ? true : false);
0146     }
0147 
0148     public function isSystemContext($name)
0149     {
0150         if (!$this->hasContext($name)) {
0151             return false;
0152         }
0153 
0154         $name = $this->_normalizeName($name);
0155         $index = $this->_shortContextNames[$name];
0156         return $this->_contexts[$index]['isSystemContext'];
0157     }
0158 
0159     public function isTopLevelContext($name)
0160     {
0161         if (!$this->hasContext($name)) {
0162             return false;
0163         }
0164         $name = $this->_normalizeName($name);
0165         $index = $this->_shortContextNames[$name];
0166         return $this->_contexts[$index]['isTopLevel'];
0167     }
0168 
0169     public function isOverwritableContext($name)
0170     {
0171         if (!$this->hasContext($name)) {
0172             return false;
0173         }
0174         $name = $this->_normalizeName($name);
0175         $index = $this->_shortContextNames[$name];
0176         return $this->_contexts[$index]['isOverwritable'];
0177     }
0178 
0179     public function count()
0180     {
0181         return count($this->_contexts);
0182     }
0183 
0184     protected function _normalizeName($name)
0185     {
0186         return strtolower($name);
0187     }
0188 
0189 }