From e9657bba5f238bdf177f2cd8e8a81a991d5a1492 Mon Sep 17 00:00:00 2001 From: Joel Wurtz Date: Tue, 27 Feb 2024 16:17:27 +0100 Subject: [PATCH] feat(yaml): add yaml parse and dump functions --- CHANGELOG.md | 3 + composer.json | 3 +- composer.lock | 74 ++++++++++++++++++- doc/going-further/helpers/yaml.md | 18 +++++ doc/reference.md | 2 + examples/yaml.php | 24 ++++++ src/functions.php | 17 +++++ .../FilesystemFindTest.php.output.txt | 2 +- .../Generated/ListTest.php.output.txt | 2 + tests/Examples/Generated/YamlDumpTest.php | 22 ++++++ .../Generated/YamlDumpTest.php.output.txt | 2 + tests/Examples/Generated/YamlParseTest.php | 22 ++++++ .../Generated/YamlParseTest.php.output.txt | 1 + 13 files changed, 189 insertions(+), 3 deletions(-) create mode 100644 doc/going-further/helpers/yaml.md create mode 100644 examples/yaml.php create mode 100644 tests/Examples/Generated/YamlDumpTest.php create mode 100644 tests/Examples/Generated/YamlDumpTest.php.output.txt create mode 100644 tests/Examples/Generated/YamlParseTest.php create mode 100644 tests/Examples/Generated/YamlParseTest.php.output.txt diff --git a/CHANGELOG.md b/CHANGELOG.md index afeeceb6..68d750d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## Not released yet +* Add a `yaml_dump()` function to dump any PHP value to a YAML string +* Add a `yaml_parse()` function to parse a YAML string to a PHP value + ## 0.13.1 (2024-02-27) * Fix instruction for downloading new castor version as a phar diff --git a/composer.json b/composer.json index e98415d8..9196fa27 100644 --- a/composer.json +++ b/composer.json @@ -45,7 +45,8 @@ "symfony/process": "^6.4.3", "symfony/string": "^6.4.3", "symfony/translation-contracts": "^3.4.1", - "symfony/var-dumper": "^6.4.3" + "symfony/var-dumper": "^6.4.3", + "symfony/yaml": "^6.4.3" }, "conflict": { "symfony/console": "6.4.3" diff --git a/composer.lock b/composer.lock index 8824607d..612ecc71 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "34fd713195fe1751e92dfaca52a5b234", + "content-hash": "16e5f7934a962f8c5ad979ca1c6cc176", "packages": [ { "name": "jolicode/jolinotif", @@ -2747,6 +2747,78 @@ } ], "time": "2024-01-23T14:51:35+00:00" + }, + { + "name": "symfony/yaml", + "version": "v6.4.3", + "source": { + "type": "git", + "url": "https://github.com/symfony/yaml.git", + "reference": "d75715985f0f94f978e3a8fa42533e10db921b90" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/yaml/zipball/d75715985f0f94f978e3a8fa42533e10db921b90", + "reference": "d75715985f0f94f978e3a8fa42533e10db921b90", + "shasum": "" + }, + "require": { + "php": ">=8.1", + "symfony/deprecation-contracts": "^2.5|^3", + "symfony/polyfill-ctype": "^1.8" + }, + "conflict": { + "symfony/console": "<5.4" + }, + "require-dev": { + "symfony/console": "^5.4|^6.0|^7.0" + }, + "bin": [ + "Resources/bin/yaml-lint" + ], + "type": "library", + "autoload": { + "psr-4": { + "Symfony\\Component\\Yaml\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Loads and dumps YAML files", + "homepage": "https://symfony.com", + "support": { + "source": "https://github.com/symfony/yaml/tree/v6.4.3" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2024-01-23T14:51:35+00:00" } ], "packages-dev": [ diff --git a/doc/going-further/helpers/yaml.md b/doc/going-further/helpers/yaml.md new file mode 100644 index 00000000..c90cafe8 --- /dev/null +++ b/doc/going-further/helpers/yaml.md @@ -0,0 +1,18 @@ +# YAML + +Castor provides a `yaml_parse()` and `yaml_dump()` functions that will parse or +dump YAML content and returns an array using the [symfony/yaml](https://symfony.com/doc/current/components/yaml.html) component: + +```php +use Castor\Attribute\AsTask; + +use function Castor\{yaml_parse, yaml_dump}; + +#[AsTask()] +function yaml_parse_and_dump(): void +{ + $content = yaml_parse(file_get_contents('file.yaml')); + + file_put_contents('file.yaml', yaml_dump($content)); +} +``` diff --git a/doc/reference.md b/doc/reference.md index f29729e0..87bb718e 100644 --- a/doc/reference.md +++ b/doc/reference.md @@ -44,6 +44,8 @@ Castor provides the following built-in functions: - [`wait_for_url`](going-further/helpers/wait-for.md#the-wait_for_url-function) - [`watch`](going-further/helpers/watch.md) - [`with`](going-further/interacting-with-castor/advanced-context.md#the-with-function) +- [`yaml_dump`](going-further/helpers/yaml.md) +- [`yaml_parse`](going-further/helpers/yaml.md) ## Attributes diff --git a/examples/yaml.php b/examples/yaml.php new file mode 100644 index 00000000..ad246e02 --- /dev/null +++ b/examples/yaml.php @@ -0,0 +1,24 @@ + 'bar']; + echo yaml_dump($data) . "\n"; +} diff --git a/src/functions.php b/src/functions.php index 387f0e6d..29e1cf67 100644 --- a/src/functions.php +++ b/src/functions.php @@ -24,6 +24,7 @@ use Symfony\Component\Finder\Finder; use Symfony\Component\Process\Exception\ProcessFailedException; use Symfony\Component\Process\Process; +use Symfony\Component\Yaml\Yaml; use Symfony\Contracts\Cache\CacheInterface; use Symfony\Contracts\Cache\CallbackInterface; use Symfony\Contracts\Cache\ItemInterface; @@ -1029,6 +1030,22 @@ function wait_for_docker_container( ); } +/** + * @see Yaml::parse() + */ +function yaml_parse(string $content, int $flags = 0): mixed +{ + return Yaml::parse($content, $flags); +} + +/** + * @see Yaml::dump() + */ +function yaml_dump(mixed $input, int $inline = 2, int $indent = 4, int $flags = 0): string +{ + return Yaml::dump($input, $inline, $indent, $flags); +} + function guard_min_version(string $minVersion): void { $currentVersion = GlobalHelper::getApplication()->getVersion(); diff --git a/tests/Examples/Generated/FilesystemFindTest.php.output.txt b/tests/Examples/Generated/FilesystemFindTest.php.output.txt index e207aef0..247c827f 100644 --- a/tests/Examples/Generated/FilesystemFindTest.php.output.txt +++ b/tests/Examples/Generated/FilesystemFindTest.php.output.txt @@ -1 +1 @@ -Number of PHP files: 27 +Number of PHP files: 28 diff --git a/tests/Examples/Generated/ListTest.php.output.txt b/tests/Examples/Generated/ListTest.php.output.txt index 871fc97a..3ead54aa 100644 --- a/tests/Examples/Generated/ListTest.php.output.txt +++ b/tests/Examples/Generated/ListTest.php.output.txt @@ -72,3 +72,5 @@ wait-for:wait-for-url-with-status-code-only Wait for an UR watch:fs-change Watches on filesystem changes watch:parallel-change Watches on filesystem changes with 2 watchers in parallel watch:stop Watches on filesystem changes and stop after first change +yaml:dump Dump a YAML content +yaml:parse Parse a YAML content diff --git a/tests/Examples/Generated/YamlDumpTest.php b/tests/Examples/Generated/YamlDumpTest.php new file mode 100644 index 00000000..ac346164 --- /dev/null +++ b/tests/Examples/Generated/YamlDumpTest.php @@ -0,0 +1,22 @@ +runTask(['yaml:dump']); + + $this->assertSame(0, $process->getExitCode()); + $this->assertStringEqualsFile(__FILE__ . '.output.txt', $process->getOutput()); + if (file_exists(__FILE__ . '.err.txt')) { + $this->assertStringEqualsFile(__FILE__ . '.err.txt', $process->getErrorOutput()); + } else { + $this->assertSame('', $process->getErrorOutput()); + } + } +} diff --git a/tests/Examples/Generated/YamlDumpTest.php.output.txt b/tests/Examples/Generated/YamlDumpTest.php.output.txt new file mode 100644 index 00000000..8218b158 --- /dev/null +++ b/tests/Examples/Generated/YamlDumpTest.php.output.txt @@ -0,0 +1,2 @@ +foo: bar + diff --git a/tests/Examples/Generated/YamlParseTest.php b/tests/Examples/Generated/YamlParseTest.php new file mode 100644 index 00000000..29ffadf9 --- /dev/null +++ b/tests/Examples/Generated/YamlParseTest.php @@ -0,0 +1,22 @@ +runTask(['yaml:parse']); + + $this->assertSame(0, $process->getExitCode()); + $this->assertStringEqualsFile(__FILE__ . '.output.txt', $process->getOutput()); + if (file_exists(__FILE__ . '.err.txt')) { + $this->assertStringEqualsFile(__FILE__ . '.err.txt', $process->getErrorOutput()); + } else { + $this->assertSame('', $process->getErrorOutput()); + } + } +} diff --git a/tests/Examples/Generated/YamlParseTest.php.output.txt b/tests/Examples/Generated/YamlParseTest.php.output.txt new file mode 100644 index 00000000..5716ca59 --- /dev/null +++ b/tests/Examples/Generated/YamlParseTest.php.output.txt @@ -0,0 +1 @@ +bar