diff --git a/src/Yaml.php b/src/Yaml.php index 8225308..eeb5eed 100755 --- a/src/Yaml.php +++ b/src/Yaml.php @@ -278,6 +278,7 @@ protected static function _decodeYaml($currentIndent, &$lines) $inIndent = false; while (list($n, $line) = each($lines)) { $lineno = $n + 1; + $line = rtrim(preg_replace("/#.*$/", "", $line)); if (strlen($line) == 0) { continue; } @@ -308,7 +309,7 @@ protected static function _decodeYaml($currentIndent, &$lines) // key: value if (strlen($m[2])) { // simple key: value - $value = $m[2]; + $value = rtrim(preg_replace("/#.*$/", "", $m[2])); // Check for booleans and constants if (preg_match('/^(t(rue)?|on|y(es)?)$/i', $value)) { $value = true; @@ -330,7 +331,15 @@ protected static function _decodeYaml($currentIndent, &$lines) // item in the list: // - FOO if (strlen($line) > 2) { - $config[] = substr($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; } else { $config[] = self::_decodeYaml($currentIndent + 1, $lines); } diff --git a/test/YamlTest.php b/test/YamlTest.php index 86415f5..c2d8449 100644 --- a/test/YamlTest.php +++ b/test/YamlTest.php @@ -46,6 +46,11 @@ public function setUp() $this->_badIndentationConfig = __DIR__ . '/_files/badindentation.yaml'; $this->_booleansConfig = __DIR__ . '/_files/booleans.yaml'; $this->_constantsConfig = __DIR__ . '/_files/constants.yaml'; + $this->_yamlInlineCommentsConfig = dirname(__FILE__) . '/_files/inlinecomments.yaml'; + $this->_yamlIndentedCommentsConfig = dirname(__FILE__) . '/_files/indentedcomments.yaml'; + $this->_yamlListConstantsConfig = dirname(__FILE__) . '/_files/listconstants.yaml'; + $this->_listBooleansConfig = dirname(__FILE__) . '/_files/listbooleans.yaml'; + } public function testLoadSingleSection() @@ -337,4 +342,79 @@ public function testAllowsIgnoringConstantStrings() $this->assertEquals('ZEND_CONFIG_YAML_ENV', $config->env); $this->assertEquals('ZEND_CONFIG_YAML_ENV_PATH/test/this', $config->path); } + + /** + * @group ZF-11329 + */ + public function testAllowsInlineCommentsInValuesUsingHash() + { + $config = new YamlConfig($this->_yamlInlineCommentsConfig, null); + $this->assertSame( + 'APPLICATION_PATH/controllers', + $config->resources->frontController->controllerDirectory + ); + } + + /** + * @group ZF-11384 + */ + public function testAllowsIndentedCommentsUsingHash() + { + $config = new YamlConfig($this->_yamlIndentedCommentsConfig, null); + $this->assertSame( + 'APPLICATION_PATH/controllers', + $config->resources->frontController->controllerDirectory + ); + } + + /** + * @group ZF-11702 + */ + public function testAllowsConstantsInLists() + { + if (!defined('ZEND_CONFIG_YAML_TEST_PATH')) { + define('ZEND_CONFIG_YAML_TEST_PATH', 'testing'); + } + $config = new YamlConfig($this->_yamlListConstantsConfig, 'production'); + + $this->assertEquals(ZEND_CONFIG_YAML_TEST_PATH, $config->paths->{0}); + $this->assertEquals(ZEND_CONFIG_YAML_TEST_PATH . '/library/test', $config->paths->{1}); + } + + /** + * @group ZF-11702 + */ + public function testAllowsBooleansInLists() + { + $config = new YamlConfig($this->_listBooleansConfig, 'production'); + + $this->assertTrue($config->usingLowerCasedYes->{0}); + $this->assertTrue($config->usingTitleCasedYes->{0}); + $this->assertTrue($config->usingCapitalYes->{0}); + $this->assertTrue($config->usingLowerY->{0}); + $this->assertTrue($config->usingUpperY->{0}); + + $this->assertFalse($config->usingLowerCasedNo->{0}); + $this->assertFalse($config->usingTitleCasedNo->{0}); + $this->assertFalse($config->usingCapitalNo->{0}); + $this->assertFalse($config->usingLowerN->{0}); + $this->assertFalse($config->usingUpperN->{0}); + + $this->assertTrue($config->usingLowerCasedTrue->{0}); + $this->assertTrue($config->usingTitleCasedTrue->{0}); + $this->assertTrue($config->usingCapitalTrue->{0}); + + $this->assertFalse($config->usingLowerCasedFalse->{0}); + $this->assertFalse($config->usingTitleCasedFalse->{0}); + $this->assertFalse($config->usingCapitalFalse->{0}); + + $this->assertTrue($config->usingLowerCasedOn->{0}); + $this->assertTrue($config->usingTitleCasedOn->{0}); + $this->assertTrue($config->usingCapitalOn->{0}); + + $this->assertFalse($config->usingLowerCasedOff->{0}); + $this->assertFalse($config->usingTitleCasedOff->{0}); + $this->assertFalse($config->usingCapitalOff->{0}); + } + } diff --git a/test/_files/indentedcomments.yaml b/test/_files/indentedcomments.yaml new file mode 100644 index 0000000..4b7b767 --- /dev/null +++ b/test/_files/indentedcomments.yaml @@ -0,0 +1,4 @@ +resources: + #frontcontroller! + frontController: + controllerDirectory: APPLICATION_PATH/controllers \ No newline at end of file diff --git a/test/_files/inlinecomments.yaml b/test/_files/inlinecomments.yaml new file mode 100644 index 0000000..c3b75e6 --- /dev/null +++ b/test/_files/inlinecomments.yaml @@ -0,0 +1,3 @@ +resources: + frontController: + controllerDirectory: APPLICATION_PATH/controllers #heynow! \ No newline at end of file diff --git a/test/_files/listbooleans.yaml b/test/_files/listbooleans.yaml new file mode 100644 index 0000000..21fc789 --- /dev/null +++ b/test/_files/listbooleans.yaml @@ -0,0 +1,51 @@ +production: + usingLowerCasedYes: + - yes + usingTitleCasedYes: + - Yes + usingCapitalYes: + - YES + usingLowerY: + - y + usingUpperY: + - Y + + usingLowerCasedNo: + - no + usingTitleCasedNo: + - No + usingCapitalNo: + - NO + usingLowerN: + - n + usingUpperN: + - N + + usingLowerCasedTrue: + - true + usingTitleCasedTrue: + - True + usingCapitalTrue: + - TRUE + + usingLowerCasedFalse: + - false + usingTitleCasedFalse: + - False + usingCapitalFalse: + - FALSE + + usingLowerCasedOn: + - on + usingTitleCasedOn: + - On + usingCapitalOn: + - ON + + usingLowerCasedOff: + - off + usingTitleCasedOff: + - Off + usingCapitalOff: + - OFF + diff --git a/test/_files/listconstants.yaml b/test/_files/listconstants.yaml new file mode 100644 index 0000000..86610ca --- /dev/null +++ b/test/_files/listconstants.yaml @@ -0,0 +1,4 @@ +production: + paths: + - ZEND_CONFIG_YAML_TEST_PATH + - ZEND_CONFIG_YAML_TEST_PATH/library/test