Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/Parse/Processor/Contracts/YamlProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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);
}
4 changes: 1 addition & 3 deletions src/Parse/Processor/Symfony3Processor.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php namespace Winter\Storm\Parse\Processor;

use Winter\Storm\Parse\Processor\Contracts\YamlProcessor;

/**
* Symfony/Yaml 3 processor.
*
Expand All @@ -10,7 +8,7 @@
*
* @author Winter CMS
*/
class Symfony3Processor implements YamlProcessor
class Symfony3Processor extends YamlProcessor
{
/**
* @inheritDoc
Expand Down
46 changes: 46 additions & 0 deletions src/Parse/Processor/YamlProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php namespace Winter\Storm\Parse\Processor;

use Winter\Storm\Parse\Processor\Contracts\YamlProcessor as YamlProcessorContract;

/**
* YAML processor abstract.
*
* Provides base functionality for YAML processors, so that extended classes only need to overwrite the methods
* that they intend to actually use.
*
* @author Winter CMS
*/
abstract class YamlProcessor implements YamlProcessorContract
{
/**
* @inheritDoc
*/
public function preprocess($text)
{
return $text;
}

/**
* @inheritDoc
*/
public function process($parsed)
{
return $parsed;
}

/**
* @inheritDoc
*/
public function prerender($data)
{
return $data;
}

/**
* @inheritDoc
*/
public function render($yaml)
{
return $yaml;
}
}
13 changes: 12 additions & 1 deletion src/Parse/Yaml.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,18 @@ public function render($vars = [], $options = [])
], $options));

$yaml = new Dumper;
return $yaml->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;
}

/**
Expand Down
160 changes: 150 additions & 10 deletions tests/Parse/YamlTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

use Winter\Storm\Parse\Processor\YamlProcessor;
use Symfony\Component\Yaml\Exception\ParseException;
use Winter\Storm\Parse\Processor\Contracts\YamlProcessor;
use Winter\Storm\Parse\Processor\Symfony3Processor;
use Winter\Storm\Parse\Yaml as YamlParser;

Expand Down Expand Up @@ -92,6 +92,115 @@ public function testParseWithPostProcessor()
], $yaml->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
Expand Down Expand Up @@ -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);
}
}