File indexing completed on 2024-12-22 05:36:28

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_Application
0017  * @subpackage Resource
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /**
0024  * @see Zend_Application_Resource_Resource
0025  */
0026 // require_once 'Zend/Application/Resource/Resource.php';
0027 
0028 /**
0029  * Abstract class for bootstrap resources
0030  *
0031  * @uses       Zend_Application_Resource_Resource
0032  * @category   Zend
0033  * @package    Zend_Application
0034  * @subpackage Resource
0035  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0036  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0037  */
0038 abstract class Zend_Application_Resource_ResourceAbstract implements Zend_Application_Resource_Resource
0039 {
0040     /**
0041      * Parent bootstrap
0042      *
0043      * @var Zend_Application_Bootstrap_Bootstrapper
0044      */
0045     protected $_bootstrap;
0046 
0047     /**
0048      * Options for the resource
0049      *
0050      * @var array
0051      */
0052     protected $_options = array();
0053 
0054     /**
0055      * Option keys to skip when calling setOptions()
0056      *
0057      * @var array
0058      */
0059     protected $_skipOptions = array(
0060         'options',
0061         'config',
0062     );
0063 
0064     /**
0065      * Create a instance with options
0066      *
0067      * @param mixed $options
0068      */
0069     public function __construct($options = null)
0070     {
0071         if (is_array($options)) {
0072             $this->setOptions($options);
0073         } else if ($options instanceof Zend_Config) {
0074             $this->setOptions($options->toArray());
0075         }
0076     }
0077 
0078     /**
0079      * Set options from array
0080      *
0081      * @param  array $options Configuration for resource
0082      * @return Zend_Application_Resource_ResourceAbstract
0083      */
0084     public function setOptions(array $options)
0085     {
0086         if (array_key_exists('bootstrap', $options)) {
0087             $this->setBootstrap($options['bootstrap']);
0088             unset($options['bootstrap']);
0089         }
0090 
0091         foreach ($options as $key => $value) {
0092             if (in_array(strtolower($key), $this->_skipOptions)) {
0093                 continue;
0094             }
0095 
0096             $method = 'set' . strtolower($key);
0097             if (method_exists($this, $method)) {
0098                 $this->$method($value);
0099             }
0100         }
0101 
0102         $this->_options = $this->mergeOptions($this->_options, $options);
0103 
0104         return $this;
0105     }
0106 
0107     /**
0108      * Retrieve resource options
0109      *
0110      * @return array
0111      */
0112     public function getOptions()
0113     {
0114         return $this->_options;
0115     }
0116 
0117     /**
0118      * Merge options recursively
0119      *
0120      * @param  array $array1
0121      * @param  mixed $array2
0122      * @return array
0123      */
0124     public function mergeOptions(array $array1, $array2 = null)
0125     {
0126         if (is_array($array2)) {
0127             foreach ($array2 as $key => $val) {
0128                 if (is_array($array2[$key])) {
0129                     $array1[$key] = (array_key_exists($key, $array1) && is_array($array1[$key]))
0130                                   ? $this->mergeOptions($array1[$key], $array2[$key])
0131                                   : $array2[$key];
0132                 } else {
0133                     $array1[$key] = $val;
0134                 }
0135             }
0136         }
0137         return $array1;
0138     }
0139 
0140     /**
0141      * Set the bootstrap to which the resource is attached
0142      *
0143      * @param  Zend_Application_Bootstrap_Bootstrapper $bootstrap
0144      * @return Zend_Application_Resource_Resource
0145      */
0146     public function setBootstrap(Zend_Application_Bootstrap_Bootstrapper $bootstrap)
0147     {
0148         $this->_bootstrap = $bootstrap;
0149         return $this;
0150     }
0151 
0152     /**
0153      * Retrieve the bootstrap to which the resource is attached
0154      *
0155      * @return null|Zend_Application_Bootstrap_Bootstrapper
0156      */
0157     public function getBootstrap()
0158     {
0159         return $this->_bootstrap;
0160     }
0161 }