web/Zend/View/Helper/Placeholder/Registry.php
changeset 0 4eba9c11703f
equal deleted inserted replaced
-1:000000000000 0:4eba9c11703f
       
     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_View
       
    17  * @subpackage Helper
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @version    $Id: Registry.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    20  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    21  */
       
    22 
       
    23 /** Zend_Registry */
       
    24 require_once 'Zend/Registry.php';
       
    25 
       
    26 /** Zend_View_Helper_Placeholder_Container_Abstract */
       
    27 require_once 'Zend/View/Helper/Placeholder/Container/Abstract.php';
       
    28 
       
    29 /** Zend_View_Helper_Placeholder_Container */
       
    30 require_once 'Zend/View/Helper/Placeholder/Container.php';
       
    31 
       
    32 /**
       
    33  * Registry for placeholder containers
       
    34  *
       
    35  * @package    Zend_View
       
    36  * @subpackage Helper
       
    37  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    38  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    39  */
       
    40 class Zend_View_Helper_Placeholder_Registry
       
    41 {
       
    42     /**
       
    43      * Zend_Registry key under which placeholder registry exists
       
    44      * @const string
       
    45      */
       
    46     const REGISTRY_KEY = 'Zend_View_Helper_Placeholder_Registry';
       
    47 
       
    48     /**
       
    49      * Default container class
       
    50      * @var string
       
    51      */
       
    52     protected $_containerClass = 'Zend_View_Helper_Placeholder_Container';
       
    53 
       
    54     /**
       
    55      * Placeholder containers
       
    56      * @var array
       
    57      */
       
    58     protected $_items = array();
       
    59 
       
    60     /**
       
    61      * Retrieve or create registry instnace
       
    62      *
       
    63      * @return void
       
    64      */
       
    65     public static function getRegistry()
       
    66     {
       
    67         if (Zend_Registry::isRegistered(self::REGISTRY_KEY)) {
       
    68             $registry = Zend_Registry::get(self::REGISTRY_KEY);
       
    69         } else {
       
    70             $registry = new self();
       
    71             Zend_Registry::set(self::REGISTRY_KEY, $registry);
       
    72         }
       
    73 
       
    74         return $registry;
       
    75     }
       
    76 
       
    77     /**
       
    78      * createContainer
       
    79      *
       
    80      * @param  string $key
       
    81      * @param  array $value
       
    82      * @return Zend_View_Helper_Placeholder_Container_Abstract
       
    83      */
       
    84     public function createContainer($key, array $value = array())
       
    85     {
       
    86         $key = (string) $key;
       
    87 
       
    88         $this->_items[$key] = new $this->_containerClass(array());
       
    89         return $this->_items[$key];
       
    90     }
       
    91 
       
    92     /**
       
    93      * Retrieve a placeholder container
       
    94      *
       
    95      * @param  string $key
       
    96      * @return Zend_View_Helper_Placeholder_Container_Abstract
       
    97      */
       
    98     public function getContainer($key)
       
    99     {
       
   100         $key = (string) $key;
       
   101         if (isset($this->_items[$key])) {
       
   102             return $this->_items[$key];
       
   103         }
       
   104 
       
   105         $container = $this->createContainer($key);
       
   106 
       
   107         return $container;
       
   108     }
       
   109 
       
   110     /**
       
   111      * Does a particular container exist?
       
   112      *
       
   113      * @param  string $key
       
   114      * @return bool
       
   115      */
       
   116     public function containerExists($key)
       
   117     {
       
   118         $key = (string) $key;
       
   119         $return =  array_key_exists($key, $this->_items);
       
   120         return $return;
       
   121     }
       
   122 
       
   123     /**
       
   124      * Set the container for an item in the registry
       
   125      *
       
   126      * @param  string $key
       
   127      * @param  Zend_View_Placeholder_Container_Abstract $container
       
   128      * @return Zend_View_Placeholder_Registry
       
   129      */
       
   130     public function setContainer($key, Zend_View_Helper_Placeholder_Container_Abstract $container)
       
   131     {
       
   132         $key = (string) $key;
       
   133         $this->_items[$key] = $container;
       
   134         return $this;
       
   135     }
       
   136 
       
   137     /**
       
   138      * Delete a container
       
   139      *
       
   140      * @param  string $key
       
   141      * @return bool
       
   142      */
       
   143     public function deleteContainer($key)
       
   144     {
       
   145         $key = (string) $key;
       
   146         if (isset($this->_items[$key])) {
       
   147             unset($this->_items[$key]);
       
   148             return true;
       
   149         }
       
   150 
       
   151         return false;
       
   152     }
       
   153 
       
   154     /**
       
   155      * Set the container class to use
       
   156      *
       
   157      * @param  string $name
       
   158      * @return Zend_View_Helper_Placeholder_Registry
       
   159      */
       
   160     public function setContainerClass($name)
       
   161     {
       
   162         if (!class_exists($name)) {
       
   163             require_once 'Zend/Loader.php';
       
   164             Zend_Loader::loadClass($name);
       
   165         }
       
   166 
       
   167         $reflection = new ReflectionClass($name);
       
   168         if (!$reflection->isSubclassOf(new ReflectionClass('Zend_View_Helper_Placeholder_Container_Abstract'))) {
       
   169             require_once 'Zend/View/Helper/Placeholder/Registry/Exception.php';
       
   170             $e = new Zend_View_Helper_Placeholder_Registry_Exception('Invalid Container class specified');
       
   171             $e->setView($this->view);
       
   172             throw $e;
       
   173         }
       
   174 
       
   175         $this->_containerClass = $name;
       
   176         return $this;
       
   177     }
       
   178 
       
   179     /**
       
   180      * Retrieve the container class
       
   181      *
       
   182      * @return string
       
   183      */
       
   184     public function getContainerClass()
       
   185     {
       
   186         return $this->_containerClass;
       
   187     }
       
   188 }