8000 [DependencyInjection] Tests + refacto for "instanceof" definitions · symfony/symfony@28b7922 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 28b7922

Browse files
dunglasnicolas-grekas
authored andcommitted
[DependencyInjection] Tests + refacto for "instanceof" definitions
1 parent 9bf7f60 commit 28b7922

File tree

8 files changed

+92
-13
lines changed

8 files changed

+92
-13
lines changed

src/Symfony/Component/DependencyInjection/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
3.3.0
55
-----
66

7+
* [EXPERIMENTAL] added "instanceof" section for local interface-defined configs
78
* added `ContainerBuilder::getReflectionClass()` for retrieving and tracking reflection class info
89
* deprecated `ContainerBuilder::getClassResource()`, use `ContainerBuilder::getReflectionClass()` or `ContainerBuilder::addObjectResource()` instead
910
* added `ContainerBuilder::fileExists()` for checking and tracking file or directory existence

src/Symfony/Component/DependencyInjection/ChildDefinition.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class ChildDefinition extends Definition
2626
private $changes = array();
2727

2828
/**
29-
* @param string|null $parent The id of Definition instance to decorate
29+
* @param string $parent The id of Definition instance to decorate
3030
*/
3131
public function __construct($parent)
3232
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ private function parseDefinition(\DOMElement $service, $file, array $defaults =
227227
}
228228

229229
if ($this->isLoadingInstanceof) {
230-
$definition = new ChildDefinition(null);
230+
$definition = new ChildDefinition('');
231231
} elseif ($parent = $service->getAttribute('parent')) {
232232
$definition = new ChildDefinition($parent);
233233

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

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -200,20 +200,13 @@ private function parseDefinitions(array $content, $file)
200200
*/
201201
private function parseDefaults(array &$content, $file)
202202
{
203-
if (!isset($content['services']['_defaults'])) {
203+
if (!$this->isUnderscoredParamValid($content, '_defaults', $file)) {
204204
return array();
205205
}
206-
if (!is_array($defaults = $content['services']['_defaults'])) {
207-
throw new InvalidArgumentException(sprintf('Service defaults must be an array, "%s" given in "%s".', gettype($defaults), $file));
208-
}
209-
if (isset($defaults['alias']) || isset($defaults['class']) || isset($defaults['factory'])) {
210-
@trigger_error('Giving a service the "_defaults" name is deprecated since Symfony 3.3 and will be forbidden in 4.0. Rename your service.', E_USER_DEPRECATED);
211206

212-
return array();
213-
}
214-
215-
$defaultKeys = array('public', 'tags', 'inherit_tags', 'autowire');
207+
$defaults = $content['services']['_defaults'];
216208
unset($content['services']['_defaults']);
209+
$defaultKeys = array('public', 'tags', 'inherit_tags', 'autowire');
217210

218211
foreach ($defaults as $key => $default) {
219212
if (!in_array($< 6D40 /span>key, $defaultKeys)) {
@@ -252,6 +245,25 @@ private function parseDefaults(array &$content, $file)
252245
return $defaults;
253246
}
254247

248+
private function isUnderscoredParamValid($content, $name, $file)
249+
{
250+
if (!isset($content['services'][$name])) {
251+
return false;
252+
}
253+
254+
if (!is_array($underscoreParam = $content['services'][$name])) {
255+
throw new InvalidArgumentException(sprintf('Service "%s" key must be an array, "%s" given in "%s".', $name, gettype($underscoreParam), $file));
256+
}
257+
258+
if (isset($underscoreParam['alias']) || isset($underscoreParam['class']) || isset($underscoreParam['factory'])) {
259+
@trigger_error(sprintf('Giving a service the "%s" name is deprecated since Symfony 3.3 and will be forbidden in 4.0. Rename your service.', $name), E_USER_DEPRECATED);
260+
261+
return false;
262+
}
263+
264+
return true;
265+
}
266+
255267
/**
256268
* Parses a definition.
257269
*
@@ -299,7 +311,7 @@ private function parseDefinition($id, $service, $file, array $defaults)
299311
}
300312

301313
if ($this->isLoadingInstanceof) {
302-
$definition = new ChildDefinition(null);
314+
$definition = new ChildDefinition('');
303315
} elseif (isset($service['parent'])) {
304316
$definition = new ChildDefinition($service['parent']);
305317

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
3+
<services>
4+
<instanceof>
5+
<service id="Symfony\Component\DependencyInjection\Tests\Loader\BarInterface" lazy="true">
6+
<autowire>set*</autowire>
7+
<tag name="foo" />
8+
<tag name="bar" />
9+
</service>
10+
</instanceof>
11+
12+
<service id="Symfony\Component\DependencyInjection\Tests\Loader\Bar" class="Symfony\Component\DependencyInjection\Tests\Loader\Bar" autowire="true" />
13+
</services>
14+
</container>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
services:
2+
_instanceof:
3+
Symfony\Component\DependencyInjection\Tests\Loader\FooInterface:
4+
autowire: true
5+
lazy: true
6+
tags:
7+
- { name: foo }
8+
- { name: bar }
9+
10+
Symfony\Component\DependencyInjection\Tests\Loader\Foo: ~

src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,4 +670,25 @@ public function testDefaultsWithAutowiredMethods()
670670
$this->assertSame(array('setFoo'), $container->getDefinition('no_defaults_child')->getAutowiredMethods());
671671
$this->assertSame(array(), $container->getDefinition('with_defaults_child')->getAutowiredMethods());
672672
}
673+
674+
public function testInstanceof()
675+
{
676+
$container = new ContainerBuilder();
677+
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
678+
$loader->load('services_instanceof.xml');
679+
$container->compile();
680+
681+
$definition = $container->getDefinition(Bar::class);
682+
$this->assertSame(array('__construct', 'set*'), $definition->getAutowiredMethods());
683+
$this->assertTrue($definition->isLazy());
684+
$this->assertSame(array('foo' => array(array()), 'bar' => array(array())), $definition->getTags());
685+
}
686+
}
687+
688+
interface BarInterface
689+
{
690+
}
691+
692+
class Bar implements BarInterface
693+
{
673694
}

src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,19 @@ public function testGetter()
417417
$this->assertEquals(array('getbar' => array('bar' => new Reference('bar'))), $container->getDefinition('foo')->getOverriddenGetters());
418418
}
419419

420+
public function testInstanceof()
421+
{
422+
$container = new ContainerBuilder();
423+
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
424+
$loader->load('services_instanceof.yml');
425+
$container->compile();
426+
427+
$definition = $container->getDefinition(Foo::class);
428+
$this->assertTrue($definition->isAutowired());
429+
$this->assertTrue($definition->isLazy());
430+
$this->assertSame(array('foo' => array(array()), 'bar' => array(array())), $definition->getTags());
431+
}
432+
420433
/**
421434
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
422435
* @expectedExceptionMessage The value of the "decorates" option for the "bar" service must be the id of the service without the "@" prefix (replace "@foo" with "foo").
@@ -437,3 +450,11 @@ public function testInvalidTagsWithDefaults()
437450
$loader->load('services31_invalid_tags.yml');
438451
}
439452
}
453+
454+
interface FooInterface
455+
{
456+
}
457+
458+
class Foo implements FooInterface
459+
{
460+
}

0 commit comments

Comments
 (0)
0