Skip to content

Commit

Permalink
Add "shared" flag and deprecate scopes concept
Browse files Browse the repository at this point in the history
  • Loading branch information
dosten committed Jun 24, 2015
1 parent dd7583d commit 6c4a676
Show file tree
Hide file tree
Showing 84 changed files with 515 additions and 90 deletions.
71 changes: 71 additions & 0 deletions UPGRADE-2.8.md
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)
;
```

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

```xml
<services>
<service id="foo" class="stdClass" shared="false" />
</services>
```
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)) {
$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
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
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
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
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
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
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>

<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
Expand Up @@ -6,6 +6,7 @@
"public": true,
"synthetic": false,
"lazy": true,
"shared": true,
"synchronized": false,
"abstract": true,
"file": null,
Expand Down
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
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
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
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
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
Expand Up @@ -6,6 +6,7 @@
"public": false,
"synthetic": true,
"lazy": false,
"shared": true,
"synchronized": false,
"abstract": false,
"file": "\/path\/to\/file",
Expand Down
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
@@ -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
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
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
@@ -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
Expand Up @@ -4,6 +4,7 @@
"public": true,
"synthetic": false,
"lazy": true,
"shared": true,
"synchronized": false,
"abstract": true,
"file": null,
Expand Down
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
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
@@ -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>
Expand Up @@ -4,6 +4,7 @@
"public": false,
"synthetic": true,
"lazy": false,
"shared": true,
"synchronized": false,
"abstract": false,
"file": "\/path\/to\/file",
Expand Down
Expand Up @@ -3,6 +3,7 @@
- Public: no
- Synthetic: yes
- Lazy: no
- Shared: yes
- Synchronized: no
- Abstract: no
- File: `/path/to/file`
Expand Down
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
@@ -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

0 comments on commit 6c4a676

Please sign in to comment.