Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a189478

Browse files
committed
[Yaml] Add custom tags support
1 parent cc398db commit a189478

File tree

12 files changed

+352
-98
lines changed

12 files changed

+352
-98
lines changed

UPGRADE-3.3.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,10 @@ TwigBridge
5959

6060
* The `TwigRendererEngine::setEnvironment()` method has been deprecated and will be removed
6161
in 4.0. Pass the Twig Environment as second argument of the constructor instead.
62+
63+
Yaml
64+
----
65+
66+
* Constructor arguments `$offset`, `$totalNumberOfLines` and
67+
`$skippedLineNumbers` of `Parser` are deprecated and will be
68+
removed in 4.0

UPGRADE-4.0.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ Serializer
216216
* The ability to pass a Doctrine `Cache` instance to the `ClassMetadataFactory`
217217
class has been removed. You should use the `CacheClassMetadataFactory` class
218218
instead.
219-
219+
220220
* Not defining the 6th argument `$format = null` of the
221221
`AbstractNormalizer::instantiateObject()` method when overriding it is not
222222
supported anymore.
@@ -294,7 +294,7 @@ Validator
294294
// ...
295295
}
296296
```
297-
297+
298298
* The default value of the strict option of the `Choice` Constraint has been
299299
changed to `true` as of 4.0. If you need the previous behaviour ensure to
300300
set the option to `false`.
@@ -390,3 +390,6 @@ Yaml
390390
the `!php/object` tag.
391391

392392
* Duplicate mapping keys lead to a `ParseException`.
393+
394+
* Constructor arguments `$offset`, `$totalNumberOfLines` and
395+
`$skippedLineNumbers` of `Parser` were removed.

src/Symfony/Component/DependencyInjection/Dumper/YamlDumper.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\DependencyInjection\Dumper;
1313

1414
use Symfony\Component\Yaml\Dumper as YmlDumper;
15+
use Symfony\Component\Yaml\Tag\TaggedValue;
1516
use Symfony\Component\DependencyInjection\Alias;
1617
use Symfony\Component\DependencyInjection\Argument\ClosureProxyArgument;
1718
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
@@ -247,10 +248,10 @@ private function dumpCallable($callable)
247248
*/
248249
private function dumpValue($value)
249250
{
250-
if ($value instanceof IteratorArgument) {
251-
$value = array('=iterator' => $value->getValues());
252-
} elseif ($value instanceof ClosureProxyArgument) {
253-
$value = array('=closure_proxy' => $value->getValues());
251+
if ($value instanceof IteratorArgument || $value instanceof ClosureProxyArgument) {
252+
$tag = $value instanceof IteratorArgument ? 'iterator' : 'closure_proxy';
253+
254+
return new TaggedValue($this->dumpValue($value->getValues()), $tag);
254255
}
255256

256257
if (is_array($value)) {

src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Symfony\Component\Config\Resource\FileResource;
2424
use Symfony\Component\Yaml\Exception\ParseException;
2525
use Symfony\Component\Yaml\Parser as YamlParser;
26+
use Symfony\Component\Yaml\Tag\TaggedValue;
2627
use Symfony\Component\Yaml\Yaml;
2728
use Symfony\Component\ExpressionLanguage\Expression;
2829

@@ -480,7 +481,7 @@ protected function loadFile($file)
480481
}
481482

482483
try {
483-
$configuration = $this->yamlParser->parse(file_get_contents($file), Yaml::PARSE_CONSTANT);
484+
$configuration = $this->yamlParser->parse(file_get_contents($file), Yaml::PARSE_CONSTANT | Yaml::PARSE_CUSTOM_TAGS);
484485
} catch (ParseException $e) {
485486
throw new InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $file), 0, $e);
486487
}
@@ -531,42 +532,42 @@ private function validate($content, $file)
531532
/**
532533
* Resolves services.
533534
*
534-
* @param string|array $value
535+
* @param mixed $value
535536
*
536-
* @return array|string|Reference
537+
* @return array|string|Reference|ArgumentInterface
537538
*/
538539
private function resolveServices($value)
539540
{
540-
if (is_array($value)) {
541-
if (array_key_exists('=iterator', $value)) {
542-
if (1 !== count($value)) {
543-
throw new InvalidArgumentException('Arguments typed "=iterator" must have no sibling keys.');
544-
}
545-
if (!is_array($value = $value['=iterator'])) {
546-
throw new InvalidArgumentException('Arguments typed "=iterator" must be arrays.');
547-
}
548-
$value = new IteratorArgument(array_map(array($this, 'resolveServices'), $value));
549-
} elseif (array_key_exists('=closure_proxy', $value)) {
550-
if (1 !== count($value)) {
551-
throw new InvalidArgumentException('Arguments typed "=closure_proxy" must have no sibling keys.');
552-
}
553-
if (!is_array($value = $value['=closure_proxy']) || array(0, 1) !== array_keys($value)) {
554-
throw new InvalidArgumentException('Arguments typed "=closure_proxy" must be arrays of [@service, method].');
541+
if ($value instanceof TaggedValue) {
542+
$argument = $value->getValue();
543+
if ('iterator' === $value->getTag()) {
544+
if (!is_array($argument)) {
545+
throw new InvalidArgumentException('"!iterator" tag only accepts sequences.');
555546
}
556-
if (!is_string($value[0]) || !is_string($value[1]) || 0 !== strpos($value[0], '@') || 0 === strpos($value[0], '@@')) {
557-
throw new InvalidArgumentException('Arguments typed "=closure_proxy" must be arrays of [@service, method].');
547+
548+
return new IteratorArgument(array_map(array($this, 'resolveServices'), $argument));
549+
}
550+
if ('closure_proxy' === $value->getTag()) {
551+
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], '@@')) {
552+
throw new InvalidArgumentException('"!closure_proxy" tagged values must be arrays of [@service, method].');
558553
}
559-
if (0 === strpos($value[0], '@?')) {
560-
$value[0] = substr($value[0], 2);
554+
555+
if (0 === strpos($argument[0], '@?')) {
556+
$argument[0] = substr($argument[0], 2);
561557
$invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;
562558
} else {
563-
$value[0] = substr($value[0], 1);
559+
$argument[0] = substr($argument[0], 1);
564560
$invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
565561
}
566-
$value = new ClosureProxyArgument($value[0], $value[1], $invalidBehavior);
567-
} else {
568-
$value = array_map(array($this, 'resolveServices'), $value);
562+
563+
return new ClosureProxyArgument($argument[0], $argument[1], $invalidBehavior);
569564
}
565+
566+
throw new InvalidArgumentException(sprintf('Unsupported tag "!%s".', $value->getTag()));
567+
}
568+
569+
if (is_array($value)) {
570+
$value = array_map(array($this, 'resolveServices'), $value);
570571
} elseif (is_string($value) && 0 === strpos($value, '@=')) {
571572
return new Expression(substr($value, 2));
572573
} elseif (is_string($value) && 0 === strpos($value, '@')) {

src/Symfony/Component/DependencyInjection/Tests/Dumper/YamlDumperTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\DependencyInjection\ContainerBuilder;
1515
use Symfony\Component\DependencyInjection\Dumper\YamlDumper;
1616
use Symfony\Component\Yaml\Yaml;
17+
use Symfony\Component\Yaml\Parser;
1718

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

65-
private function assertEqualYamlStructure($yaml, $expected, $message = '')
66+
private function assertEqualYamlStructure($expected, $yaml, $message = '')
6667
{
67-
$this->assertEquals(Yaml::parse($expected), Yaml::parse($yaml), $message);
68+
$parser = new Parser();
69+
70+
$this->assertEquals($parser->parse($expected, Yaml::PARSE_CUSTOM_TAGS), $parser->parse($yaml, Yaml::PARSE_CUSTOM_TAGS), $message);
6871
}
6972
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services9.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,12 @@ services:
109109
factory: ['@factory_simple', getInstance]
110110
lazy_context:
111111
class: LazyContext
112-
arguments: [{ '=iterator': [foo, '@foo.baz', { '%foo%': 'foo is %foo%', foobar: '%foo%' }, true, '@service_container'] }]
112+
arguments: [!iterator [foo, '@foo.baz', { '%foo%': 'foo is %foo%', foobar: '%foo%' }, true, '@service_container']]
113113
lazy_context_ignore_invalid_ref:
114114
class: LazyContext
115-
arguments: [{ '=iterator': ['@foo.baz', '@?invalid'] }]
115+
arguments: [!iterator ['@foo.baz', '@?invalid']]
116116
closure_proxy:
117117
class: BarClass
118-
arguments: [{ '=closure_proxy': ['@closure_proxy', getBaz] }]
118+
arguments: [!closure_proxy ['@closure_proxy', getBaz]]
119119
alias_for_foo: '@foo'
120120
alias_for_alias: '@foo'

src/Symfony/Component/DependencyInjection/composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"php": ">=5.5.9"
2020
},
2121
"require-dev": {
22-
"symfony/yaml": "~3.2",
22+
"symfony/yaml": "~3.3",
2323
"symfony/config": "~2.8|~3.0",
2424
"symfony/expression-language": "~2.8|~3.0"
2525
},
@@ -30,7 +30,7 @@
3030
"symfony/proxy-manager-bridge": "Generate service proxies to lazy load them"
3131
},
3232
"conflict": {
33-
"symfony/yaml": "<3.2"
33+
"symfony/yaml": "<3.3"
3434
},
3535
"autoload": {
3636
"psr-4": { "Symfony\\Component\\DependencyInjection\\": "" },

0 commit comments

Comments
 (0)