8000 [DependencyInjection] Deprecate scope concept by dosten · Pull Request #14984 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DependencyInjection] Deprecate scope concept #14984

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions UPGRADE-2.8.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,74 @@ Translator
$messages = array_replace_recursive($catalogue->all(), $messages);
}
```

DependencyInjection
-------------------

* The concept of scopes were deprecated, the deprecated methods are:

- `Symfony\Component\DependencyInjection\ContainerBuilder::getScopes()`
- `Symfony\Component\DependencyInjection\ContainerBuilder::getScopeChildren()`
- `Symfony\Component\DependencyInjection\ContainerInterface::enterScope()`
- `Symfony\Component\DependencyInjection\ContainerInterface::leaveScope()`
- `Symfony\Component\DependencyInjection\ContainerInterface::addScope()`
- `Symfony\Component\DependencyInjection\ContainerInterface::hasScope()`
- `Symfony\Component\DependencyInjection\ContainerInterface::isScopeActive()`
- `Symfony\Component\DependencyInjection\Definition::setScope()`
- `Symfony\Component\DependencyInjection\Definition::getScope()`
- `Symfony\Component\DependencyInjection\Reference::isStrict()`

Also, the `$scope` and `$strict` parameters of `Symfony\Component\DependencyInjection\ContainerInterface::set()` and `Symfony\Component\DependencyInjection\Reference` respectively were deprecated.

* A new `shared` flag has been added to the service definition
in replacement of the `prototype` scope.

Before:

```php
use Symfony\Component\DependencyInjection\ContainerBuilder;

$container = new ContainerBuilder();
$container
->register('foo', 'stdClass')
->setScope(ContainerBuilder::SCOPE_PROTOTYPE)
;
```

```yml
services:
foo:
class: stdClass
scope: prototype
```

```xml
<services>
<service id="foo" class="stdClass" scope="prototype" />
</services>
```

After:

```php
use Symfony\Component\DependencyInjection\ContainerBuilder;

$container = new ContainerBuilder();
$container
->register('foo', 'stdClass')
->setShared(false)
;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to show examples for YML and XML as well as those formats are the most used ones for configuring services.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, the structure should be?

Before:

  • PHP example
  • Yaml example
  • XML example

After

  • PHP example
  • Yaml example
  • XML example

?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me

```

```yml
services:
foo:
class: stdClass
shared: false
```

```xml
<services>
<service id="foo" class="stdClass" shared="false" />
</services>
```
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ public function getProxyFactoryCode(Definition $definition, $id)
{
$instantiation = 'return';

if (ContainerInterface::SCOPE_CONTAINER === $definition->getScope()) {
if ($definition->isShared() && ContainerInterface::SCOPE_CONTAINER === $definition->getScope(false)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will need to bump the DependencyInjection component version in composer.json of the bridge or check if the method exists first.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to check if the method exists.

$instantiation .= " \$this->services['$id'] =";
} elseif (ContainerInterface::SCOPE_PROTOTYPE !== $scope = $definition->getScope()) {
} elseif ($definition->isShared() && ContainerInterface::SCOPE_PROTOTYPE !== $scope = $definition->getScope(false)) {
$instantiation .= " \$this->services['$id'] = \$this->scopedServices['$scope']['$id'] =";
}

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/ProxyManager/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
],
"require": {
"php": ">=5.3.9",
"symfony/dependency-injection": "~2.3|~3.0.0",
"symfony/dependency-injection": "~2.8|~3.0.0",
"ocramius/proxy-manager": "~0.4|~1.0"
},
"require-dev": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,16 @@ private function getContainerDefinitionData(Definition $definition, $omitTags =
{
$data = array(
'class' => (string) $definition->getClass(),
'scope' => $definition->getScope(),
'scope' => $definition->getScope(false),
'public' => $definition->isPublic(),
'synthetic' => $definition->isSynthetic(),
'lazy' => $definition->isLazy(),
);

if (method_exists($definition, 'isShared')) {
$data['shared'] = $definition->isShared();
}

if (method_exists($definition, 'isSynchronized')) {
$data['synchronized'] = $definition->isSynchronized(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,16 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o
protected function describeContainerDefinition(Definition $definition, array $options = array())
{
$output = '- Class: `'.$definition->getClass().'`'
."\n".'- Scope: `'.$definition->getScope().'`'
."\n".'- Scope: `'.$definition->getScope(false).'`'
."\n".'- Public: '.($definition->isPublic() ? 'yes' : 'no')
."\n".'- Synthetic: '.($definition->isSynthetic() ? 'yes' : 'no')
."\n".'- Lazy: '.($definition->isLazy() ? 'yes' : 'no')
;

if (method_exists($definition, 'isShared')) {
$output .= "\n".'- Shared: '.($definition->isShared() ? 'yes' : 'no');
}

if (method_exists($definition, 'isSynchronized')) {
$output .= "\n".'- Synchronized: '.($definition->isSynchronized(false) ? 'yes' : 'no');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o
$serviceIds = isset($options['tag']) && $options['tag'] ? array_keys($builder->findTaggedServiceIds($options['tag'])) : $builder->getServiceIds();
$maxTags = array();

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

$description[] = sprintf('<comment>Scope</comment> %s', $definition->getScope());
$description[] = sprintf('<comment>Scope</comment> %s', $definition->getScope(false));
$description[] = sprintf('<comment>Public</comment> %s', $definition->isPublic() ? 'yes' : 'no');
$description[] = sprintf('<comment>Synthetic</comment> %s', $definition->isSynthetic() ? 'yes' : 'no');
$description[] = sprintf('<comment>Lazy</comment> %s', $definition->isLazy() ? 'yes' : 'no');
if (method_exists($definition, 'isShared')) {
$description[] = sprintf('<comment>Shared</comment> %s', $definition->isShared() ? 'yes' : 'no');
}
if (method_exists($definition, 'isSynchronized')) {
$description[] = sprintf('<comment>Synchronized</comment> %s', $definition->isSynchronized(false) ? 'yes' : 'no');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,10 +362,13 @@ private function getContainerDefinitionDocument(Definition $definition, $id = nu
}
}

$serviceXML->setAttribute('scope', $definition->getScope());
$serviceXML->setAttribute('scope', $definition->getScope(false));
$serviceXML->setAttribute('public', $definition->isPublic() ? 'true' : 'false');
$serviceXML->setAttribute('synthetic', $definition->isSynthetic() ? 'true' : 'false');
$serviceXML->setAttribute('lazy', $definition->isLazy() ? 'true' : 'false');
if (method_exists($definition, 'isShared')) {
$serviceXML->setAttribute('shared', $definition->isShared() ? 'true' : 'false');
}
if (method_exists($definition, 'isSynchronized')) {
$serviceXML->setAttribute('synchronized', $definition->isSynchronized(false) ? 'true' : 'false');
}
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@
</parameters>

<services>
<service id="test.client" class="%test.client.class%" scope="prototype">
<service id="test.client" class="%test.client.class%" shared="false">
<argument type="service" id="kernel" />
<argument>%test.client.parameters%</argument>
<argument type="service" id="test.client.history" />
<argument type="service" id="test.client.cookiejar" />
</service>

10000 <service id="test.client.history" class="%test.client.history.class%" scope="prototype" />
<service id="test.client.history" class="%test.client.history.class%" shared="false" />

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

<service id="test.session.listener" class="%test.session.listener.class%">
<argument type="service" id="service_container" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"public": true,
"synthetic": false,
"lazy": true,
"shared": true,
"synchronized": false,
"abstract": true,
"file": null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ definition_1
- Public: yes
- Synthetic: no
- Lazy: yes
- Shared: yes
- Synchronized: no
- Abstract: yes
- Factory Class: `Full\Qualified\FactoryClass`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<container>
<alias id="alias_1" service="service_1" public="true"/>
<alias id="alias_2" service="service_2" public="false"/>
<definition id="definition_1" class="Full\Qualified\Class1" scope="container" public="true" synthetic="false" lazy="true" synchronized="false" abstract="true" file="">
<definition id="definition_1" class="Full\Qualified\Class1" scope="container" public="true" synthetic="false" lazy="true" shared="true" synchronized="false" abstract="true" file="">
<factory class="Full\Qualified\FactoryClass" method="get"/>
</definition>
<service id="service_container" class="Symfony\Component\DependencyInjection\ContainerBuilder"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"public": true,
"synthetic": false,
"lazy": true,
"shared": true,
"synchronized": false,
"abstract": true,
"file": null,
Expand All @@ -21,6 +22,7 @@
"public": false,
"synthetic": true,
"lazy": false,
"shared": true,
"synchronized": false,
"abstract": false,
"file": "\/path\/to\/file",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ definition_1
- Public: yes
- Synthetic: no
- Lazy: yes
- Shared: yes
- Synchronized: no
- Abstract: yes
- Factory Class: `Full\Qualified\FactoryClass`
Expand All @@ -25,6 +26,7 @@ definition_2
- Public: no
- Synthetic: yes
- Lazy: no
- Shared: yes
- Synchronized: no
- Abstract: no
- File: `/path/to/file`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
<container>
<alias id="alias_1" service="service_1" public="true"/>
<alias id="alias_2" service="service_2" public="false"/>
<definition id="definition_1" class="Full\Qualified\Class1" scope="container" public="true" synthetic="false" lazy="true" synchronized="false" abstract="true" file="">
<definition id="definition_1" class="Full\Qualified\Class1" scope="container" public="true" synthetic="false" lazy="true" shared="true" synchronized="false" abstract="true" file="">
<factory class="Full\Qualified\FactoryClass" method="get"/>
</definition>
<definition id="definition_2" class="Full\Qualified\Class2" scope="container" public="false" synthetic="true" lazy="false" synchronized="false" abstract="false" file="/path/to/file">
<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">
<factory service="factory.service" method="get"/>
<tags>
<tag name="tag1">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"public": false,
"synthetic": true,
"lazy": false,
"shared": true,
"synchronized": false,
"abstract": false,
"file": "\/path\/to\/file",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ definition_2
- Public: no
- Synthetic: yes
- Lazy: no
- Shared: yes
- Synchronized: no
- Abstract: no
- File: `/path/to/file`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<container>
<definition id="definition_2" class="Full\Qualified\Class2" scope="container" public="false" synthetic="true" lazy="false" synchronized="false" abstract="false" file="/path/to/file">
<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">
<factory service="factory.service" method="get"/>
<tags>
<tag name="tag1">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"public": false,
"synthetic": true,
"lazy": false,
"shared": true,
"synchronized": false,
"abstract": false,
"file": "\/path\/to\/file",
Expand All @@ -20,6 +21,7 @@
"public": false,
"synthetic": true,
"lazy": false,
"shared": true,
"synchronized": false,
"abstract": false,
"file": "\/path\/to\/file",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ definition_2
- Public: no
- Synthetic: yes
- Lazy: no
- Shared: yes
- Synchronized: no
- Abstract: no
- File: `/path/to/file`
Expand All @@ -30,6 +31,7 @@ definition_2
- Public: no
- Synthetic: yes
- Lazy: no
- Shared: yes
- Synchronized: no
- Abstract: no
- File: `/path/to/file`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<container>
<tag name="tag1">
<definition id="definition_2" class="Full\Qualified\Class2" scope="container" public="false" synthetic="true" lazy="false" synchronized="false" abstract="false" file="/path/to/file">
<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">
<factory service="factory.service" method="get"/>
</definition>
</tag>
<tag name="tag2">
<definition id="definition_2" class="Full\Qualified\Class2" scope="container" public="false" synthetic="true" lazy="false" synchronized="false" abstract="false" file="/path/to/file">
<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">
<factory service="factory.service" method="get"/>
</definition>
</tag>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"public": true,
"synthetic": false,
"lazy": true,
"shared": true,
"synchronized": false,
"abstract": true,
"file": null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- Public: yes
- Synthetic: no
- Lazy: yes
- Shared: yes
- Synchronized: no
- Abstract: yes
- Factory Class: `Full\Qualified\FactoryClass`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<comment>Public</comment> yes
<comment>Synthetic</comment> no
<comment>Lazy</comment> yes
<comment>Shared</comment> yes
<comment>Synchronized</comment> no
<comment>Abstract</comment> yes
<comment>Factory Class</comment> Full\Qualified\FactoryClass
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<definition class="Full\Qualified\Class1" scope="container" public="true" synthetic="false" lazy="true" synchronized="false" abstract="true" file="">
<definition class="Full\Qualified\Class1" scope="container" public="true" synthetic="false" lazy="true" shared="true" synchronized="false" abstract="true" file="">
<factory class="Full\Qualified\FactoryClass" method="get"/>
</definition>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"public": false,
"synthetic": true,
"lazy": false,
"shared": true,
"synchronized": false,
"abstract": false,
"file": "\/path\/to\/file",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- Public: no
- Synthetic: yes
- Lazy: no
- Shared: yes
- Synchronized: no
- Abstract: no
- File: `/path/to/file`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<comment>Public</comment> no
<comment>Synthetic</comment> yes
<comment>Lazy</comment> no
<comment>Shared</comment> yes
<comment>Synchronized</comment> no
<comment>Abstract</comment> no
<comment>Required File</comment> /path/to/file
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<definition class="Full\Qualified\Class2" scope="container" public="false" synthetic="true" lazy="false" synchronized="false" abstract="false" file="/path/to/file">
<definition class="Full\Qualified\Class2" scope="container" public="false" synthetic="true" lazy="false" shared="true" synchronized="false" abstract="false" file="/path/to/file">
<factory service="factory.service" method="get"/>
<tags>
<tag name="tag1">
Expand Down
Loading
0