File indexing completed on 2024-12-29 05:28:12

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_View
0017  * @subpackage Helper
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @version    $Id$
0020  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0021  */
0022 
0023 /** Zend_Registry */
0024 // require_once 'Zend/Registry.php';
0025 
0026 /** Zend_View_Helper_Placeholder_Container_Abstract */
0027 // require_once 'Zend/View/Helper/Placeholder/Container/Abstract.php';
0028 
0029 /** Zend_View_Helper_Placeholder_Container */
0030 // require_once 'Zend/View/Helper/Placeholder/Container.php';
0031 
0032 /**
0033  * Registry for placeholder containers
0034  *
0035  * @package    Zend_View
0036  * @subpackage Helper
0037  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0038  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0039  */
0040 class Zend_View_Helper_Placeholder_Registry
0041 {
0042     /**
0043      * Zend_Registry key under which placeholder registry exists
0044      * @const string
0045      */
0046     const REGISTRY_KEY = 'Zend_View_Helper_Placeholder_Registry';
0047 
0048     /**
0049      * Default container class
0050      * @var string
0051      */
0052     protected $_containerClass = 'Zend_View_Helper_Placeholder_Container';
0053 
0054     /**
0055      * Placeholder containers
0056      * @var array
0057      */
0058     protected $_items = array();
0059 
0060     /**
0061      * Retrieve or create registry instnace
0062      *
0063      * @return void
0064      */
0065     public static function getRegistry()
0066     {
0067         if (Zend_Registry::isRegistered(self::REGISTRY_KEY)) {
0068             $registry = Zend_Registry::get(self::REGISTRY_KEY);
0069         } else {
0070             $registry = new self();
0071             Zend_Registry::set(self::REGISTRY_KEY, $registry);
0072         }
0073 
0074         return $registry;
0075     }
0076 
0077     /**
0078      * createContainer
0079      *
0080      * @param  string $key
0081      * @param  array $value
0082      * @return Zend_View_Helper_Placeholder_Container_Abstract
0083      */
0084     public function createContainer($key, array $value = array())
0085     {
0086         $key = (string) $key;
0087 
0088         $this->_items[$key] = new $this->_containerClass($value);
0089         return $this->_items[$key];
0090     }
0091 
0092     /**
0093      * Retrieve a placeholder container
0094      *
0095      * @param  string $key
0096      * @return Zend_View_Helper_Placeholder_Container_Abstract
0097      */
0098     public function getContainer($key)
0099     {
0100         $key = (string) $key;
0101         if (isset($this->_items[$key])) {
0102             return $this->_items[$key];
0103         }
0104 
0105         $container = $this->createContainer($key);
0106 
0107         return $container;
0108     }
0109 
0110     /**
0111      * Does a particular container exist?
0112      *
0113      * @param  string $key
0114      * @return bool
0115      */
0116     public function containerExists($key)
0117     {
0118         $key = (string) $key;
0119         $return =  array_key_exists($key, $this->_items);
0120         return $return;
0121     }
0122 
0123     /**
0124      * Set the container for an item in the registry
0125      *
0126      * @param  string $key
0127      * @param  Zend_View_Placeholder_Container_Abstract $container
0128      * @return Zend_View_Placeholder_Registry
0129      */
0130     public function setContainer($key, Zend_View_Helper_Placeholder_Container_Abstract $container)
0131     {
0132         $key = (string) $key;
0133         $this->_items[$key] = $container;
0134         return $this;
0135     }
0136 
0137     /**
0138      * Delete a container
0139      *
0140      * @param  string $key
0141      * @return bool
0142      */
0143     public function deleteContainer($key)
0144     {
0145         $key = (string) $key;
0146         if (isset($this->_items[$key])) {
0147             unset($this->_items[$key]);
0148             return true;
0149         }
0150 
0151         return false;
0152     }
0153 
0154     /**
0155      * Set the container class to use
0156      *
0157      * @param  string $name
0158      * @return Zend_View_Helper_Placeholder_Registry
0159      */
0160     public function setContainerClass($name)
0161     {
0162         if (!class_exists($name)) {
0163             // require_once 'Zend/Loader.php';
0164             Zend_Loader::loadClass($name);
0165         }
0166 
0167         $reflection = new ReflectionClass($name);
0168         if (!$reflection->isSubclassOf(new ReflectionClass('Zend_View_Helper_Placeholder_Container_Abstract'))) {
0169             // require_once 'Zend/View/Helper/Placeholder/Registry/Exception.php';
0170             $e = new Zend_View_Helper_Placeholder_Registry_Exception('Invalid Container class specified');
0171             $e->setView($this->view);
0172             throw $e;
0173         }
0174 
0175         $this->_containerClass = $name;
0176         return $this;
0177     }
0178 
0179     /**
0180      * Retrieve the container class
0181      *
0182      * @return string
0183      */
0184     public function getContainerClass()
0185     {
0186         return $this->_containerClass;
0187     }
0188 }