/lib/vendor/Zend/Tool/Project/Provider/DbAdapter.php

https://github.com/skoop/Gesichtbuch · PHP · 139 lines · 71 code · 30 blank · 38 comment · 13 complexity · 6558318ff5c2be3806b6ec447ddc6cd5 MD5 · raw file

  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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: DbAdapter.php 6352 2010-08-16 15:02:45Z tpater $
  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-2010 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. $this->_registry->getResponse()->appendContent('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. // parse_str suffers when magic_quotes is enabled
  79. if (get_magic_quotes_gpc()) {
  80. array_walk_recursive($dsnVars, array($this, '_cleanMagicQuotesInValues'));
  81. }
  82. $dbConfigValues = array('resources' => array('db' => null));
  83. if (isset($dsnVars['adapter'])) {
  84. $dbConfigValues['resources']['db']['adapter'] = $dsnVars['adapter'];
  85. unset($dsnVars['adapter']);
  86. }
  87. $dbConfigValues['resources']['db']['params'] = $dsnVars;
  88. $isPretend = $this->_registry->getRequest()->isPretend();
  89. // get the config resource
  90. $applicationConfig = $this->_loadedProfile->search('ApplicationConfigFile');
  91. $applicationConfig->addItem($dbConfigValues, $this->_sectionName, null);
  92. $response = $this->_registry->getResponse();
  93. if ($isPretend) {
  94. $response->appendContent('A db configuration for the ' . $this->_sectionName
  95. . ' section would be written to the application config file with the following contents: '
  96. );
  97. $response->appendContent($applicationConfig->getContents());
  98. } else {
  99. $applicationConfig->create();
  100. $response->appendContent('A db configuration for the ' . $this->_sectionName
  101. . ' section has been written to the application config file.'
  102. );
  103. }
  104. }
  105. protected function _cleanMagicQuotesInValues(&$value, $key)
  106. {
  107. $value = stripslashes($value);
  108. }
  109. }