You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feature #21071 [DI] Add "inherit-tags" with configurable defaults + same for "public", "tags" & "autowire" (nicolas-grekas, ogizanagi)
This PR was merged into the 3.3-dev branch.
Discussion
----------
[DI] Add "inherit-tags" with configurable defaults + same for "public", "tags" & "autowire"
| Q | A
| ------------- | ---
| Branch? | master
| Bug fix? | no
| New feature? | yes
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | #20048
| License | MIT
| Doc PR | -
Instead of making services private by default (#20048), which is going to create a big migration burden that might not be worth it, I'd like to propose the idea of changing the default for the current file.
Having a place to redefine some defaults, this can also be used to enable autowiring for a file, making it much lighter in the end.
This PR handles defaults for "public", "autowired" and "tags". Not sure the other options need a configurable default. Please comment if you think otherwise.
Short example (the feat is more interesting with a longer list of services):
```yaml
services:
_defaults:
public: false
autowire: ['_construct', 'set*']
foo:
class: Foo
```
```xml
<?xml version="1.0" encoding="utf-8"?>
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/serviceshttp://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<defaults public="false">
<autowire>__construct</autowire>
<tag name="foo"/>
</defaults>
</services>
</container>
```
ping @dunglas@weaverryan
Commits
-------
beec1cf [DI] Allow definitions to inherit tags from parent context
05f24d5 [DI] Add "defaults" tag to XML services configuration
7b4a18b [DI] Add "_defaults" key to Yaml services configuration
thrownewInvalidArgumentException(sprintf('The "autowire" attribute cannot be used together with "<autowire>" tags for tag "<defaults>" in %s.', $file));
165
+
}
166
+
if (XmlUtils::phpize($defaultsNode->getAttribute('autowire'))) {
@@ -148,9 +149,56 @@ private function parseDefinitions($content, $file)
148
149
if (!is_array($content['services'])) {
149
150
thrownewInvalidArgumentException(sprintf('The "services" key should contain an array in %s. Check your YAML syntax.', $file));
150
151
}
152
+
if (isset($content['services']['_defaults'])) {
153
+
if (!is_array($defaults = $content['services']['_defaults'])) {
154
+
thrownewInvalidArgumentException(sprintf('Service defaults must be an array, "%s" given in "%s".', gettype($defaults), $file));
155
+
}
156
+
if (isset($defaults['alias']) || isset($defaults['class']) || isset($defaults['factory'])) {
157
+
@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);
thrownewInvalidArgumentException(sprintf('The configuration key "%s" cannot be used to define a default value in "%s". Allowed keys are "%s".', $key, $file, implode('", "', $defaultKeys)));
166
+
}
167
+
}
168
+
if (isset($defaults['tags'])) {
169
+
if (!is_array($tags = $defaults['tags'])) {
170
+
thrownewInvalidArgumentException(sprintf('Parameter "tags" in "_defaults" must be an array in %s. Check your YAML syntax.', $file));
171
+
}
172
+
173
+
foreach ($tagsas$tag) {
174
+
if (!is_array($tag)) {
175
+
$tag = array('name' => $tag);
176
+
}
177
+
178
+
if (!isset($tag['name'])) {
179
+
thrownewInvalidArgumentException(sprintf('A "tags" entry in "_defaults" is missing a "name" key in %s.', $file));
180
+
}
181
+
$name = $tag['name'];
182
+
unset($tag['name']);
183
+
184
+
if (!is_string($name) || '' === $name) {
185
+
thrownewInvalidArgumentException(sprintf('The tag name in "_defaults" must be a non-empty string in %s.', $file));
186
+
}
187
+
188
+
foreach ($tagas$attribute => $value) {
189
+
if (!is_scalar($value) && null !== $value) {
190
+
thrownewInvalidArgumentException(sprintf('Tag "%s", attribute "%s" in "_defaults" must be of a scalar-type in %s. Check your YAML syntax.', $name, $attribute, $file));
thrownewInvalidArgumentException(sprintf('Parameter "tags" must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file));
268
336
}
269
337
270
-
foreach ($service['tags']as$tag) {
338
+
foreach ($tagsas$tag) {
271
339
if (!is_array($tag)) {
272
340
$tag = array('name' => $tag);
273
341
}
274
342
275
343
if (!isset($tag['name'])) {
276
344
thrownewInvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in %s.', $id, $file));
277
345
}
346
+
$name = $tag['name'];
347
+
unset($tag['name']);
278
348
279
-
if (!is_string($tag['name']) || '' === $tag['name']) {
349
+
if (!is_string($name) || '' === $name) {
280
350
thrownewInvalidArgumentException(sprintf('The tag name for service "%s" in %s must be a non-empty string.', $id, $file));
281
351
}
282
352
283
-
$name = $tag['name'];
284
-
unset($tag['name']);
285
-
286
353
foreach ($tagas$attribute => $value) {
287
354
if (!is_scalar($value) && null !== $value) {
288
355
thrownewInvalidArgumentException(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));
@@ -303,11 +370,12 @@ private function parseDefinition($id, $service, $file)
0 commit comments