File indexing completed on 2024-12-29 05:28:09
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_Tool 0017 * @subpackage Framework 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 /** 0024 * @see Zend_Tool_Project_Provider_Abstract 0025 */ 0026 // require_once 'Zend/Tool/Project/Provider/Abstract.php'; 0027 0028 /** 0029 * @see Zend_Tool_Framework_Provider_Interactable 0030 */ 0031 // require_once 'Zend/Tool/Framework/Provider/Interactable.php'; 0032 0033 /** 0034 * @category Zend 0035 * @package Zend_Tool 0036 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0037 * @license http://framework.zend.com/license/new-bsd New BSD License 0038 */ 0039 class Zend_Tool_Project_Provider_DbAdapter 0040 extends Zend_Tool_Project_Provider_Abstract 0041 implements Zend_Tool_Framework_Provider_Interactable, Zend_Tool_Framework_Provider_Pretendable 0042 { 0043 0044 protected $_appConfigFilePath = null; 0045 0046 protected $_config = null; 0047 0048 protected $_sectionName = 'production'; 0049 0050 public function configure($dsn = null, /* $interactivelyPrompt = false, */ $sectionName = 'production') 0051 { 0052 $profile = $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION); 0053 0054 $appConfigFileResource = $profile->search('applicationConfigFile'); 0055 0056 if ($appConfigFileResource == false) { 0057 throw new Zend_Tool_Project_Exception('A project with an application config file is required to use this provider.'); 0058 } 0059 0060 $this->_appConfigFilePath = $appConfigFileResource->getPath(); 0061 0062 $this->_config = new Zend_Config_Ini($this->_appConfigFilePath, null, array('skipExtends' => true, 'allowModifications' => true)); 0063 0064 if ($sectionName != 'production') { 0065 $this->_sectionName = $sectionName; 0066 } 0067 0068 if (!isset($this->_config->{$this->_sectionName})) { 0069 throw new Zend_Tool_Project_Exception('The config does not have a ' . $this->_sectionName . ' section.'); 0070 } 0071 0072 if (isset($this->_config->{$this->_sectionName}->resources->db)) { 0073 throw new Zend_Tool_Project_Exception('The config already has a db resource configured in section ' . $this->_sectionName . '.'); 0074 } 0075 0076 if ($dsn) { 0077 $this->_configureViaDSN($dsn); 0078 //} elseif ($interactivelyPrompt) { 0079 // $this->_promptForConfig(); 0080 } else { 0081 $this->_registry->getResponse()->appendContent('Nothing to do!'); 0082 } 0083 0084 0085 } 0086 0087 protected function _configureViaDSN($dsn) 0088 { 0089 $dsnVars = array(); 0090 0091 if (strpos($dsn, '=') === false) { 0092 throw new Zend_Tool_Project_Provider_Exception('At least one name value pair is expected, typcially ' 0093 . 'in the format of "adapter=Mysqli&username=uname&password=mypass&dbname=mydb"' 0094 ); 0095 } 0096 0097 parse_str($dsn, $dsnVars); 0098 0099 // parse_str suffers when magic_quotes is enabled 0100 if (get_magic_quotes_gpc()) { 0101 array_walk_recursive($dsnVars, array($this, '_cleanMagicQuotesInValues')); 0102 } 0103 0104 $dbConfigValues = array('resources' => array('db' => null)); 0105 0106 if (isset($dsnVars['adapter'])) { 0107 $dbConfigValues['resources']['db']['adapter'] = $dsnVars['adapter']; 0108 unset($dsnVars['adapter']); 0109 } 0110 0111 $dbConfigValues['resources']['db']['params'] = $dsnVars; 0112 0113 $isPretend = $this->_registry->getRequest()->isPretend(); 0114 0115 // get the config resource 0116 $applicationConfig = $this->_loadedProfile->search('ApplicationConfigFile'); 0117 $applicationConfig->addItem($dbConfigValues, $this->_sectionName, null); 0118 0119 $response = $this->_registry->getResponse(); 0120 0121 if ($isPretend) { 0122 $response->appendContent('A db configuration for the ' . $this->_sectionName 0123 . ' section would be written to the application config file with the following contents: ' 0124 ); 0125 $response->appendContent($applicationConfig->getContents()); 0126 } else { 0127 $applicationConfig->create(); 0128 $response->appendContent('A db configuration for the ' . $this->_sectionName 0129 . ' section has been written to the application config file.' 0130 ); 0131 } 0132 } 0133 0134 protected function _cleanMagicQuotesInValues(&$value, $key) 0135 { 0136 $value = stripslashes($value); 0137 } 0138 0139 }