DbAdapter.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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_Tool
  17. * @subpackage Framework
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: View.php 18386 2009-09-23 20:44:43Z ralph $
  21. */
  22. /**
  23. * @see Zend_Tool_Project_Provider_Abstract
  24. */
  25. require_once 'Zend/Tool/Project/Provider/Abstract.php';
  26. /**
  27. * @see Zend_Tool_Framework_Provider_Interactable
  28. */
  29. require_once 'Zend/Tool/Framework/Provider/Interactable.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Tool
  33. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Zend_Tool_Project_Provider_DbAdapter
  37. extends Zend_Tool_Project_Provider_Abstract
  38. implements Zend_Tool_Framework_Provider_Interactable, Zend_Tool_Framework_Provider_Pretendable
  39. {
  40. protected $_appConfigFilePath = null;
  41. protected $_config = null;
  42. protected $_sectionName = 'production';
  43. public function configure($dsn = null, $interactivelyPrompt = false, $sectionName = 'production')
  44. {
  45. $profile = $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION);
  46. $appConfigFileResource = $profile->search('applicationConfigFile');
  47. if ($appConfigFileResource == false) {
  48. throw new Zend_Tool_Project_Exception('A project with an application config file is required to use this provider.');
  49. }
  50. $this->_appConfigFilePath = $appConfigFileResource->getPath();
  51. $this->_config = new Zend_Config_Ini($this->_appConfigFilePath, null, array('skipExtends' => true, 'allowModifications' => true));
  52. if ($sectionName != 'production') {
  53. $this->_sectionName = $sectionName;
  54. }
  55. if (!isset($this->_config->{$this->_sectionName})) {
  56. throw new Zend_Tool_Project_Exception('The config does not have a ' . $this->_sectionName . ' section.');
  57. }
  58. if (isset($this->_config->{$this->_sectionName}->resources->db)) {
  59. throw new Zend_Tool_Project_Exception('The config already has a db resource configured in section ' . $this->_sectionName . '.');
  60. }
  61. if ($dsn) {
  62. $this->_configureViaDSN($dsn);
  63. } elseif ($interactivelyPrompt) {
  64. $this->_promptForConfig();
  65. } else {
  66. echo 'Nothing to do!';
  67. }
  68. }
  69. protected function _configureViaDSN($dsn)
  70. {
  71. $dsnVars = array();
  72. if (strpos($dsn, '=') === false) {
  73. throw new Zend_Tool_Project_Provider_Exception('At least one name value pair is expected, typcially '
  74. . 'in the format of "adapter=Mysqli&username=uname&password=mypass&dbname=mydb"'
  75. );
  76. }
  77. parse_str($dsn, $dsnVars);
  78. $dbConfigValues = array();
  79. if (isset($dsnVars['adapter'])) {
  80. $dbConfigValues['adapter'] = $dsnVars['adapter'];
  81. unset($dsnVars['adapter']);
  82. }
  83. $dbConfigValues['params'] = $dsnVars;
  84. $isPretend = $this->_registry->getRequest()->isPretend();
  85. $content = $this->_writeToApplicationConfig($dbConfigValues, $isPretend);
  86. $response = $this->_registry->getResponse();
  87. if ($isPretend) {
  88. $response->appendContent('A db configuration for the ' . $this->_sectionName
  89. . ' would be written to the application config file with the following contents: '
  90. );
  91. $response->appendContent($content);
  92. } else {
  93. $response->appendContent('A db configuration for the ' . $this->_sectionName
  94. . ' has been written to the application config file.'
  95. );
  96. }
  97. }
  98. protected function _promptForConfig()
  99. {
  100. echo '//@todo';
  101. }
  102. protected function _promtForConfigPdoMysql()
  103. {
  104. $r = array(
  105. 'username' => 'Username',
  106. 'password' => 'Password',
  107. 'dbname' => 'Database',
  108. 'driver_options' => array(
  109. )
  110. );
  111. }
  112. protected function _writeToApplicationConfig($configValues, $isPretend = false)
  113. {
  114. $configKeyNames = array('resources', 'db');
  115. $newDbLines = array();
  116. $rii = new RecursiveIteratorIterator(
  117. new RecursiveArrayIterator($configValues),
  118. RecursiveIteratorIterator::SELF_FIRST
  119. );
  120. $lastDepth = 0;
  121. foreach ($rii as $name => $value) {
  122. if ($lastDepth > $rii->getDepth()) {
  123. array_pop($configKeyNames);
  124. }
  125. $lastDepth = $rii->getDepth();
  126. if (is_array($value)) {
  127. array_push($configKeyNames, $name);
  128. } else {
  129. $newDbLines[] = implode('.', $configKeyNames) . '.' . $name . ' = "' . $value . "\"\n";
  130. }
  131. }
  132. $originalLines = file($this->_appConfigFilePath);
  133. $newLines = array();
  134. $insideSection = false;
  135. foreach ($originalLines as $originalLineIndex => $originalLine) {
  136. if ($insideSection === false && preg_match('#^\[' . $this->_sectionName . '#', $originalLine)) {
  137. $insideSection = true;
  138. }
  139. if ($insideSection) {
  140. if ((trim($originalLine) == null) || ($originalLines[$originalLineIndex + 1][0] == '[')) {
  141. foreach ($newDbLines as $newDbLine) {
  142. $newLines[] = $newDbLine;
  143. }
  144. $insideSection = null;
  145. }
  146. }
  147. $newLines[] = $originalLine;
  148. }
  149. $newConfigContents = implode('', $newLines);
  150. if (!$isPretend) {
  151. file_put_contents($this->_appConfigFilePath, $newConfigContents);
  152. }
  153. return $newConfigContents;
  154. }
  155. }