web/lib/Zend/Application/Resource/ResourceAbstract.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_Application
       
    17  * @subpackage Resource
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: ResourceAbstract.php 23384 2010-11-19 00:00:29Z ramon $
       
    21  */
       
    22 
       
    23 /**
       
    24  * @see Zend_Application_Resource_Resource
       
    25  */
       
    26 require_once 'Zend/Application/Resource/Resource.php';
       
    27 
       
    28 /**
       
    29  * Abstract class for bootstrap resources
       
    30  *
       
    31  * @uses       Zend_Application_Resource_Resource
       
    32  * @category   Zend
       
    33  * @package    Zend_Application
       
    34  * @subpackage Resource
       
    35  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    36  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    37  */
       
    38 abstract class Zend_Application_Resource_ResourceAbstract implements Zend_Application_Resource_Resource
       
    39 {
       
    40     /**
       
    41      * Parent bootstrap
       
    42      *
       
    43      * @var Zend_Application_Bootstrap_Bootstrapper
       
    44      */
       
    45     protected $_bootstrap;
       
    46 
       
    47     /**
       
    48      * Options for the resource
       
    49      *
       
    50      * @var array
       
    51      */
       
    52     protected $_options = array();
       
    53 
       
    54     /**
       
    55      * Option keys to skip when calling setOptions()
       
    56      *
       
    57      * @var array
       
    58      */
       
    59     protected $_skipOptions = array(
       
    60         'options',
       
    61         'config',
       
    62     );
       
    63 
       
    64     /**
       
    65      * Create a instance with options
       
    66      *
       
    67      * @param mixed $options
       
    68      */
       
    69     public function __construct($options = null)
       
    70     {
       
    71         if (is_array($options)) {
       
    72             $this->setOptions($options);
       
    73         } else if ($options instanceof Zend_Config) {
       
    74             $this->setOptions($options->toArray());
       
    75         }
       
    76     }
       
    77 
       
    78     /**
       
    79      * Set options from array
       
    80      *
       
    81      * @param  array $options Configuration for resource
       
    82      * @return Zend_Application_Resource_ResourceAbstract
       
    83      */
       
    84     public function setOptions(array $options)
       
    85     {
       
    86         if (array_key_exists('bootstrap', $options)) {
       
    87             $this->setBootstrap($options['bootstrap']);
       
    88             unset($options['bootstrap']);
       
    89         }
       
    90 
       
    91         foreach ($options as $key => $value) {
       
    92             if (in_array(strtolower($key), $this->_skipOptions)) {
       
    93                 continue;
       
    94             }
       
    95 
       
    96             $method = 'set' . strtolower($key);
       
    97             if (method_exists($this, $method)) {
       
    98                 $this->$method($value);
       
    99             }
       
   100         }
       
   101 
       
   102         $this->_options = $this->mergeOptions($this->_options, $options);
       
   103 
       
   104         return $this;
       
   105     }
       
   106 
       
   107     /**
       
   108      * Retrieve resource options
       
   109      *
       
   110      * @return array
       
   111      */
       
   112     public function getOptions()
       
   113     {
       
   114         return $this->_options;
       
   115     }
       
   116 
       
   117     /**
       
   118      * Merge options recursively
       
   119      *
       
   120      * @param  array $array1
       
   121      * @param  mixed $array2
       
   122      * @return array
       
   123      */
       
   124     public function mergeOptions(array $array1, $array2 = null)
       
   125     {
       
   126         if (is_array($array2)) {
       
   127             foreach ($array2 as $key => $val) {
       
   128                 if (is_array($array2[$key])) {
       
   129                     $array1[$key] = (array_key_exists($key, $array1) && is_array($array1[$key]))
       
   130                                   ? $this->mergeOptions($array1[$key], $array2[$key])
       
   131                                   : $array2[$key];
       
   132                 } else {
       
   133                     $array1[$key] = $val;
       
   134                 }
       
   135             }
       
   136         }
       
   137         return $array1;
       
   138     }
       
   139 
       
   140     /**
       
   141      * Set the bootstrap to which the resource is attached
       
   142      *
       
   143      * @param  Zend_Application_Bootstrap_Bootstrapper $bootstrap
       
   144      * @return Zend_Application_Resource_Resource
       
   145      */
       
   146     public function setBootstrap(Zend_Application_Bootstrap_Bootstrapper $bootstrap)
       
   147     {
       
   148         $this->_bootstrap = $bootstrap;
       
   149         return $this;
       
   150     }
       
   151 
       
   152     /**
       
   153      * Retrieve the bootstrap to which the resource is attached
       
   154      *
       
   155      * @return null|Zend_Application_Bootstrap_Bootstrapper
       
   156      */
       
   157     public function getBootstrap()
       
   158     {
       
   159         return $this->_bootstrap;
       
   160     }
       
   161 }