Skip to content

Commit 04d5d92

Browse files
committed
Add "shared" flag and deprecate scopes concept
1 parent f2ff9b9 commit 04d5d92

Some content is hidden

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

43 files changed

+373
-56
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ CHANGELOG
55
-----
66

77
* allowed specifying a directory to recursively load all configuration files it contains
8+
* deprecated the concept of scopes
9+
* added `Definition::setShared()` and `Definition::isShared()`
810

911
2.7.0
1012
-----

Compiler/CheckDefinitionValidityPass.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* - non synthetic, non abstract services always have a class set
2626
* - synthetic services are always public
2727
* - synthetic services are always of non-prototype scope
28+
* - shared services are always of non-prototype scope
2829
*
2930
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
3031
*/
@@ -46,10 +47,15 @@ public function process(ContainerBuilder $container)
4647
}
4748

4849
// synthetic service has non-prototype scope
49-
if ($definition->isSynthetic() && ContainerInterface::SCOPE_PROTOTYPE === $definition->getScope()) {
50+
if ($definition->isSynthetic() && ContainerInterface::SCOPE_PROTOTYPE === $definition->getScope(false)) {
5051
throw new RuntimeException(sprintf('A synthetic service ("%s") cannot be of scope "prototype".', $id));
5152
}
5253

54+
// shared service has non-prototype scope
55+
if ($definition->isShared() && ContainerInterface::SCOPE_PROTOTYPE === $definition->getScope(false)) {
56+
throw new RuntimeException(sprintf('A shared service ("%s") cannot be of scope "prototype".', $id));
57+
}
58+
5359
if ($definition->getFactory() && ($definition->getFactoryClass(false) || $definition->getFactoryService(false) || $definition->getFactoryMethod(false))) {
5460
throw new RuntimeException(sprintf('A service ("%s") can use either the old or the new factory syntax, not both.', $id));
5561
}

Compiler/CheckReferenceValidityPass.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ public function process(ContainerBuilder $container)
4646
{
4747
$this->container = $container;
4848

49-
$children = $this->container->getScopeChildren();
49+
$children = $this->container->getScopeChildren(false);
5050
$ancestors = array();
5151

52-
$scopes = $this->container->getScopes();
52+
$scopes = $this->container->getScopes(false);
5353
foreach ($scopes as $name => $parent) {
5454
$ancestors[$name] = array($parent);
5555

@@ -65,7 +65,7 @@ public function process(ContainerBuilder $container)
6565

6666
$this->currentId = $id;
6767
$this->currentDefinition = $definition;
68-
$this->currentScope = $scope = $definition->getScope();
68+
$this->currentScope = $scope = $definition->getScope(false);
6969

7070
if (ContainerInterface::SCOPE_CONTAINER === $scope) {
7171
$this->currentScopeChildren = array_keys($scopes);
@@ -125,15 +125,15 @@ private function validateScope(Reference $reference, Definition $definition = nu
125125
return;
126126
}
127127

128-
if (!$reference->isStrict()) {
128+
if (!$reference->isStrict(false)) {
129129
return;
130130
}
131131

132132
if (null === $definition) {
133133
return;
134134
}
135135

136-
if ($this->currentScope === $scope = $definition->getScope()) {
136+
if ($this->currentScope === $scope = $definition->getScope(false)) {
137137
return;
138138
}
139139

Compiler/InlineServiceDefinitionsPass.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ private function inlineArguments(ContainerBuilder $container, array $arguments)
9292
if ($this->isInlineableDefinition($container, $id, $definition = $container->getDefinition($id))) {
9393
$this->compiler->addLogMessage($this->formatter->formatInlineService($this, $id, $this->currentId));
9494

95-
if (ContainerInterface::SCOPE_PROTOTYPE !== $definition->getScope()) {
95+
if ($definition->isShared() && ContainerInterface::SCOPE_PROTOTYPE !== $definition->getScope(false)) {
9696
$arguments[$k] = $definition;
9797
} else {
9898
$arguments[$k] = clone $definition;
@@ -119,7 +119,7 @@ private function inlineArguments(ContainerBuilder $container, array $arguments)
119119
*/
120120
private function isInlineableDefinition(ContainerBuilder $container, $id, Definition $definition)
121121
{
122-
if (ContainerInterface::SCOPE_PROTOTYPE === $definition->getScope()) {
122+
if (!$definition->isShared() || ContainerInterface::SCOPE_PROTOTYPE === $definition->getScope(false)) {
123123
return true;
124124
}
125125

@@ -152,6 +152,6 @@ private function isInlineableDefinition(ContainerBuilder $container, $id, Defini
152152
return false;
153153
}
154154

155-
return $container->getDefinition(reset($ids))->getScope() === $definition->getScope();
155+
return $container->getDefinition(reset($ids))->getScope(false) === $definition->getScope(false);
156156
}
157157
}

Compiler/ResolveDefinitionTemplatesPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ private function resolveDefinition($id, DefinitionDecorator $definition)
153153

154154
// these attributes are always taken from the child
155155
$def->setAbstract($definition->isAbstract());
156-
$def->setScope($definition->getScope());
156+
$def->setScope($definition->getScope(false), false);
157157
$def->setTags($definition->getTags());
158158

159159
// set new definition on container

Compiler/ResolveReferencesToAliasesPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ private function processArguments(array $arguments)
6868
$defId = $this->getDefinitionId($id = (string) $argument);
6969

7070
if ($defId !== $id) {
71-
$arguments[$k] = new Reference($defId, $argument->getInvalidBehavior(), $argument->isStrict());
71+
$arguments[$k] = new Reference($defId, $argument->getInvalidBehavior(), $argument->isStrict(false));
7272
}
7373
}
7474
}

Container.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ public function setParameter($name, $value)
180180
* Setting a service to null resets the service: has() returns false and get()
181181
* behaves in the same way as if the service was never created.
182182
*
183+
* Note: The $scope parameter is deprecated since version 2.8 and will be removed in 3.0.
184+
*
183185
* @param string $id The service identifier
184186
* @param object $service The service instance
185187
* @param string $scope The scope of the service
@@ -191,6 +193,10 @@ public function setParameter($name, $value)
191193
*/
192194
public function set($id, $service, $scope = self::SCOPE_CONTAINER)
193195
{
196+
if (!in_array($scope, array('container', 'request')) || ('request' === $scope && 'request' !== $id)) {
197+
@trigger_error('The concept of container scopes is deprecated since version 2.8 and will be removed in 3.0. Omit the third parameter.', E_USER_DEPRECATED);
198+
}
199+
194200
if (self::SCOPE_PROTOTYPE === $scope) {
195201
throw new InvalidArgumentException(sprintf('You cannot set service "%s" of scope "prototype".', $id));
196202
}
@@ -397,9 +403,15 @@ public function getServiceIds()
397403
* @throws InvalidArgumentException When the scope does not exist
398404
*
399405
* @api
406+
*
407+
* @deprecated since version 2.8, to be removed in 3.0.
400408
*/
401409
public function enterScope($name)
402410
{
411+
if ('request' !== $name) {
412+
@trigger_error('The '.__METHOD__.' method is deprecated since version 2.8 and will be removed in 3.0.', E_USER_DEPRECATED);
413+
}
414+
403415
if (!isset($this->scopes[$name])) {
404416
throw new InvalidArgumentException(sprintf('The scope "%s" does not exist.', $name));
405417
}
@@ -445,9 +457,15 @@ public function enterScope($name)
445457
* @throws InvalidArgumentException if the scope is not active
446458
*
447459
* @api
460+
*
461+
* @deprecated since version 2.8, to be removed in 3.0.
448462
*/
449463
public function leaveScope($name)
450464
{
465+
if ('request' !== $name) {
466+
@trigger_error('The '.__METHOD__.' method is deprecated since version 2.8 and will be removed in 3.0.', E_USER_DEPRECATED);
467+
}
468+
451469
if (!isset($this->scopedServices[$name])) {
452470
throw new InvalidArgumentException(sprintf('The scope "%s" is not active.', $name));
453471
}
@@ -492,12 +510,17 @@ public function leaveScope($name)
492510
* @throws InvalidArgumentException
493511
*
494512
* @api
513+
*
514+
* @deprecated since version 2.8, to be removed in 3.0.
495515
*/
496516
public function addScope(ScopeInterface $scope)
497517
{
498518
$name = $scope->getName();
499519
$parentScope = $scope->getParentName();
500520

521+
if ('request' !== $name) {
522+
@trigger_error('The '.__METHOD__.' method is deprecated since version 2.8 and will be removed in 3.0.', E_USER_DEPRECATED);
523+
}
501524
if (self::SCOPE_CONTAINER === $name || self::SCOPE_PROTOTYPE === $name) {
502525
throw new InvalidArgumentException(sprintf('The scope "%s" is reserved.', $name));
503526
}
@@ -526,9 +549,15 @@ public function addScope(ScopeInterface $scope)
526549
* @return bool
527550
*
528551
* @api
552+
*
553+
* @deprecated since version 2.8, to be removed in 3.0.
529554
*/
530555
public function hasScope($name)
531556
{
557+
if ('request' !== $name) {
558+
@trigger_error('The '.__METHOD__.' method is deprecated since version 2.8 and will be removed in 3.0.', E_USER_DEPRECATED);
559+
}
560+
532561
return isset($this->scopes[$name]);
533562
}
534563

@@ -542,9 +571,13 @@ public function hasScope($name)
542571
* @return bool
543572
*
544573
* @api
574+
*
575+
* @deprecated since version 2.8, to be removed in 3.0.
545576
*/
546577
public function isScopeActive($name)
547578
{
579+
@trigger_error('The '.__METHOD__.' method is deprecated since version 2.8 and will be removed in 3.0.', E_USER_DEPRECATED);
580+
548581
return isset($this->scopedServices[$name]);
549582
}
550583

ContainerBuilder.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,9 +358,15 @@ public function getCompiler()
358358
* @return array An array of scopes
359359
*
360360
* @api
361+
*
362+
* @deprecated since version 2.8, to be removed in 3.0.
361363
*/
362-
public function getScopes()
364+
public function getScopes($triggerDeprecationError = true)
363365
{
366+
if ($triggerDeprecationError) {
367+
@trigger_error('The '.__METHOD__.' method is deprecated since version 2.8 and will be removed in 3.0.', E_USER_DEPRECATED);
368+
}
369+
364370
return $this->scopes;
365371
}
366372

@@ -370,15 +376,23 @@ public function getScopes()
370376
* @return array An array of scope children.
371377
*
372378
* @api
379+
*
380+
* @deprecated since version 2.8, to be removed in 3.0.
373381
*/
374-
public function getScopeChildren()
382+
public function getScopeChildren($triggerDeprecationError = true)
375383
{
384+
if ($triggerDeprecationError) {
385+
@trigger_error('The '.__METHOD__.' method is deprecated since version 2.8 and will be removed in 3.0.', E_USER_DEPRECATED);
386+
}
387+
376388
return $this->scopeChildren;
377389
}
378390

379391
/**
380392
* Sets a service.
381393
*
394+
* Note: The $scope parameter is deprecated since version 2.8 and will be removed in 3.0.
395+
*
382396
* @param string $id The service identifier
383397
* @param object $service The service instance
384398
* @param string $scope The scope
@@ -1176,7 +1190,7 @@ private function callMethod($service, $call)
11761190
*/
11771191
private function shareService(Definition $definition, $service, $id)
11781192
{
1179-
if (self::SCOPE_PROTOTYPE !== $scope = $definition->getScope()) {
1193+
if ($definition->isShared() && self::SCOPE_PROTOTYPE !== $scope = $definition->getScope(false)) {
11801194
if (self::SCOPE_CONTAINER !== $scope && !isset($this->scopedServices[$scope])) {
11811195
throw new InactiveScopeException($id, $scope);
11821196
}

ContainerInterface.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ interface ContainerInterface
3434
/**
3535
* Sets a service.
3636
*
37+
* Note: The $scope parameter is deprecated since version 2.8 and will be removed in 3.0.
38+
*
3739
* @param string $id The service identifier
3840
* @param object $service The service instance
3941
* @param string $scope The scope of the service
@@ -110,6 +112,8 @@ public function setParameter($name, $value);
110112
* @param string $name
111113
*
112114
* @api
115+
*
116+
* @deprecated since version 2.8, to be removed in 3.0.
113117
*/
114118
public function enterScope($name);
115119

@@ -119,6 +123,8 @@ public function enterScope($name);
119123
* @param string $name
120124
*
121125
* @api
126+
*
127+
* @deprecated since version 2.8, to be removed in 3.0.
122128
*/
123129
public function leaveScope($name);
124130

@@ -128,6 +134,8 @@ public function leaveScope($name);
128134
* @param ScopeInterface $scope
129135
*
130136
* @api
137+
*
138+
* @deprecated since version 2.8, to be removed in 3.0.
131139
*/
132140
public function addScope(ScopeInterface $scope);
133141

@@ -139,6 +147,8 @@ public function addScope(ScopeInterface $scope);
139147
* @return bool
140148
*
141149
* @api
150+
*
151+
* @deprecated since version 2.8, to be removed in 3.0.
142152
*/
143153
public function hasScope($name);
144154

@@ -152,6 +162,8 @@ public function hasScope($name);
152162
* @return bool
153163
*
154164
* @api
165+
*
166+
* @deprecated since version 2.8, to be removed in 3.0.
155167
*/
156168
public function isScopeActive($name);
157169
}

0 commit comments

Comments
 (0)