Browse Source

ZF-11934: Applying modified patch by Adam Lundrigan from Maks Slesarenko to Yaml decoder

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@24712 44c647ce-9c0f-0410-b52a-842ac1e357ba
bittarman 13 years ago
parent
commit
7e5ec010a6
3 changed files with 49 additions and 20 deletions
  1. 38 20
      library/Zend/Config/Yaml.php
  2. 10 0
      tests/Zend/Config/YamlTest.php
  3. 1 0
      tests/Zend/Config/_files/zf11934.yaml

+ 38 - 20
library/Zend/Config/Yaml.php

@@ -126,7 +126,7 @@ class Zend_Config_Yaml extends Zend_Config
      *
      * @param  string        $yaml     YAML file to process
      * @param  mixed         $section  Section to process
-     * @param  array|boolean $options 
+     * @param  array|boolean $options
      */
     public function __construct($yaml, $section = null, $options = false)
     {
@@ -285,7 +285,7 @@ class Zend_Config_Yaml extends Zend_Config
         $inIndent = false;
         while (list($n, $line) = each($lines)) {
             $lineno = $n + 1;
-            
+
             $line = rtrim(preg_replace("/#.*$/", "", $line));
             if (strlen($line) == 0) {
                 continue;
@@ -314,16 +314,8 @@ class Zend_Config_Yaml extends Zend_Config
                 // key: value
                 if (strlen($m[2])) {
                     // simple key: value
-                    $value = rtrim(preg_replace("/#.*$/", "", $m[2]));
-                    // Check for booleans and constants
-                    if (preg_match('/^(t(rue)?|on|y(es)?)$/i', $value)) {
-                        $value = true;
-                    } elseif (preg_match('/^(f(alse)?|off|n(o)?)$/i', $value)) {
-                        $value = false;
-                    } elseif (!self::$_ignoreConstants) {
-                        // test for constants
-                        $value = self::_replaceConstants($value);
-                    }
+                    $value = preg_replace("/#.*$/", "", $m[2]);
+                    $value = self::_parseValue($value);
                 } else {
                     // key: and then values on new lines
                     $value = self::_decodeYaml($currentIndent + 1, $lines);
@@ -337,14 +329,8 @@ class Zend_Config_Yaml extends Zend_Config
                 // - FOO
                 if (strlen($line) > 2) {
                     $value = substr($line, 2);
-                    if (preg_match('/^(t(rue)?|on|y(es)?)$/i', $value)) {
-                        $value = true;
-                    } elseif (preg_match('/^(f(alse)?|off|n(o)?)$/i', $value)) {
-                        $value = false;
-                    } elseif (!self::$_ignoreConstants) {
-                         $value = self::_replaceConstants($value);
-                    }
-                    $config[] = $value;
+
+                    $config[] = self::_parseValue($value);
                 } else {
                     $config[] = self::_decodeYaml($currentIndent + 1, $lines);
                 }
@@ -360,6 +346,38 @@ class Zend_Config_Yaml extends Zend_Config
     }
 
     /**
+     * Parse values
+     *
+     * @param string $value
+     * @return string
+     */
+    protected static function _parseValue($value)
+    {
+        $value = trim($value);
+
+        // remove quotes from string.
+        if ('"' == $value['0']) {
+            if ('"' == $value[count($value) -1]) {
+                $value = substr($value, 1, -1);
+            }
+        } elseif ('\'' == $value['0'] && '\'' == $value[count($value) -1]) {
+            $value = strtr($value, array("''" => "'", "'" => ''));
+        }
+
+        // Check for booleans and constants
+        if (preg_match('/^(t(rue)?|on|y(es)?)$/i', $value)) {
+            $value = true;
+        } elseif (preg_match('/^(f(alse)?|off|n(o)?)$/i', $value)) {
+            $value = false;
+        } elseif (!self::$_ignoreConstants) {
+            // test for constants
+            $value = self::_replaceConstants($value);
+        }
+
+        return $value;
+    }
+
+    /**
      * Replace any constants referenced in a string with their values
      *
      * @param  string $value

+ 10 - 0
tests/Zend/Config/YamlTest.php

@@ -50,6 +50,7 @@ class Zend_Config_YamlTest extends PHPUnit_Framework_TestCase
         $this->_yamlIndentedCommentsConfig  = dirname(__FILE__) . '/_files/indentedcomments.yaml';
         $this->_yamlListConstantsConfig     = dirname(__FILE__) . '/_files/listconstants.yaml';
         $this->_listBooleansConfig          = dirname(__FILE__) . '/_files/listbooleans.yaml';
+        $this->_yamlSingleQuotedString    = dirname(__FILE__) . '/_files/zf11934.yaml';
     }
 
     public function testLoadSingleSection()
@@ -406,4 +407,13 @@ class Zend_Config_YamlTest extends PHPUnit_Framework_TestCase
         $this->assertFalse($config->usingCapitalOff->{0});
     }
     
+    /**
+     * @group ZF-11934
+     */
+    public function testAllowsSingleQuotedStringValues()
+    {
+        $config = new Zend_Config_Yaml($this->_yamlSingleQuotedString);
+        $this->assertEquals('two', $config->one);
+    }
+    
 }

+ 1 - 0
tests/Zend/Config/_files/zf11934.yaml

@@ -0,0 +1 @@
+one: 'two'