web/lib/Zend/Application/Resource/Db.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: Db.php 22544 2010-07-10 15:01:37Z freak $
       
    21  */
       
    22 
       
    23 /**
       
    24  * @see Zend_Application_Resource_ResourceAbstract
       
    25  */
       
    26 require_once 'Zend/Application/Resource/ResourceAbstract.php';
       
    27 
       
    28 /**
       
    29  * Resource for creating database adapter
       
    30  *
       
    31  * @uses       Zend_Application_Resource_ResourceAbstract
       
    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 class Zend_Application_Resource_Db extends Zend_Application_Resource_ResourceAbstract
       
    39 {
       
    40     /**
       
    41      * Adapter to use
       
    42      *
       
    43      * @var string
       
    44      */
       
    45     protected $_adapter = null;
       
    46 
       
    47     /**
       
    48      * @var Zend_Db_Adapter_Interface
       
    49      */
       
    50     protected $_db;
       
    51 
       
    52     /**
       
    53      * Parameters to use
       
    54      *
       
    55      * @var array
       
    56      */
       
    57     protected $_params = array();
       
    58 
       
    59     /**
       
    60      * Wether to register the created adapter as default table adapter
       
    61      *
       
    62      * @var boolean
       
    63      */
       
    64     protected $_isDefaultTableAdapter = true;
       
    65 
       
    66     /**
       
    67      * Set the adapter
       
    68      *
       
    69      * @param  $adapter string
       
    70      * @return Zend_Application_Resource_Db
       
    71      */
       
    72     public function setAdapter($adapter)
       
    73     {
       
    74         $this->_adapter = $adapter;
       
    75         return $this;
       
    76     }
       
    77 
       
    78     /**
       
    79      * Adapter type to use
       
    80      *
       
    81      * @return string
       
    82      */
       
    83     public function getAdapter()
       
    84     {
       
    85         return $this->_adapter;
       
    86     }
       
    87 
       
    88     /**
       
    89      * Set the adapter params
       
    90      *
       
    91      * @param  $adapter string
       
    92      * @return Zend_Application_Resource_Db
       
    93      */
       
    94     public function setParams(array $params)
       
    95     {
       
    96         $this->_params = $params;
       
    97         return $this;
       
    98     }
       
    99 
       
   100     /**
       
   101      * Adapter parameters
       
   102      *
       
   103      * @return array
       
   104      */
       
   105     public function getParams()
       
   106     {
       
   107         return $this->_params;
       
   108     }
       
   109 
       
   110     /**
       
   111      * Set whether to use this as default table adapter
       
   112      *
       
   113      * @param  boolean $defaultTableAdapter
       
   114      * @return Zend_Application_Resource_Db
       
   115      */
       
   116     public function setIsDefaultTableAdapter($isDefaultTableAdapter)
       
   117     {
       
   118         $this->_isDefaultTableAdapter = $isDefaultTableAdapter;
       
   119         return $this;
       
   120     }
       
   121 
       
   122     /**
       
   123      * Is this adapter the default table adapter?
       
   124      *
       
   125      * @return void
       
   126      */
       
   127     public function isDefaultTableAdapter()
       
   128     {
       
   129         return $this->_isDefaultTableAdapter;
       
   130     }
       
   131 
       
   132     /**
       
   133      * Retrieve initialized DB connection
       
   134      *
       
   135      * @return null|Zend_Db_Adapter_Interface
       
   136      */
       
   137     public function getDbAdapter()
       
   138     {
       
   139         if ((null === $this->_db)
       
   140             && (null !== ($adapter = $this->getAdapter()))
       
   141         ) {
       
   142             $this->_db = Zend_Db::factory($adapter, $this->getParams());
       
   143         }
       
   144         return $this->_db;
       
   145     }
       
   146 
       
   147     /**
       
   148      * Defined by Zend_Application_Resource_Resource
       
   149      *
       
   150      * @return Zend_Db_Adapter_Abstract|null
       
   151      */
       
   152     public function init()
       
   153     {
       
   154         if (null !== ($db = $this->getDbAdapter())) {
       
   155             if ($this->isDefaultTableAdapter()) {
       
   156                 Zend_Db_Table::setDefaultAdapter($db);
       
   157             }
       
   158             return $db;
       
   159         }
       
   160     }
       
   161 
       
   162     /**
       
   163      * Set the default metadata cache
       
   164      * 
       
   165      * @param string|Zend_Cache_Core $cache
       
   166      * @return Zend_Application_Resource_Db
       
   167      */
       
   168     public function setDefaultMetadataCache($cache)
       
   169     {
       
   170         $metadataCache = null;
       
   171 
       
   172         if (is_string($cache)) {
       
   173             $bootstrap = $this->getBootstrap();
       
   174             if ($bootstrap instanceof Zend_Application_Bootstrap_ResourceBootstrapper
       
   175                 && $bootstrap->hasPluginResource('CacheManager')
       
   176             ) {
       
   177                 $cacheManager = $bootstrap->bootstrap('CacheManager')
       
   178                     ->getResource('CacheManager');
       
   179                 if (null !== $cacheManager && $cacheManager->hasCache($cache)) {
       
   180                     $metadataCache = $cacheManager->getCache($cache);
       
   181                 }
       
   182             }
       
   183         } else if ($cache instanceof Zend_Cache_Core) {
       
   184             $metadataCache = $cache;
       
   185         }
       
   186 
       
   187         if ($metadataCache instanceof Zend_Cache_Core) {
       
   188             Zend_Db_Table::setDefaultMetadataCache($metadataCache);
       
   189         }
       
   190 
       
   191         return $this;
       
   192     }
       
   193 }