8000 [Yaml] Add tags support by GuilhemN · Pull Request #21194 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Yaml] Add tags support #21194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\DependencyInjection\Dumper;

use Symfony\Component\Yaml\Dumper as YmlDumper;
use Symfony\Component\Yaml\Tag\TaggedValue;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Argument\ClosureProxyArgument;
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
Expand Down Expand Up @@ -251,10 +252,10 @@ private function dumpCallable($callable)
*/
private function dumpValue($value)
{
if ($value instanceof IteratorArgument) {
$value = array('=iterator' => $value->getValues());
} elseif ($value instanceof ClosureProxyArgument) {
$value = array('=closure_proxy' => $value->getValues());
if ($value instanceof IteratorArgument || $value instanceof ClosureProxyArgument) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instanceof ArgumentInterface?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't know the tag corresponding to other instances so I think it's better this way here.

$tag = $value instanceof IteratorArgument ? 'iterator' : 'closure_proxy';

return new TaggedValue($tag, $this->dumpValue($value->getValues()));
}

if (is_array($value)) {
Expand Down
53 changes: 27 additions & 26 deletions src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Parser as YamlParser;
use Symfony\Component\Yaml\Tag\TaggedValue;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\ExpressionLanguage\Expression;

Expand Down Expand Up @@ -506,7 +507,7 @@ protected function loadFile($file)
}

try {
$configuration = $this->yamlParser->parse(file_get_contents($file), Yaml::PARSE_CONSTANT);
$configuration = $this->yamlParser->parse(file_get_contents($file), Yaml::PARSE_CONSTANT | Yaml::PARSE_CUSTOM_TAGS);
} catch (ParseException $e) {
throw new InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $file), 0, $e);
}
Expand Down Expand Up @@ -557,42 +558,42 @@ private function validate($content, $file)
/**
* Resolves services.
*
* @param string|array $value
* @param mixed $value
*
* @return array|string|Reference
* @return array|string|Reference|ArgumentInterface
*/
private function resolveServices($value)
{
if (is_array($value)) {
if (array_key_exists('=iterator', $value)) {
if (1 !== count($value)) {
throw new InvalidArgumentException('Arguments typed "=iterator" must have no sibling keys.');
}
if (!is_array($value = $value['=iterator'])) {
throw new InvalidArgumentException('Arguments typed "=iterator" must be arrays.');
}
$value = new IteratorArgument(array_map(array($this, 'resolveServices'), $value));
} elseif (array_key_exists('=closure_proxy', $value)) {
if (1 !== count($value)) {
throw new InvalidArgumentException('Arguments typed "=closure_proxy" must have no sibling keys.');
}
if (!is_array($value = $value['=closure_proxy']) || array(0, 1) !== array_keys($value)) {
throw new InvalidArgumentException('Arguments typed "=closure_proxy" must be arrays of [@service, method].');
if ($value instanceof TaggedValue) {
$argument = $value->getValue();
if ('iterator' === $value->getTag()) {
if (!is_array($argument)) {
throw new InvalidArgumentException('"!iterator" tag only accepts sequences.');
}
if (!is_string($value[0]) || !is_string($value[1]) || 0 !== strpos($value[0], '@') || 0 === strpos($value[0], '@@')) {
throw new InvalidArgumentException('Arguments typed "=closure_proxy" must be arrays of [@service, method].');

return new IteratorArgument(array_map(array($this, 'resolveServices'), $argument));
}
if ('closure_proxy' === $value->getTag()) {
if (!is_array($argument) || array(0, 1) !== array_keys($argument) || !is_string($argument[0]) || !is_string($argument[1]) || 0 !== strpos($argument[0], '@') || 0 === strpos($argument[0], '@@')) {
throw new InvalidArgumentException('"!closure_proxy" tagged values must be arrays of [@service, method].');
}
if (0 === strpos($value[0], '@?')) {
$value[0] = substr($value[0], 2);

if (0 === strpos($argument[0], '@?')) {
$argument[0] = substr($argument[0], 2);
$invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;
} else {
$value[0] = substr($value[0], 1);
$argument[0] = substr($argument[0], 1);
$invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
}
$value = new ClosureProxyArgument($value[0], $value[1], $invalidBehavior);
} else {
$value = array_map(array($this, 'resolveServices'), $value);

return new ClosureProxyArgument($argument[0], $argument[1], $invalidBehavior);
}

throw new InvalidArgumentException(sprintf('Unsupported tag "!%s".', $value->getTag()));
}

if (is_array($value)) {
$value = array_map(array($this, 'resolveServices'), $value);
} elseif (is_string($value) && 0 === strpos($value, '@=')) {
return new Expression(substr($value, 2));
} elseif (is_string($value) && 0 === strpos($value, '@')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Dumper\YamlDumper;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Parser;

class YamlDumperTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -62,8 +63,10 @@ public function testDumpAutowireData()
$this->assertStringEqualsFile(self::$fixturesPath.'/yaml/services24.yml', $dumper->dump());
}

private function assertEqualYamlStructure($yaml, $expected, $message = '')
private function assertEqualYamlStructure($expected, $yaml, $message = '')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this accept a new $flags argument instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works either way but imo this function should always be executed with the same flags used in YamlFileLoader.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh sure!

{
$this->assertEquals(Yaml::parse($expected), Yaml::parse($yaml), $message);
$parser = new Parser();

$this->assertEquals($parser->parse($expected, Yaml::PARSE_CUSTOM_TAGS), $parser->parse($yaml, Yaml::PARSE_CUSTOM_TAGS), $message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ services:
factory: ['@factory_simple', getInstance]
lazy_context:
class: LazyContext
arguments: [{ '=iterator': [foo, '@foo.baz', { '%foo%': 'foo is %foo%', foobar: '%foo%' }, true, '@service_container'] }]
arguments: [!iterator [foo, '@foo.baz', { '%foo%': 'foo is %foo%', foobar: '%foo%' }, true, '@service_container']]
lazy_context_ignore_invalid_ref:
class: LazyContext
arguments: [{ '=iterator': ['@foo.baz', '@?invalid'] }]
arguments: [!iterator ['@foo.baz', '@?invalid']]
closure_proxy:
class: BarClass
arguments: [{ '=closure_proxy': ['@closure_proxy', getBaz] }]
arguments: [!closure_proxy ['@closure_proxy', getBaz]]
alias_for_foo: '@foo'
alias_for_alia 8000 s: '@foo'
6 changes: 3 additions & 3 deletions src/Symfony/Component/DependencyInjection/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"php": ">=5.5.9"
},
"require-dev": {
"symfony/yaml": "~3.2",
"symfony/yaml": "~3.3",
"symfony/config": "~3.3",
"symfony/expression-language": "~2.8|~3.0"
},
Expand All @@ -30,8 +30,8 @@
"symfony/proxy-manager-bridge": "Generate service proxies to lazy load them"
},
"conflict": {
"symfony/config": "<3.3",
"symfony/yaml": "<3.2"
"symfony/yaml": "<3.3",
"symfony/config": "<3.3"
},
"autoload": {
"psr-4": { "Symfony\\Component\\DependencyInjection\\": "" },
Expand Down
Loading
0