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

Skip to content

Commit 50e752b

Browse files
committed
feature #14984 [DependencyInjection] Deprecate scope concept (dosten)
This PR was merged into the 2.8 branch. Discussion ---------- [DependencyInjection] Deprecate scope concept | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | License | MIT This PR mark as deprecated the concept of scopes in the DI container. See #10557 (comment). Also adds a new `shared` flag to the service definitions in replacement of the `prototype` scope. Commits ------- 6c4a676 Add "shared" flag and deprecate scopes concept
2 parents dd7583d + 6c4a676 commit 50e752b

File tree

84 files changed

+515
-90
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+515
-90
lines changed

UPGRADE-2.8.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,74 @@ Translator
6565
$messages = array_replace_recursive($catalogue->all(), $messages);
6666
}
6767
```
68+
69+
DependencyInjection
70+
-------------------
71+
72+
* The concept of scopes were deprecated, the deprecated methods are:
73+
74+
- `Symfony\Component\DependencyInjection\ContainerBuilder::getScopes()`
75+
- `Symfony\Component\DependencyInjection\ContainerBuilder::getScopeChildren()`
76+
- `Symfony\Component\DependencyInjection\ContainerInterface::enterScope()`
77+
- `Symfony\Component\DependencyInjection\ContainerInterface::leaveScope()`
78+
- `Symfony\Component\DependencyInjection\ContainerInterface::addScope()`
79+
- `Symfony\Component\DependencyInjection\ContainerInterface::hasScope()`
80+
- `Symfony\Component\DependencyInjection\ContainerInterface::isScopeActive()`
81+
- `Symfony\Component\DependencyInjection\Definition::setScope()`
82+
- `Symfony\Component\DependencyInjection\Definition::getScope()`
83+
- `Symfony\Component\DependencyInjection\Reference::isStrict()`
84+
85+
Also, the `$scope` and `$strict` parameters of `Symfony\Component\DependencyInjection\ContainerInterface::set()` and `Symfony\Component\DependencyInjection\Reference` respectively were deprecated.
86+
87+
* A new `shared` flag has been added to the service definition
88+
in replacement of the `prototype` scope.
89+
90+
Before:
91+
92+
```php
93+
use Symfony\Component\DependencyInjection\ContainerBuilder;
94+
95+
$container = new ContainerBuilder();
96+
$container
97+
->register('foo', 'stdClass')
98+
->setScope(ContainerBuilder::SCOPE_PROTOTYPE)
99+
;
100+
```
101+
102+
```yml
103+
services:
104+
foo:
105+
class: stdClass
106+
scope: prototype
107+
```
108+
109+
```xml
110+
<services>
111+
<service id="foo" class="stdClass" scope="prototype" />
112+
</services>
113+
```
114+
115+
After:
116+
117+
```php
118+
use Symfony\Component\DependencyInjection\ContainerBuilder;
119+
120+
$container = new ContainerBuilder();
121+
$container
122+
->register('foo', 'stdClass')
123+
->setShared(false)
124+
;
125+
```
126+
127+
```yml
128+
services:
129+
foo:
130+
class: stdClass
131+
shared: false
132+
```
133+
134+
```xml
135+
<services>
136+
<service id="foo" class="stdClass" shared="false" />
137+
</services>
138+
```

src/Symfony/Bridge/ProxyManager/LazyProxy/PhpDumper/ProxyDumper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ public function getProxyFactoryCode(Definition $definition, $id)
6868
{
6969
$instantiation = 'return';
7070

71-
if (ContainerInterface::SCOPE_CONTAINER === $definition->getScope()) {
71+
if ($definition->isShared() && ContainerInterface::SCOPE_CONTAINER === $definition->getScope(false)) {
7272
$instantiation .= " \$this->services['$id'] =";
73-
} elseif (ContainerInterface::SCOPE_PROTOTYPE !== $scope = $definition->getScope()) {
73+
} elseif ($definition->isShared() && ContainerInterface::SCOPE_PROTOTYPE !== $scope = $definition->getScope(false)) {
7474
$instantiation .= " \$this->services['$id'] = \$this->scopedServices['$scope']['$id'] =";
7575
}
7676

src/Symfony/Bridge/ProxyManager/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
],
1818
"require": {
1919
"php": ">=5.3.9",
20-
"symfony/dependency-injection": "~2.3|~3.0.0",
20+
"symfony/dependency-injection": "~2.8|~3.0.0",
2121
"ocramius/proxy-manager": "~0.4|~1.0"
2222
},
2323
"require-dev": {

src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/JsonDescriptor.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,16 @@ private function getContainerDefinitionData(Definition $definition, $omitTags =
213213
{
214214
$data = array(
215215
'class' => (string) $definition->getClass(),
216-
'scope' => $definition->getScope(),
216+
'scope' => $definition->getScope(false),
217217
'public' => $definition->isPublic(),
218218
'synthetic' => $definition->isSynthetic(),
219219
'lazy' => $definition->isLazy(),
220220
);
221221

222+
if (method_exists($definition, 'isShared')) {
223+
$data['shared'] = $definition->isShared();
224+
}
225+
222226
if (method_exists($definition, 'isSynchronized')) {
223227
$data['synchronized'] = $definition->isSynchronized(false);
224228
}

src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/MarkdownDescriptor.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,16 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o
179179
protected function describeContainerDefinition(Definition $definition, array $options = array())
180180
{
181181
$output = '- Class: `'.$definition->getClass().'`'
182-
."\n".'- Scope: `'.$definition->getScope().'`'
182+
."\n".'- Scope: `'.$definition->getScope(false).'`'
183183
."\n".'- Public: '.($definition->isPublic() ? 'yes' : 'no')
184184
."\n".'- Synthetic: '.($definition->isSynthetic() ? 'yes' : 'no')
185185
."\n".'- Lazy: '.($definition->isLazy() ? 'yes' : 'no')
186186
;
187187

188+
if (method_exists($definition, 'isShared')) {
189+
$output .= "\n".'- Shared: '.($definition->isShared() ? 'yes' : 'no');
190+
}
191+
188192
if (method_exists($definition, 'isSynchronized')) {
189193
$output .= "\n".'- Synchronized: '.($definition->isSynchronized(false) ? 'yes' : 'no');
190194
}

src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/TextDescriptor.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o
174174
$serviceIds = isset($options['tag']) && $options['tag'] ? array_keys($builder->findTaggedServiceIds($options['tag'])) : $builder->getServiceIds();
175175
$maxTags = array();
176176

177-
foreach ($serviceIds as $key => $serviceId) {
177+
foreach ($serviceIds as $key => $serviceId) {
178178
$definition = $this->resolveServiceDefinition($builder, $serviceId);
179179
if ($definition instanceof Definition) {
180180
// filter out private services unless shown explicitly
@@ -261,10 +261,13 @@ protected function describeContainerDefinition(Definition $definition, array $op
261261
$description[] = '<comment>Tags</comment> -';
262262
}
263263

264-
$description[] = sprintf('<comment>Scope</comment> %s', $definition->getScope());
264+
$description[] = sprintf('<comment>Scope</comment> %s', $definition->getScope(false));
265265
$description[] = sprintf('<comment>Public</comment> %s', $definition->isPublic() ? 'yes' : 'no');
266266
$description[] = sprintf('<comment>Synthetic</comment> %s', $definition->isSynthetic() ? 'yes' : 'no');
267267
$description[] = sprintf('<comment>Lazy</comment> %s', $definition->isLazy() ? 'yes' : 'no');
268+
if (method_exists($definition, 'isShared')) {
269+
$description[] = sprintf('<comment>Shared</comment> %s', $definition->isShared() ? 'yes' : 'no');
270+
}
268271
if (method_exists($definition, 'isSynchronized')) {
269272
$description[] = sprintf('<comment>Synchronized</comment> %s', $definition->isSynchronized(false) ? 'yes' : 'no');
270273
}

src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/XmlDescriptor.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,10 +362,13 @@ private function getContainerDefinitionDocument(Definition $definition, $id = nu
362362
}
363363
}
364364

365-
$serviceXML->setAttribute('scope', $definition->getScope());
365+
$serviceXML->setAttribute('scope', $definition->getScope(false));
366366
$serviceXML->setAttribute('public', $definition->isPublic() ? 'true' : 'false');
367367
$serviceXML->setAttribute('synthetic', $definition->isSynthetic() ? 'true' : 'false');
368368
$serviceXML->setAttribute('lazy', $definition->isLazy() ? 'true' : 'false');
369+
if (method_exists($definition, 'isShared')) {
370+
$serviceXML->setAttribute('shared', $definition->isShared() ? 'true' : 'false');
371+
}
369372
if (method_exists($definition, 'isSynchronized')) {
370373
$serviceXML->setAttribute('synchronized', $definition->isSynchronized(false) ? 'true' : 'false');
371374
}

src/Symfony/Bundle/FrameworkBundle/Resources/config/test.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@
1313
</parameters>
1414

1515
<services>
16-
<service id="test.client" class="%test.client.class%" scope="prototype">
16+
<service id="test.client" class="%test.client.class%" shared="false">
1717
<argument type="service" id="kernel" />
1818
<argument>%test.client.parameters%</argument>
1919
<argument type="service" id="test.client.history" />
2020
<argument type="service" id="test.client.cookiejar" />
2121
</service>
2222

23-
<service id="test.client.history" class="%test.client.history.class%" scope="prototype" />
23+
<service id="test.client.history" class="%test.client.history.class%" shared="false" />
2424

25-
<service id="test.client.cookiejar" class="%test.client.cookiejar.class%" scope="prototype" />
25+
<service id="test.client.cookiejar" class="%test.client.cookiejar.class%" shared="false" />
2626

2727
<service id="test.session.listener" class="%test.session.listener.class%">
2828
<argument type="service" id="service_container" />

src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/builder_1_public.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"public": true,
77
"synthetic": false,
88
"lazy": true,
9+
"shared": true,
910
"synchronized": false,
1011
"abstract": true,
1112
"file": null,

src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/builder_1_public.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ definition_1
1212
- Public: yes
1313
- Synthetic: no
1414
- Lazy: yes
15+
- Shared: yes
1516
- Synchronized: no
1617
- Abstract: yes
1718
- Factory Class: `Full\Qualified\FactoryClass`

src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/builder_1_public.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<container>
33
<alias id="alias_1" service="service_1" public="true"/>
44
<alias id="alias_2" service="service_2" public="false"/>
5-
<definition id="definition_1" class="Full\Qualified\Class1" scope="container" public="true" synthetic="false" lazy="true" synchronized="false" abstract="true" file="">
5+
<definition id="definition_1" class="Full\Qualified\Class1" scope="container" public="true" synthetic="false" lazy="true" shared="true" synchronized="false" abstract="true" file="">
66
<factory class="Full\Qualified\FactoryClass" method="get"/>
77
</definition>
88
<service id="service_container" class="Symfony\Component\DependencyInjection\ContainerBuilder"/>

src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/builder_1_services.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"public": true,
77
"synthetic": false,
88
"lazy": true,
9+
"shared": true,
910
"synchronized": false,
1011
"abstract": true,
1112
"file": null,
@@ -21,6 +22,7 @@
2122
"public": false,
2223
"synthetic": true,
2324
"lazy": false,
25+
"shared": true,
2426
"synchronized": false,
2527
"abstract": false,
2628
"file": "\/path\/to\/file",

src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/builder_1_services.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ definition_1
1212
- Public: yes
1313
- Synthetic: no
1414
- Lazy: yes
15+
- Shared: yes
1516
- Synchronized: no
1617
- Abstract: yes
1718
- Factory Class: `Full\Qualified\FactoryClass`
@@ -25,6 +26,7 @@ definition_2
2526
- Public: no
2627
- Synthetic: yes
2728
- Lazy: no
29+
- Shared: yes
2830
- Synchronized: no
2931
- Abstract: no
3032
- File: `/path/to/file`

src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/builder_1_services.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
<container>
33
<alias id="alias_1" service="service_1" public="true"/>
44
<alias id="alias_2" service="service_2" public="false"/>
5-
<definition id="definition_1" class="Full\Qualified\Class1" scope="container" public="true" synthetic="false" lazy="true" synchronized="false" abstract="true" file="">
5+
<definition id="definition_1" class="Full\Qualified\Class1" scope="container" public="true" synthetic="false" lazy="true" shared="true" synchronized="false" abstract="true" file="">
66
<factory class="Full\Qualified\FactoryClass" method="get"/>
77
</definition>
8-
<definition id="definition_2" class="Full\Qualified\Class2" scope="container" public="false" synthetic="true" lazy="false" synchronized="false" abstract="false" file="/path/to/file">
8+
<definition id="definition_2" class="Full\Qualified\Class2" scope="container" public="false" synthetic="true" lazy="false" shared="true" synchronized="false" abstract="false" file="/path/to/file">
99
<factory service="factory.service" method="get"/>
1010
<tags>
1111
<tag name="tag1">

src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/builder_1_tag1.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"public": false,
77
"synthetic": true,
88
"lazy": false,
9+
"shared": true,
910
"synchronized": false,
1011
"abstract": false,
1112
"file": "\/path\/to\/file",

src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/builder_1_tag1.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ definition_2
1212
- Public: no
1313
- Synthetic: yes
1414
- Lazy: no
15+
- Shared: yes
1516
- Synchronized: no
1617
- Abstract: no
1718
- File: `/path/to/file`

src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/builder_1_tag1.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<container>
3-
<definition id="definition_2" class="Full\Qualified\Class2" scope="container" public="false" synthetic="true" lazy="false" synchronized="false" abstract="false" file="/path/to/file">
3+
<definition id="definition_2" class="Full\Qualified\Class2" scope="container" public="false" synthetic="true" lazy="false" shared="true" synchronized="false" abstract="false" file="/path/to/file">
44
<factory service="factory.service" method="get"/>
55
<tags>
66
<tag name="tag1">

src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/builder_1_tags.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"public": false,
77
"synthetic": true,
88
"lazy": false,
9+
"shared": true,
910
"synchronized": false,
1011
"abstract": false,
1112
"file": "\/path\/to\/file",
@@ -20,6 +21,7 @@
2021
"public": false,
2122
"synthetic": true,
2223
"lazy": false,
24+
"shared": true,
2325
"synchronized": false,
2426
"abstract": false,
2527
"file": "\/path\/to\/file",

src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/builder_1_tags.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ definition_2
1212
- Public: no
1313
- Synthetic: yes
1414
- Lazy: no
15+
- Shared: yes
1516
- Synchronized: no
1617
- Abstract: no
1718
- File: `/path/to/file`
@@ -30,6 +31,7 @@ definition_2
3031
- Public: no
3132
- Synthetic: yes
3233
- Lazy: no
34+
- Shared: yes
3335
- Synchronized: no
3436
- Abstract: no
3537
- File: `/path/to/file`

src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/builder_1_tags.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<container>
33
<tag name="tag1">
4-
<definition id="definition_2" class="Full\Qualified\Class2" scope="container" public="false" synthetic="true" lazy="false" synchronized="false" abstract="false" file="/path/to/file">
4+
<definition id="definition_2" class="Full\Qualified\Class2" scope="container" public="false" synthetic="true" lazy="false" shared="true" synchronized="false" abstract="false" file="/path/to/file">
55
<factory service="factory.service" method="get"/>
66
</definition>
77
</tag>
88
<tag name="tag2">
9-
<definition id="definition_2" class="Full\Qualified\Class2" scope="container" public="false" synthetic="true" lazy="false" synchronized="false" abstract="false" file="/path/to/file">
9+
<definition id="definition_2" class="Full\Qualified\Class2" scope="container" public="false" synthetic="true" lazy="false" shared="true" synchronized="false" abstract="false" file="/path/to/file">
1010
<factory service="factory.service" method="get"/>
1111
</definition>
1212
</tag>

src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/definition_1.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"public": true,
55
"synthetic": false,
66
"lazy": true,
7+
"shared": true,
78
"synchronized": false,
89
"abstract": true,
910
"file": null,

src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/definition_1.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
- Public: yes
44
- Synthetic: no
55
- Lazy: yes
6+
- Shared: yes
67
- Synchronized: no
78
- Abstract: yes
89
- Factory Class: `Full\Qualified\FactoryClass`

src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/definition_1.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<comment>Public</comment> yes
66
<comment>Synthetic</comment> no
77
<comment>Lazy</comment> yes
8+
<comment>Shared</comment> yes
89
<comment>Synchronized</comment> no
910
<comment>Abstract</comment> yes
1011
<comment>Factory Class</comment> Full\Qualified\FactoryClass
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<definition class="Full\Qualified\Class1" scope="container" public="true" synthetic="false" lazy="true" synchronized="false" abstract="true" file="">
2+
<definition class="Full\Qualified\Class1" scope="container" public="true" synthetic="false" lazy="true" shared="true" synchronized="false" abstract="true" file="">
33
<factory class="Full\Qualified\FactoryClass" method="get"/>
44
</definition>

src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/definition_2.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"public": false,
55
"synthetic": true,
66
"lazy": false,
7+
"shared": true,
78
"synchronized": false,
89
"abstract": false,
910
"file": "\/path\/to\/file",

src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/definition_2.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
- Public: no
44
- Synthetic: yes
55
- Lazy: no
6+
- Shared: yes
67
- Synchronized: no
78
- Abstract: no
89
- File: `/path/to/file`

src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/definition_2.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<comment>Public</comment> no
99
<comment>Synthetic</comment> yes
1010
<comment>Lazy</comment> no
11+
<comment>Shared</comment> yes
1112
<comment>Synchronized</comment> no
1213
<comment>Abstract</comment> no
1314
<comment>Required File</comment> /path/to/file

src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/definition_2.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<definition class="Full\Qualified\Class2" scope="container" public="false" synthetic="true" lazy="false" synchronized="false" abstract="false" file="/path/to/file">
2+
<definition class="Full\Qualified\Class2" scope="container" public="false" synthetic="true" lazy="false" shared="true" synchronized="false" abstract="false" file="/path/to/file">
33
<factory service="factory.service" method="get"/>
44
<tags>
55
<tag name="tag1">

0 commit comments

Comments
 (0)