web/lib/Zend/Tool/Project/Context/Repository.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     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-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    18  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    19  * @version    $Id: Repository.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    20  */
       
    21 
       
    22 require_once 'Zend/Tool/Project/Context/System/Interface.php';
       
    23 require_once 'Zend/Tool/Project/Context/System/TopLevelRestrictable.php';
       
    24 require_once 'Zend/Tool/Project/Context/System/NotOverwritable.php';
       
    25 
       
    26 /**
       
    27  * @category   Zend
       
    28  * @package    Zend_Tool
       
    29  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    30  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    31  */
       
    32 class Zend_Tool_Project_Context_Repository implements Countable
       
    33 {
       
    34 
       
    35     protected static $_instance = null;
       
    36     protected static $_isInitialized = false;
       
    37 
       
    38     protected $_shortContextNames = array();
       
    39     protected $_contexts          = array();
       
    40 
       
    41     /**
       
    42      * Enter description here...
       
    43      *
       
    44      * @return Zend_Tool_Project_Context_Repository
       
    45      */
       
    46     public static function getInstance()
       
    47     {
       
    48         if (self::$_instance == null) {
       
    49             self::$_instance = new self();
       
    50         }
       
    51 
       
    52         return self::$_instance;
       
    53     }
       
    54 
       
    55     public static function resetInstance()
       
    56     {
       
    57         self::$_instance = null;
       
    58         self::$_isInitialized = false;
       
    59     }
       
    60 
       
    61     protected function __construct()
       
    62     {
       
    63         if (self::$_isInitialized == false) {
       
    64             $this->addContextClass('Zend_Tool_Project_Context_System_ProjectDirectory')
       
    65                  ->addContextClass('Zend_Tool_Project_Context_System_ProjectProfileFile')
       
    66                  ->addContextClass('Zend_Tool_Project_Context_System_ProjectProvidersDirectory');
       
    67             self::$_isInitialized = true;
       
    68         }
       
    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 
       
    83 
       
    84     public function addContextClass($contextClass)
       
    85     {
       
    86         if (!class_exists($contextClass)) {
       
    87             require_once 'Zend/Loader.php';
       
    88             Zend_Loader::loadClass($contextClass);
       
    89         }
       
    90         $reflectionContextClass = new ReflectionClass($contextClass);
       
    91         if ($reflectionContextClass->isInstantiable()) {
       
    92             $context = new $contextClass();
       
    93             return $this->addContext($context);
       
    94         }
       
    95         return $this;
       
    96     }
       
    97 
       
    98     /**
       
    99      * Enter description here...
       
   100      *
       
   101      * @param Zend_Tool_Project_Context_Interface $context
       
   102      * @return Zend_Tool_Project_Context_Repository
       
   103      */
       
   104     public function addContext(Zend_Tool_Project_Context_Interface $context)
       
   105     {
       
   106         $isSystem       = ($context instanceof Zend_Tool_Project_Context_System_Interface);
       
   107         $isTopLevel     = ($context instanceof Zend_Tool_Project_Context_System_TopLevelRestrictable);
       
   108         $isOverwritable = !($context instanceof Zend_Tool_Project_Context_System_NotOverwritable);
       
   109 
       
   110         $index = (count($this->_contexts)) ? max(array_keys($this->_contexts)) + 1 : 1;
       
   111 
       
   112         $normalName = $this->_normalizeName($context->getName());
       
   113 
       
   114         if (isset($this->_shortContextNames[$normalName]) && ($this->_contexts[$this->_shortContextNames[$normalName]]['isOverwritable'] === false) ) {
       
   115             require_once 'Zend/Tool/Project/Context/Exception.php';
       
   116             throw new Zend_Tool_Project_Context_Exception('Context ' . $context->getName() . ' is not overwriteable.');
       
   117         }
       
   118 
       
   119         $this->_shortContextNames[$normalName] = $index;
       
   120         $this->_contexts[$index] = array(
       
   121             'isTopLevel'     => $isTopLevel,
       
   122             'isSystem'       => $isSystem,
       
   123             'isOverwritable' => $isOverwritable,
       
   124             'normalName'     => $normalName,
       
   125             'context'        => $context
       
   126             );
       
   127 
       
   128         return $this;
       
   129     }
       
   130 
       
   131     public function getContext($name)
       
   132     {
       
   133         if (!$this->hasContext($name)) {
       
   134             require_once 'Zend/Tool/Project/Context/Exception.php';
       
   135             throw new Zend_Tool_Project_Context_Exception('Context by name ' . $name . ' does not exist in the registry.');
       
   136         }
       
   137 
       
   138         $name = $this->_normalizeName($name);
       
   139         return clone $this->_contexts[$this->_shortContextNames[$name]]['context'];
       
   140     }
       
   141 
       
   142     public function hasContext($name)
       
   143     {
       
   144         $name = $this->_normalizeName($name);
       
   145         return (isset($this->_shortContextNames[$name]) ? true : false);
       
   146     }
       
   147 
       
   148     public function isSystemContext($name)
       
   149     {
       
   150         if (!$this->hasContext($name)) {
       
   151             return false;
       
   152         }
       
   153 
       
   154         $name = $this->_normalizeName($name);
       
   155         $index = $this->_shortContextNames[$name];
       
   156         return $this->_contexts[$index]['isSystemContext'];
       
   157     }
       
   158 
       
   159     public function isTopLevelContext($name)
       
   160     {
       
   161         if (!$this->hasContext($name)) {
       
   162             return false;
       
   163         }
       
   164         $name = $this->_normalizeName($name);
       
   165         $index = $this->_shortContextNames[$name];
       
   166         return $this->_contexts[$index]['isTopLevel'];
       
   167     }
       
   168 
       
   169     public function isOverwritableContext($name)
       
   170     {
       
   171         if (!$this->hasContext($name)) {
       
   172             return false;
       
   173         }
       
   174         $name = $this->_normalizeName($name);
       
   175         $index = $this->_shortContextNames[$name];
       
   176         return $this->_contexts[$index]['isOverwritable'];
       
   177     }
       
   178 
       
   179     public function count()
       
   180     {
       
   181         return count($this->_contexts);
       
   182     }
       
   183 
       
   184     protected function _normalizeName($name)
       
   185     {
       
   186         return strtolower($name);
       
   187     }
       
   188 
       
   189 }