From 72846a4b134b4b156c3b1602f570d42f8c7f91b6 Mon Sep 17 00:00:00 2001 From: Ben Thomson Date: Wed, 3 Nov 2021 15:17:23 +0800 Subject: [PATCH 1/3] Allow for YAML rendering pre/post processing. This is a BC break since it changes the contract. Classes implementing the "Winter\Storm\Parse\Processor\Contracts\YamlProcessor" interface should instead extend the "Winter\Storm\Parse\Processor\YamlProcessor" abstract to maintain parity with the interface. --- .../Processor/Contracts/YamlProcessor.php | 18 +- src/Parse/Processor/YamlProcessor.php | 46 +++++ src/Parse/Yaml.php | 13 +- tests/Parse/YamlTest.php | 160 ++++++++++++++++-- 4 files changed, 225 insertions(+), 12 deletions(-) create mode 100644 src/Parse/Processor/YamlProcessor.php 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/YamlProcessor.php b/src/Parse/Processor/YamlProcessor.php new file mode 100644 index 000000000..40c0729a9 --- /dev/null +++ b/src/Parse/Processor/YamlProcessor.php @@ -0,0 +1,46 @@ +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 9a73070da..2f8b5b385 100644 --- a/tests/Parse/YamlTest.php +++ b/tests/Parse/YamlTest.php @@ -1,6 +1,6 @@ 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 + ); + } } /** - * 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); } } From ff3ec81e41f11405ee81d3139d778cd15ac70ee0 Mon Sep 17 00:00:00 2001 From: Ben Thomson Date: Wed, 3 Nov 2021 15:22:12 +0800 Subject: [PATCH 2/3] Remove debug code from process abstract method --- src/Parse/Processor/YamlProcessor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Parse/Processor/YamlProcessor.php b/src/Parse/Processor/YamlProcessor.php index 40c0729a9..0e572d486 100644 --- a/src/Parse/Processor/YamlProcessor.php +++ b/src/Parse/Processor/YamlProcessor.php @@ -25,7 +25,7 @@ public function preprocess($text) */ public function process($parsed) { - return (object) $parsed; + return $parsed; } /** From 6c7b2d4fcdf428cfbab6f3de0a6067c6ae43158b Mon Sep 17 00:00:00 2001 From: Ben Thomson Date: Thu, 16 Dec 2021 15:13:05 +0800 Subject: [PATCH 3/3] Fix tests --- src/Parse/Processor/Symfony3Processor.php | 4 +--- tests/Parse/YamlTest.php | 3 +-- 2 files changed, 2 insertions(+), 5 deletions(-) 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 @@