Skip to content

Commit 346a180

Browse files
[DI] Add "_defaults" key to Yaml services configuration
1 parent d0e8476 commit 346a180

File tree

3 files changed

+122
-16
lines changed

3 files changed

+122
-16
lines changed

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

Lines changed: 67 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,56 @@ private function parseDefinitions($content, $file)
146146
if (!is_array($content['services'])) {
147147
throw new InvalidArgumentException(sprintf('The "services" key should contain an array in %s. Check your YAML syntax.', $file));
148148
}
149+
if (isset($content['services']['_defaults'])) {
150+
if (!is_array($defaults = $content['services']['_defaults'])) {
151+
throw new InvalidArgumentException(sprintf('Service defaults must be an array, "%s" given in "%s".', gettype($defaults), $file));
152+
}
153+
if (isset($defaults['alias']) || isset($defaults['class']) || isset($defaults['factory'])) {
154+
@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);
155+
$defaults = array();
156+
} else {
157+
$defaultKeys = array('public', 'tags', 'autowire');
158+
unset($content['services']['_defaults']);
159+
160+
foreach ($defaults as $key => $default) {
161+
if (!in_array($key, $defaultKeys)) {
162+
throw new InvalidArgumentException(sprintf('The configuration key "%s" cannot be used to define a default value in "%s". Allowed keys are "%s".', $key, $file, implode('", "', $defaultKeys)));
163+
}
164+
}
165+
if (isset($defaults['tags'])) {
166+
if (!is_array($tags = $defaults['tags'])) {
167+
throw new InvalidArgumentException(sprintf('Parameter "tags" in "_defaults" must be an array in %s. Check your YAML syntax.', $file));
168+
}
169+
170+
foreach ($tags as $tag) {
171+
if (!is_array($tag)) {
172+
$tag = array('name' => $tag);
173+
}
174+
175+
if (!isset($tag['name'])) {
176+
throw new InvalidArgumentException(sprintf('A "tags" entry in "_defaults" is missing a "name" key in %s.', $file));
177+
}
178+
$name = $tag['name'];
179+
unset($tag['name']);
180+
181+
if (!is_string($name) || '' === $name) {
182+
throw new InvalidArgumentException(sprintf('The tag name in "_defaults" must be a non-empty string in %s.', $file));
183+
}
184+
185+
foreach ($tag as $attribute => $value) {
186+
if (!is_scalar($value) && null !== $value) {
187+
throw new InvalidArgumentException(sprintf('Tag "%s", attribute "%s" in "_defaults" must be of a scalar-type in %s. Check your YAML syntax.', $name, $attribute, $file));
188+
}
189+
}
190+
}
191+
}
192+
}
193+
} else {
194+
$defaults = array();
195+
}
149196

150197
foreach ($content['services'] as $id => $service) {
151-
$this->parseDefinition($id, $service, $file);
198+
$this->parseDefinition($id, $service, $file, $defaults);
152199
}
153200
}
154201

@@ -158,10 +205,11 @@ private function parseDefinitions($content, $file)
158205
* @param string $id
159206
* @param array $service
160207
* @param string $file
208+
* @param array $defaults
161209
*
162210
* @throws InvalidArgumentException When tags are invalid
163211
*/
164-
private function parseDefinition($id, $service, $file)
212+
private function parseDefinition($id, $service, $file, array $defaults)
165213
{
166214
if (is_string($service) && 0 === strpos($service, '@')) {
167215
$this->container->setAlias($id, substr($service, 1));
@@ -176,7 +224,7 @@ private function parseDefinition($id, $service, $file)
176224
static::checkDefinition($id, $service, $file);
177225

178226
if (isset($service['alias'])) {
179-
$public = !array_key_exists('public', $service) || (bool) $service['public'];
227+
$public = array_key_exists('public', $service) ? (bool) $service['public'] : !empty($defaults['public']);
180228
$this->container->setAlias($id, new Alias($service['alias'], $public));
181229

182230
foreach ($service as $key => $value) {
@@ -190,6 +238,7 @@ private function parseDefinition($id, $service, $file)
190238

191239
if (isset($service['parent'])) {
192240
$definition = new ChildDefinition($service['parent']);
241+
$defaults = array();
193242
} else {
194243
$definition = new Definition();
195244
}
@@ -210,8 +259,9 @@ private function parseDefinition($id, $service, $file)
210259
$definition->setLazy($service['lazy']);
211260
}
212261

213-
if (isset($service['public'])) {
214-
$definition->setPublic($service['public']);
262+
$public = isset($service['public']) ? $service['public'] : (isset($defaults['public']) ? $defaults['public'] : null);
263+
if (null !== $public) {
264+
$definition->setPublic($public);
215265
}
216266

217267
if (isset($service['abstract'])) {
@@ -260,27 +310,27 @@ private function parseDefinition($id, $service, $file)
260310
}
261311
}
262312

263-
if (isset($service['tags'])) {
264-
if (!is_array($service['tags'])) {
313+
$tags = isset($service['tags']) ? $service['tags'] : (isset($defaults['tags']) ? $defaults['tags'] : null);
314+
if (null !== $tags) {
315+
if (!is_array($tags)) {
265316
throw new InvalidArgumentException(sprintf('Parameter "tags" must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file));
266317
}
267318

268-
foreach ($service['tags'] as $tag) {
319+
foreach ($tags as $tag) {
269320
if (!is_array($tag)) {
270321
$tag = array('name' => $tag);
271322
}
272323

273324
if (!isset($tag['name'])) {
274325
throw new InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in %s.', $id, $file));
275326
}
327+
$name = $tag['name'];
328+
unset($tag['name']);
276329

277-
if (!is_string($tag['name']) || '' === $tag['name']) {
330+
if (!is_string($name) || '' === $name) {
278331
throw new InvalidArgumentException(sprintf('The tag name for service "%s" in %s must be a non-empty string.', $id, $file));
279332
}
280333

281-
$name = $tag['name'];
282-
unset($tag['name']);
283-
284334
foreach ($tag as $attribute => $value) {
285335
if (!is_scalar($value) && null !== $value) {
286336
throw new InvalidArgumentException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s", attribute "%s" in %s. Check your YAML syntax.', $id, $name, $attribute, $file));
@@ -301,11 +351,12 @@ private function parseDefinition($id, $service, $file)
301351
$definition->setDecoratedService($service['decorates'], $renameId, $priority);
302352
}
303353

304-
if (isset($service['autowire'])) {
305-
if (is_array($service['autowire'])) {
306-
$definition->setAutowiredMethods($service['autowire']);
354+
$autowire = isset($service['autowire']) ? $service['autowire'] : (isset($defaults['autowire']) ? $defaults['autowire'] : null);
355+
if (null !== $autowire) {
356+
if (is_array($autowire)) {
357+
$definition->setAutowiredMethods($autowire);
307358
} else {
308-
$definition->setAutowired($service['autowire']);
359+
$definition->setAutowired($autowire);
309360
}
310361
}
311362

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
services:
2+
_defaults:
3+
public: false
4+
autowire: true
5+
tags:
6+
- name: foo
7+
8+
with_defaults:
9+
class: Foo
10+
11+
with_null:
12+
class: Foo
13+
public: ~
14+
autowire: ~
15+
tags: ~
16+
17+
no_defaults:
18+
class: Foo
19+
public: true
20+
autowire: false
21+
tags: []
22+
23+
no_defaults_child:
24+
parent: no_defaults
25+
autowire: ~
26+
tags:
27+
- name: bar
28+
29+
with_defaults_child:
30+
parent: with_defaults

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,31 @@ public function testAutowire()
329329
$this->assertEquals(array('set*', 'bar'), $container->getDefinition('autowire_array')->getAutowiredMethods());
330330
}
331331

332+
public function testDefaults()
333+
{
334+
$container = new ContainerBuilder();
335+
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
336+
$loader->load('services28.yml');
337+
338+
$this->assertFalse($container->getDefinition('with_defaults')->isPublic());
339+
$this->assertFalse($container->getDefinition('with_null')->isPublic());
340+
$this->assertTrue($container->getDefinition('no_defaults')->isPublic());
341+
$this->assertTrue($container->getDefinition('no_defaults_child')->isPublic());
342+
$this->assertArrayNotHasKey('public', $container->getDefinition('no_defaults_child')->getChanges());
343+
344+
$this->assertSame(array('foo' => array(array())), $container->getDefinition('with_defaults')->getTags());
345+
$this->assertSame(array('foo' => array(array())), $container->getDefinition('with_null')->getTags());
346+
$this->assertSame(array(), $container->getDefinition('no_defaults')->getTags());
347+
$this->assertSame(array('bar' => array(array())), $container->getDefinition('no_defaults_child')->getTags());
348+
$this->assertSame(array(), $container->getDefinition('with_defaults_child')->getTags());
349+
350+
$this->assertTrue($container->getDefinition('with_defaults')->isAutowired());
351+
$this->assertTrue($container->getDefinition('with_null')->isAutowired());
352+
$this->assertFalse($container->getDefinition('no_defaults')->isAutowired());
353+
$this->assertFalse($container->getDefinition('no_defaults_child')->isAutowired());
354+
$this->assertArrayNotHasKey('autowire', $container->getDefinition('no_defaults_child')->getChanges());
355+
}
356+
332357
/**
333358
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
334359
* @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").

0 commit comments

Comments
 (0)