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_ResourceAbstract
0025  */
0026 // require_once 'Zend/Application/Resource/ResourceAbstract.php';
0027 
0028 /**
0029  * Resource for creating database adapter
0030  *
0031  * @uses       Zend_Application_Resource_ResourceAbstract
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 class Zend_Application_Resource_Db extends Zend_Application_Resource_ResourceAbstract
0039 {
0040     /**
0041      * Adapter to use
0042      *
0043      * @var string
0044      */
0045     protected $_adapter = null;
0046 
0047     /**
0048      * @var Zend_Db_Adapter_Abstract
0049      */
0050     protected $_db;
0051 
0052     /**
0053      * Parameters to use
0054      *
0055      * @var array
0056      */
0057     protected $_params = array();
0058 
0059     /**
0060      * Wether to register the created adapter as default table adapter
0061      *
0062      * @var boolean
0063      */
0064     protected $_isDefaultTableAdapter = true;
0065 
0066     /**
0067      * Set the adapter
0068      *
0069      * @param  string $adapter
0070      * @return Zend_Application_Resource_Db
0071      */
0072     public function setAdapter($adapter)
0073     {
0074         $this->_adapter = $adapter;
0075         return $this;
0076     }
0077 
0078     /**
0079      * Adapter type to use
0080      *
0081      * @return string
0082      */
0083     public function getAdapter()
0084     {
0085         return $this->_adapter;
0086     }
0087 
0088     /**
0089      * Set the adapter params
0090      *
0091      * @param array $params
0092      * @return Zend_Application_Resource_Db
0093      */
0094     public function setParams(array $params)
0095     {
0096         $this->_params = $params;
0097         return $this;
0098     }
0099 
0100     /**
0101      * Adapter parameters
0102      *
0103      * @return array
0104      */
0105     public function getParams()
0106     {
0107         return $this->_params;
0108     }
0109 
0110     /**
0111      * Set whether to use this as default table adapter
0112      *
0113      * @param bool $isDefaultTableAdapter
0114      * @return Zend_Application_Resource_Db
0115      */
0116     public function setIsDefaultTableAdapter($isDefaultTableAdapter)
0117     {
0118         $this->_isDefaultTableAdapter = $isDefaultTableAdapter;
0119         return $this;
0120     }
0121 
0122     /**
0123      * Is this adapter the default table adapter?
0124      *
0125      * @return bool
0126      */
0127     public function isDefaultTableAdapter()
0128     {
0129         return $this->_isDefaultTableAdapter;
0130     }
0131 
0132     /**
0133      * Retrieve initialized DB connection
0134      *
0135      * @return null|Zend_Db_Adapter_Abstract
0136      */
0137     public function getDbAdapter()
0138     {
0139         if ((null === $this->_db)
0140             && (null !== ($adapter = $this->getAdapter()))
0141         ) {
0142             $this->_db = Zend_Db::factory($adapter, $this->getParams());
0143 
0144             if ($this->_db instanceof Zend_Db_Adapter_Abstract 
0145                 && $this->isDefaultTableAdapter()
0146             ) {
0147                 Zend_Db_Table::setDefaultAdapter($this->_db);
0148             }
0149         }
0150         return $this->_db;
0151     }
0152 
0153     /**
0154      * Defined by Zend_Application_Resource_Resource
0155      *
0156      * @return Zend_Db_Adapter_Abstract|null
0157      */
0158     public function init()
0159     {
0160         if (null !== ($db = $this->getDbAdapter())) {
0161             return $db;
0162         }
0163 
0164         return null;
0165     }
0166 
0167     /**
0168      * Set the default metadata cache
0169      *
0170      * @param string|Zend_Cache_Core $cache
0171      * @return Zend_Application_Resource_Db
0172      */
0173     public function setDefaultMetadataCache($cache)
0174     {
0175         $metadataCache = null;
0176 
0177         if (is_string($cache)) {
0178             $bootstrap = $this->getBootstrap();
0179             if ($bootstrap instanceof Zend_Application_Bootstrap_ResourceBootstrapper
0180                 && $bootstrap->hasPluginResource('CacheManager')
0181             ) {
0182                 $cacheManager = $bootstrap->bootstrap('CacheManager')
0183                     ->getResource('CacheManager');
0184                 if (null !== $cacheManager && $cacheManager->hasCache($cache)) {
0185                     $metadataCache = $cacheManager->getCache($cache);
0186                 }
0187             }
0188         } else if ($cache instanceof Zend_Cache_Core) {
0189             $metadataCache = $cache;
0190         }
0191 
0192         if ($metadataCache instanceof Zend_Cache_Core) {
0193             Zend_Db_Table::setDefaultMetadataCache($metadataCache);
0194         }
0195 
0196         return $this;
0197     }
0198 }