Db.php 4.7 KB

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