Selaa lähdekoodia

ZF-9045
- Fix for DbAdapter setup
- Added autodetection support for quoting in ApplicationConfigFile context

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@20820 44c647ce-9c0f-0410-b52a-842ac1e357ba

ralph 16 vuotta sitten
vanhempi
commit
3051c15800

+ 19 - 8
library/Zend/Tool/Project/Context/Zf/ApplicationConfigFile.php

@@ -89,7 +89,12 @@ class Zend_Tool_Project_Context_Zf_ApplicationConfigFile extends Zend_Tool_Proje
     public function getContents()
     {
         if ($this->_content === null) {
-            $this->_content = $this->_getDefaultContents();
+            if (file_exists($this->getPath())) {
+                $this->_content = file_get_contents($this->getPath());
+            } else {
+                $this->_content = $this->_getDefaultContents();
+            }
+            
         }
         
         return $this->_content;
@@ -111,11 +116,17 @@ class Zend_Tool_Project_Context_Zf_ApplicationConfigFile extends Zend_Tool_Proje
      */
     public function addStringItem($key, $value, $section = 'production', $quoteValue = true)
     {
-        if ($quoteValue) {
+        
+        
+        if ($quoteValue === null) {
+            $quoteValue = preg_match('#[\"\']#', $value) ? false : true;
+        }
+        
+        if ($quoteValue == true) {
             $value = '"' . $value . '"';
         }
         
-        $contentLines = file($this->getPath());
+        $contentLines = preg_split('#[\n\r]#', $this->getContents());
         
         $newLines = array();
         $insideSection = false;
@@ -128,8 +139,8 @@ class Zend_Tool_Project_Context_Zf_ApplicationConfigFile extends Zend_Tool_Proje
             
             if ($insideSection) {
                 // if its blank, or a section heading
-                if ((trim($contentLine) == null) || ($contentLines[$contentLineIndex + 1][0] == '[')) {
-                    $newLines[] = $key . ' = ' . $value . "\n";
+                if ((trim($contentLine) == null) || (isset($contentLines[$contentLineIndex + 1]{0}) && $contentLines[$contentLineIndex + 1]{0} == '[')) {
+                    $newLines[] = $key . ' = ' . $value;
                     $insideSection = null;
                 }
             }
@@ -137,7 +148,7 @@ class Zend_Tool_Project_Context_Zf_ApplicationConfigFile extends Zend_Tool_Proje
             $newLines[] = $contentLine;
         }
 
-        $this->_content = implode('', $newLines);
+        $this->_content = implode("\n", $newLines);
         return $this;
     }
     
@@ -242,8 +253,7 @@ class Zend_Tool_Project_Context_Zf_ApplicationConfigFile extends Zend_Tool_Proje
     
     protected function _getDefaultContents()
     {
-        // resources.log.zendmonitor.writerName = "ZendMonitor"
-        
+
         $contents =<<<EOS
 [production]
 phpSettings.display_startup_errors = 0
@@ -267,6 +277,7 @@ phpSettings.display_errors = 1
 resources.frontController.params.displayExceptions = 1
 
 EOS;
+
         return $contents;
     }
     

+ 20 - 83
library/Zend/Tool/Project/Provider/DbAdapter.php

@@ -47,7 +47,7 @@ class Zend_Tool_Project_Provider_DbAdapter
     
     protected $_sectionName = 'production';
     
-    public function configure($dsn = null, $interactivelyPrompt = false, $sectionName = 'production')
+    public function configure($dsn = null, /* $interactivelyPrompt = false, */ $sectionName = 'production')
     {
         $profile = $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION);
         
@@ -78,7 +78,7 @@ class Zend_Tool_Project_Provider_DbAdapter
         } elseif ($interactivelyPrompt) {
             $this->_promptForConfig();
         } else {
-            echo 'Nothing to do!';
+            $this->_registry->getResponse()->appendContent('Nothing to do!');
         }
         
         
@@ -96,107 +96,44 @@ class Zend_Tool_Project_Provider_DbAdapter
         
         parse_str($dsn, $dsnVars);
 
-        $dbConfigValues = array();
+        // parse_str suffers when magic_quotes is enabled
+        if (get_magic_quotes_gpc()) {
+            array_walk_recursive(&$dsnVars, array($this, '_cleanMagicQuotesInValues'));
+        }
+        
+        $dbConfigValues = array('resources' => array('db' => null));
         
         if (isset($dsnVars['adapter'])) {
-            $dbConfigValues['adapter'] = $dsnVars['adapter'];
+            $dbConfigValues['resources']['db']['adapter'] = $dsnVars['adapter'];
             unset($dsnVars['adapter']);
         }
         
-        $dbConfigValues['params'] = $dsnVars;
+        $dbConfigValues['resources']['db']['params'] = $dsnVars;
         
         $isPretend = $this->_registry->getRequest()->isPretend();
-        
-        $content = $this->_writeToApplicationConfig($dbConfigValues, $isPretend);
+
+        // get the config resource
+        $applicationConfig = $this->_loadedProfile->search('ApplicationConfigFile');
+        $applicationConfig->addItem($dbConfigValues, $this->_sectionName, null);
         
         $response = $this->_registry->getResponse();
         
         if ($isPretend) {
             $response->appendContent('A db configuration for the ' . $this->_sectionName
-                . ' would be written to the application config file with the following contents: '
+                . ' section would be written to the application config file with the following contents: '
                 );
-            $response->appendContent($content);
+            $response->appendContent($applicationConfig->getContents());
         } else {
+            $applicationConfig->create();
             $response->appendContent('A db configuration for the ' . $this->_sectionName
-                . ' has been written to the application config file.'
+                . ' section has been written to the application config file.'
                 );
         }
     }
     
-    protected function _promptForConfig()
-    {
-        echo '//@todo';
-    }
-
-    protected function _promtForConfigPdoMysql()
-    {
-        $r = array(
-            'username' => 'Username',
-            'password' => 'Password',
-            'dbname'   => 'Database',
-            'driver_options' => array(
-                
-                )
-            );
-    }
-
-    protected function _writeToApplicationConfig($configValues, $isPretend = false)
+    protected function _cleanMagicQuotesInValues(&$value, $key)
     {
-        $configKeyNames = array('resources', 'db');
-        
-        $newDbLines = array();
-        
-        $rii = new RecursiveIteratorIterator(
-            new RecursiveArrayIterator($configValues),
-            RecursiveIteratorIterator::SELF_FIRST
-            );
-        
-        $lastDepth = 0;
-        
-        foreach ($rii as $name => $value) {
-            if ($lastDepth > $rii->getDepth()) {
-                array_pop($configKeyNames);
-            }
-            
-            $lastDepth = $rii->getDepth();
-            
-            if (is_array($value)) {
-                array_push($configKeyNames, $name);
-            } else {
-                $newDbLines[] = implode('.', $configKeyNames) . '.' . $name . ' = "' . $value . "\"\n";
-            }
-        }
-        
-        $originalLines = file($this->_appConfigFilePath);
-        
-        $newLines = array();
-        $insideSection = false;
-        
-        foreach ($originalLines as $originalLineIndex => $originalLine) {
-            
-            if ($insideSection === false && preg_match('#^\[' . $this->_sectionName . '#', $originalLine)) {
-                $insideSection = true;
-            }
-            
-            if ($insideSection) {
-                if ((trim($originalLine) == null) || ($originalLines[$originalLineIndex + 1][0] == '[')) {
-                    foreach ($newDbLines as $newDbLine) {
-                        $newLines[] = $newDbLine;
-                    }
-                    $insideSection = null;
-                }
-            }
-            
-            $newLines[] = $originalLine;
-        }
-
-        $newConfigContents = implode('', $newLines);
-        
-        if (!$isPretend) {
-            file_put_contents($this->_appConfigFilePath, $newConfigContents);
-        }
-        
-        return $newConfigContents;
+        $value = stripslashes($value);
     }
     
 }