31 require_once 'Zend/Tool/Framework/Provider/Interactable.php'; |
31 require_once 'Zend/Tool/Framework/Provider/Interactable.php'; |
32 |
32 |
33 /** |
33 /** |
34 * @category Zend |
34 * @category Zend |
35 * @package Zend_Tool |
35 * @package Zend_Tool |
36 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
36 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) |
37 * @license http://framework.zend.com/license/new-bsd New BSD License |
37 * @license http://framework.zend.com/license/new-bsd New BSD License |
38 */ |
38 */ |
39 class Zend_Tool_Project_Provider_DbAdapter |
39 class Zend_Tool_Project_Provider_DbAdapter |
40 extends Zend_Tool_Project_Provider_Abstract |
40 extends Zend_Tool_Project_Provider_Abstract |
41 implements Zend_Tool_Framework_Provider_Interactable, Zend_Tool_Framework_Provider_Pretendable |
41 implements Zend_Tool_Framework_Provider_Interactable, Zend_Tool_Framework_Provider_Pretendable |
42 { |
42 { |
43 |
43 |
44 protected $_appConfigFilePath = null; |
44 protected $_appConfigFilePath = null; |
45 |
45 |
46 protected $_config = null; |
46 protected $_config = null; |
47 |
47 |
48 protected $_sectionName = 'production'; |
48 protected $_sectionName = 'production'; |
49 |
49 |
50 public function configure($dsn = null, /* $interactivelyPrompt = false, */ $sectionName = 'production') |
50 public function configure($dsn = null, /* $interactivelyPrompt = false, */ $sectionName = 'production') |
51 { |
51 { |
52 $profile = $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION); |
52 $profile = $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION); |
53 |
53 |
54 $appConfigFileResource = $profile->search('applicationConfigFile'); |
54 $appConfigFileResource = $profile->search('applicationConfigFile'); |
55 |
55 |
56 if ($appConfigFileResource == false) { |
56 if ($appConfigFileResource == false) { |
57 throw new Zend_Tool_Project_Exception('A project with an application config file is required to use this provider.'); |
57 throw new Zend_Tool_Project_Exception('A project with an application config file is required to use this provider.'); |
58 } |
58 } |
59 |
59 |
60 $this->_appConfigFilePath = $appConfigFileResource->getPath(); |
60 $this->_appConfigFilePath = $appConfigFileResource->getPath(); |
61 |
61 |
62 $this->_config = new Zend_Config_Ini($this->_appConfigFilePath, null, array('skipExtends' => true, 'allowModifications' => true)); |
62 $this->_config = new Zend_Config_Ini($this->_appConfigFilePath, null, array('skipExtends' => true, 'allowModifications' => true)); |
63 |
63 |
64 if ($sectionName != 'production') { |
64 if ($sectionName != 'production') { |
65 $this->_sectionName = $sectionName; |
65 $this->_sectionName = $sectionName; |
66 } |
66 } |
67 |
67 |
68 if (!isset($this->_config->{$this->_sectionName})) { |
68 if (!isset($this->_config->{$this->_sectionName})) { |
69 throw new Zend_Tool_Project_Exception('The config does not have a ' . $this->_sectionName . ' section.'); |
69 throw new Zend_Tool_Project_Exception('The config does not have a ' . $this->_sectionName . ' section.'); |
70 } |
70 } |
71 |
71 |
72 if (isset($this->_config->{$this->_sectionName}->resources->db)) { |
72 if (isset($this->_config->{$this->_sectionName}->resources->db)) { |
73 throw new Zend_Tool_Project_Exception('The config already has a db resource configured in section ' . $this->_sectionName . '.'); |
73 throw new Zend_Tool_Project_Exception('The config already has a db resource configured in section ' . $this->_sectionName . '.'); |
74 } |
74 } |
75 |
75 |
76 if ($dsn) { |
76 if ($dsn) { |
77 $this->_configureViaDSN($dsn); |
77 $this->_configureViaDSN($dsn); |
78 //} elseif ($interactivelyPrompt) { |
78 //} elseif ($interactivelyPrompt) { |
79 // $this->_promptForConfig(); |
79 // $this->_promptForConfig(); |
80 } else { |
80 } else { |
81 $this->_registry->getResponse()->appendContent('Nothing to do!'); |
81 $this->_registry->getResponse()->appendContent('Nothing to do!'); |
82 } |
82 } |
83 |
83 |
84 |
84 |
85 } |
85 } |
86 |
86 |
87 protected function _configureViaDSN($dsn) |
87 protected function _configureViaDSN($dsn) |
88 { |
88 { |
89 $dsnVars = array(); |
89 $dsnVars = array(); |
90 |
90 |
91 if (strpos($dsn, '=') === false) { |
91 if (strpos($dsn, '=') === false) { |
92 throw new Zend_Tool_Project_Provider_Exception('At least one name value pair is expected, typcially ' |
92 throw new Zend_Tool_Project_Provider_Exception('At least one name value pair is expected, typcially ' |
93 . 'in the format of "adapter=Mysqli&username=uname&password=mypass&dbname=mydb"' |
93 . 'in the format of "adapter=Mysqli&username=uname&password=mypass&dbname=mydb"' |
94 ); |
94 ); |
95 } |
95 } |
96 |
96 |
97 parse_str($dsn, $dsnVars); |
97 parse_str($dsn, $dsnVars); |
98 |
98 |
99 // parse_str suffers when magic_quotes is enabled |
99 // parse_str suffers when magic_quotes is enabled |
100 if (get_magic_quotes_gpc()) { |
100 if (get_magic_quotes_gpc()) { |
101 array_walk_recursive($dsnVars, array($this, '_cleanMagicQuotesInValues')); |
101 array_walk_recursive($dsnVars, array($this, '_cleanMagicQuotesInValues')); |
102 } |
102 } |
103 |
103 |
104 $dbConfigValues = array('resources' => array('db' => null)); |
104 $dbConfigValues = array('resources' => array('db' => null)); |
105 |
105 |
106 if (isset($dsnVars['adapter'])) { |
106 if (isset($dsnVars['adapter'])) { |
107 $dbConfigValues['resources']['db']['adapter'] = $dsnVars['adapter']; |
107 $dbConfigValues['resources']['db']['adapter'] = $dsnVars['adapter']; |
108 unset($dsnVars['adapter']); |
108 unset($dsnVars['adapter']); |
109 } |
109 } |
110 |
110 |
111 $dbConfigValues['resources']['db']['params'] = $dsnVars; |
111 $dbConfigValues['resources']['db']['params'] = $dsnVars; |
112 |
112 |
113 $isPretend = $this->_registry->getRequest()->isPretend(); |
113 $isPretend = $this->_registry->getRequest()->isPretend(); |
114 |
114 |
115 // get the config resource |
115 // get the config resource |
116 $applicationConfig = $this->_loadedProfile->search('ApplicationConfigFile'); |
116 $applicationConfig = $this->_loadedProfile->search('ApplicationConfigFile'); |
117 $applicationConfig->addItem($dbConfigValues, $this->_sectionName, null); |
117 $applicationConfig->addItem($dbConfigValues, $this->_sectionName, null); |
118 |
118 |
119 $response = $this->_registry->getResponse(); |
119 $response = $this->_registry->getResponse(); |
120 |
120 |
121 if ($isPretend) { |
121 if ($isPretend) { |
122 $response->appendContent('A db configuration for the ' . $this->_sectionName |
122 $response->appendContent('A db configuration for the ' . $this->_sectionName |
123 . ' section would be written to the application config file with the following contents: ' |
123 . ' section would be written to the application config file with the following contents: ' |
124 ); |
124 ); |
125 $response->appendContent($applicationConfig->getContents()); |
125 $response->appendContent($applicationConfig->getContents()); |