diff --git a/src/Parse/Processor/Contracts/YamlProcessor.php b/src/Parse/Processor/Contracts/YamlProcessor.php index ed40b4edb..c9b5f5048 100644 --- a/src/Parse/Processor/Contracts/YamlProcessor.php +++ b/src/Parse/Processor/Contracts/YamlProcessor.php @@ -3,7 +3,7 @@ /** * Yaml processor contract. * - * Allows for pre-or-post processing of YAML content during parsing. + * Allows for pre-or-post processing of YAML content during parsing or rendering. * * @author Winter CMS */ @@ -24,4 +24,20 @@ public function preprocess($text); * @return mixed */ public function process($parsed); + + /** + * Pre-process the data that will be rendered to a YAML string or file. + * + * @param mixed $data + * @return mixed + */ + public function prerender($data); + + /** + * Post-process a rendered YAML string or file. + * + * @param string $yaml + * @return string + */ + public function render($yaml); } diff --git a/src/Parse/Processor/Symfony3Processor.php b/src/Parse/Processor/Symfony3Processor.php index 8eed335a9..2e11321d7 100644 --- a/src/Parse/Processor/Symfony3Processor.php +++ b/src/Parse/Processor/Symfony3Processor.php @@ -1,7 +1,5 @@ dump($vars, $inline, 0, $exceptionOnInvalidType, $objectSupport); + + if (!is_null($this->processor) && method_exists($this->processor, 'prerender')) { + $vars = $this->processor->prerender($vars); + } + + $yamlContent = $yaml->dump($vars, $inline, 0, $exceptionOnInvalidType, $objectSupport); + + if (!is_null($this->processor) && method_exists($this->processor, 'render')) { + $yamlContent = $this->processor->render($yamlContent); + } + + return $yamlContent; } /** diff --git a/tests/Parse/YamlTest.php b/tests/Parse/YamlTest.php index 098af0b43..4bb66f134 100644 --- a/tests/Parse/YamlTest.php +++ b/tests/Parse/YamlTest.php @@ -1,7 +1,7 @@ test); } + public function testRenderWithoutProcessor() + { + $parser = new YamlParser; + + $yaml = $parser->render([ + '1.0.0' => [ + 'First version', + 'some_update_file.php', + ], + '1.0.1' => [ + 'Second version', + ], + 'test' => [ + 'String-based key', + ], + 'test two' => [ + 'String-based key with a space', + ], + ]); + + $this->assertIsString($yaml); + $this->assertEquals( + "1.0.0:\n" . + " - 'First version'\n" . + " - some_update_file.php\n" . + "1.0.1:\n" . + " - 'Second version'\n" . + "test:\n" . + " - 'String-based key'\n" . + "'test two':\n" . + " - 'String-based key with a space'\n", + $yaml + ); + } + + public function testRenderWithPreProcessor() + { + $parser = new YamlParser; + + $parser->setProcessor(new UppercaseKeysProcessor); + $yaml = $parser->render([ + '1.0.0' => [ + 'First version', + 'some_update_file.php', + ], + '1.0.1' => [ + 'Second version', + ], + 'test' => [ + 'String-based key', + ], + 'test two' => [ + 'String-based key with a space', + ], + ]); + $parser->removeProcessor(); + + $this->assertIsString($yaml); + $this->assertEquals( + "1.0.0:\n" . + " - 'First version'\n" . + " - some_update_file.php\n" . + "1.0.1:\n" . + " - 'Second version'\n" . + "TEST:\n" . + " - 'String-based key'\n" . + "'TEST TWO':\n" . + " - 'String-based key with a space'\n", + $yaml + ); + } + + public function testRenderWithPreAndPostProcessor() + { + $parser = new YamlParser; + + $parser->setProcessor(new QuotedUpperKeysProcessor); + $yaml = $parser->render([ + '1.0.0' => [ + 'First version', + 'some_update_file.php', + ], + '1.0.1' => [ + 'Second version', + ], + 'test' => [ + 'String-based key', + ], + 'test two' => [ + 'String-based key with a space', + ], + ]); + $parser->removeProcessor(); + + $this->assertIsString($yaml); + $this->assertEquals( + "'1.0.0':\n" . + " - 'First version'\n" . + " - some_update_file.php\n" . + "'1.0.1':\n" . + " - 'Second version'\n" . + "'TEST':\n" . + " - 'String-based key'\n" . + "'TEST TWO':\n" . + " - 'String-based key with a space'\n", + $yaml + ); + } + public function testSymfony3YamlFile() { // This YAML file should not be parseable by default @@ -151,33 +260,64 @@ public function testSymfony3YamlFileWithProcessor() } /** - * Test pre-processor + * Test parse pre-processor */ -class UppercaseYamlProcessor implements YamlProcessor +class UppercaseYamlProcessor extends YamlProcessor { public function preprocess($text) { return strtoupper($text); } +} +/** + * Test parse post-processor + */ +class ObjectYamlProcessor extends YamlProcessor +{ public function process($parsed) { - return $parsed; + return (object) $parsed; } } /** - * Test post-processor + * Test render pre-processor */ -class ObjectYamlProcessor implements YamlProcessor +class UppercaseKeysProcessor extends YamlProcessor { - public function preprocess($text) + public function prerender($data) { - return $text; + $processed = []; + + foreach ($data as $key => $value) { + $processed[strtoupper($key)] = $value; + } + + return $processed; } +} - public function process($parsed) +/** + * Test render pre-and-post-processor + */ +class QuotedUpperKeysProcessor extends YamlProcessor +{ + public function prerender($data) { - return (object) $parsed; + $processed = []; + + foreach ($data as $key => $value) { + $processed[strtoupper($key)] = $value; + } + + return $processed; + } + + public function render($yaml) + { + return preg_replace_callback('/^\s*([\'"]{0}[^\'"\n\r:]+[\'"]{0})\s*:\s*$/m', function ($matches) { + return "'" . trim($matches[1]) . "':"; + }, $yaml); } }