File indexing completed on 2024-06-16 05:29:52

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 // require_once 'Zend/Application/Resource/ResourceAbstract.php';
0024 
0025 // require_once 'Zend/Db/Table.php';
0026 
0027 /**
0028  */
0029 
0030 /**
0031  * Cache Manager resource
0032  *
0033  * Example configuration:
0034  * <pre>
0035  *   resources.multidb.defaultMetadataCache = "database"
0036  *
0037  *   resources.multidb.db1.adapter = "pdo_mysql"
0038  *   resources.multidb.db1.host = "localhost"
0039  *   resources.multidb.db1.username = "webuser"
0040  *   resources.multidb.db1.password = "XXXX"
0041  *   resources.multidb.db1.dbname = "db1"
0042  *   resources.multidb.db1.default = true
0043  *
0044  *   resources.multidb.db2.adapter = "pdo_pgsql"
0045  *   resources.multidb.db2.host = "example.com"
0046  *   resources.multidb.db2.username = "dba"
0047  *   resources.multidb.db2.password = "notthatpublic"
0048  *   resources.multidb.db2.dbname = "db2"
0049  * </pre>
0050  *
0051  * @category   Zend
0052  * @package    Zend_Application
0053  * @subpackage Resource
0054  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0055  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0056  */
0057 class Zend_Application_Resource_Multidb extends Zend_Application_Resource_ResourceAbstract
0058 {
0059     /**
0060      * Associative array containing all configured db's
0061      *
0062      * @var array
0063      */
0064     protected $_dbs = array();
0065 
0066     /**
0067      * An instance of the default db, if set
0068      *
0069      * @var null|Zend_Db_Adapter_Abstract
0070      */
0071     protected $_defaultDb;
0072 
0073     /**
0074      * Initialize the Database Connections (instances of Zend_Db_Table_Abstract)
0075      *
0076      * @return Zend_Application_Resource_Multidb
0077      */
0078     public function init()
0079     {
0080         $options = $this->getOptions();
0081 
0082         if (isset($options['defaultMetadataCache'])) {
0083             $this->_setDefaultMetadataCache($options['defaultMetadataCache']);
0084             unset($options['defaultMetadataCache']);
0085         }
0086 
0087         foreach ($options as $id => $params) {
0088             $adapter = $params['adapter'];
0089             $default = (int) (
0090                 isset($params['isDefaultTableAdapter']) && $params['isDefaultTableAdapter']
0091                 || isset($params['default']) && $params['default']
0092             );
0093             unset(
0094                 $params['adapter'],
0095                 $params['default'],
0096                 $params['isDefaultTableAdapter']
0097             );
0098 
0099             $this->_dbs[$id] = Zend_Db::factory($adapter, $params);
0100 
0101             if ($default) {
0102                 $this->_setDefault($this->_dbs[$id]);
0103             }
0104         }
0105 
0106         return $this;
0107     }
0108 
0109     /**
0110      * Determine if the given db(identifier) is the default db.
0111      *
0112      * @param  string|Zend_Db_Adapter_Abstract $db The db to determine whether it's set as default
0113      * @return boolean True if the given parameter is configured as default. False otherwise
0114      */
0115     public function isDefault($db)
0116     {
0117         if (!$db instanceof Zend_Db_Adapter_Abstract) {
0118             $db = $this->getDb($db);
0119         }
0120 
0121         return $db === $this->_defaultDb;
0122     }
0123 
0124     /**
0125      * Retrieve the specified database connection
0126      *
0127      * @param  null|string|Zend_Db_Adapter_Abstract $db The adapter to retrieve.
0128      *                                               Null to retrieve the default connection
0129      * @return Zend_Db_Adapter_Abstract
0130      * @throws Zend_Application_Resource_Exception if the given parameter could not be found
0131      */
0132     public function getDb($db = null)
0133     {
0134         if ($db === null) {
0135             return $this->getDefaultDb();
0136         }
0137 
0138         if (isset($this->_dbs[$db])) {
0139             return $this->_dbs[$db];
0140         }
0141 
0142         throw new Zend_Application_Resource_Exception(
0143             'A DB adapter was tried to retrieve, but was not configured'
0144         );
0145     }
0146 
0147     /**
0148      * Get the default db connection
0149      *
0150      * @param  boolean $justPickOne If true, a random (the first one in the stack)
0151      *                           connection is returned if no default was set.
0152      *                           If false, null is returned if no default was set.
0153      * @return null|Zend_Db_Adapter_Abstract
0154      */
0155     public function getDefaultDb($justPickOne = true)
0156     {
0157         if ($this->_defaultDb !== null) {
0158             return $this->_defaultDb;
0159         }
0160 
0161         if ($justPickOne) {
0162             return reset($this->_dbs); // Return first db in db pool
0163         }
0164 
0165         return null;
0166     }
0167 
0168     /**
0169      * Set the default db adapter
0170      *
0171      * @var Zend_Db_Adapter_Abstract $adapter Adapter to set as default
0172      */
0173     protected function _setDefault(Zend_Db_Adapter_Abstract $adapter)
0174     {
0175         Zend_Db_Table::setDefaultAdapter($adapter);
0176         $this->_defaultDb = $adapter;
0177     }
0178 
0179    /**
0180      * Set the default metadata cache
0181      *
0182      * @param string|Zend_Cache_Core $cache
0183      * @return Zend_Application_Resource_Multidb
0184      */
0185     protected function _setDefaultMetadataCache($cache)
0186     {
0187         $metadataCache = null;
0188 
0189         if (is_string($cache)) {
0190             $bootstrap = $this->getBootstrap();
0191             if ($bootstrap instanceof Zend_Application_Bootstrap_ResourceBootstrapper &&
0192                 $bootstrap->hasPluginResource('CacheManager')
0193             ) {
0194                 $cacheManager = $bootstrap->bootstrap('CacheManager')
0195                     ->getResource('CacheManager');
0196                 if (null !== $cacheManager && $cacheManager->hasCache($cache)) {
0197                     $metadataCache = $cacheManager->getCache($cache);
0198                 }
0199             }
0200         } else if ($cache instanceof Zend_Cache_Core) {
0201             $metadataCache = $cache;
0202         }
0203 
0204         if ($metadataCache instanceof Zend_Cache_Core) {
0205             Zend_Db_Table::setDefaultMetadataCache($metadataCache);
0206         }
0207 
0208         return $this;
0209     }
0210 }