Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1c0c22d

Browse files
committed
[DI] Replace container injection by explicit service locators
[SecurityBundle] Avoid container injection in FirewallMap
1 parent91904af commit1c0c22d

File tree

37 files changed

+476
-9
lines changed

37 files changed

+476
-9
lines changed

‎src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
useSymfony\Component\Config\Definition\Exception\InvalidConfigurationException;
1717
useSymfony\Component\DependencyInjection\Alias;
1818
useSymfony\Component\DependencyInjection\Argument\IteratorArgument;
19+
useSymfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
1920
useSymfony\Component\DependencyInjection\ChildDefinition;
2021
useSymfony\Component\HttpKernel\DependencyInjection\Extension;
2122
useSymfony\Component\DependencyInjection\Loader\XmlFileLoader;
@@ -239,7 +240,7 @@ private function createFirewalls($config, ContainerBuilder $container)
239240

240241
// load firewall map
241242
$mapDef =$container->getDefinition('security.firewall.map');
242-
$map =$authenticationProviders =array();
243+
$map =$authenticationProviders =$contextRefs =array();
243244
foreach ($firewallsas$name =>$firewall) {
244245
$configId ='security.firewall.map.config.'.$name;
245246

@@ -253,8 +254,10 @@ private function createFirewalls($config, ContainerBuilder $container)
253254
->replaceArgument(2,newReference($configId))
254255
;
255256

257+
$contextRefs[$contextId] =newReference($contextId);
256258
$map[$contextId] =$matcher;
257259
}
260+
$mapDef->replaceArgument(0,newServiceLocatorArgument($contextRefs));
258261
$mapDef->replaceArgument(1,newIteratorArgument($map));
259262

260263
// add authentication providers to authentication manager

‎src/Symfony/Bundle/SecurityBundle/Resources/config/security.xml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@
105105
</service>
106106

107107
<serviceid="security.firewall.map"class="Symfony\Bundle\SecurityBundle\Security\FirewallMap"public="false">
108-
<argumenttype="service"id="service_container" />
109-
<argument />
108+
<argument/><!-- Firewall context locator-->
109+
<argument /><!-- Request matchers-->
110110
</service>
111111

112112
<serviceid="security.firewall.context"class="Symfony\Bundle\SecurityBundle\Security\FirewallContext"abstract="true">

‎src/Symfony/Bundle/SecurityBundle/Security/FirewallMap.php‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
namespaceSymfony\Bundle\SecurityBundle\Security;
1313

14+
usePsr\Container\ContainerInterface;
1415
useSymfony\Component\Security\Http\FirewallMapInterface;
1516
useSymfony\Component\HttpFoundation\Request;
16-
useSymfony\Component\DependencyInjection\ContainerInterface;
1717

1818
/**
1919
* This is a lazy-loading firewall map implementation.
@@ -116,9 +116,6 @@ public function __construct(ContainerInterface $container, $map)
116116
$this->contexts =new \SplObjectStorage();
117117
}
118118

119-
/**
120-
* {@inheritdoc}
121-
*/
122119
publicfunctiongetListeners(Request$request)
123120
{
124121
$context =$this->getFirewallContext($request);
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\DependencyInjection\Argument;
13+
14+
useSymfony\Component\DependencyInjection\Reference;
15+
useSymfony\Component\DependencyInjection\Exception\InvalidArgumentException;
16+
17+
/**
18+
* Represents a service locator able to lazy load a given range of services.
19+
*
20+
* @author Robin Chalas <robin.chalas@gmail.com>
21+
*
22+
* @experimental in version 3.3
23+
*/
24+
class ServiceLocatorArgumentimplements ArgumentInterface
25+
{
26+
private$values;
27+
28+
/**
29+
* @param Reference[] $values An array of references indexed by identifier
30+
*/
31+
publicfunction__construct(array$values)
32+
{
33+
$this->setValues($values);
34+
}
35+
36+
publicfunctiongetValues()
37+
{
38+
return$this->values;
39+
}
40+
41+
publicfunctionsetValues(array$values)
42+
{
43+
foreach ($valuesas$v) {
44+
if (!$vinstanceof Reference) {
45+
thrownewInvalidArgumentException('Values of a ServiceLocatorArgument must be Reference objects.');
46+
}
47+
}
48+
49+
$this->values =$values;
50+
}
51+
}

‎src/Symfony/Component/DependencyInjection/CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
3.3.0
55
-----
66

7+
*[EXPERIMENTAL] added "service-locator" argument for lazy loading a set of identified values and services
78
*[EXPERIMENTAL] added prototype services for PSR4-based discovery and registration
89
* added`ContainerBuilder::getReflectionClass()` for retrieving and tracking reflection class info
910
* deprecated`ContainerBuilder::getClassResource()`, use`ContainerBuilder::getReflectionClass()` or`ContainerBuilder::addObjectResource()` instead

‎src/Symfony/Component/DependencyInjection/ContainerBuilder.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
useSymfony\Component\DependencyInjection\Argument\ClosureProxyArgument;
1515
useSymfony\Component\DependencyInjection\Argument\IteratorArgument;
1616
useSymfony\Component\DependencyInjection\Argument\RewindableGenerator;
17+
useSymfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
1718
useSymfony\Component\DependencyInjection\Compiler\Compiler;
1819
useSymfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1920
useSymfony\Component\DependencyInjection\Compiler\PassConfig;
@@ -1122,6 +1123,15 @@ public function resolveServices($value)
11221123
foreach ($valueas$k =>$v) {
11231124
$value[$k] =$this->resolveServices($v);
11241125
}
1126+
}elseif ($valueinstanceof ServiceLocatorArgument) {
1127+
$parameterBag =$this->getParameterBag();
1128+
$services =array();
1129+
foreach ($value->getValues()as$k =>$v) {
1130+
$services[$k] =function ()use ($v,$parameterBag) {
1131+
return$this->resolveServices($parameterBag->unescapeValue($parameterBag->resolveValue($v)));
1132+
};
1133+
}
1134+
$value =newServiceLocator($services);
11251135
}elseif ($valueinstanceof IteratorArgument) {
11261136
$parameterBag =$this->getParameterBag();
11271137
$value =newRewindableGenerator(function ()use ($value,$parameterBag) {

‎src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
useSymfony\Component\DependencyInjection\Argument\ClosureProxyArgument;
1515
useSymfony\Component\DependencyInjection\Argument\IteratorArgument;
16+
useSymfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
1617
useSymfony\Component\DependencyInjection\Variable;
1718
useSymfony\Component\DependencyInjection\Definition;
1819
useSymfony\Component\DependencyInjection\ContainerBuilder;
@@ -897,6 +898,7 @@ private function startClass($class, $baseClass, $namespace)
897898
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
898899
use Symfony\Component\DependencyInjection\Exception\LogicException;
899900
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
901+
use Symfony\Component\DependencyInjection\ServiceLocator;
900902
$bagClass
901903
902904
/*{$this->docStar}
@@ -1536,6 +1538,14 @@ private function dumpValue($value, $interpolate = true)
15361538
}
15371539

15381540
returnsprintf('array(%s)',implode(',',$code));
1541+
}elseif ($valueinstanceof ServiceLocatorArgument) {
1542+
$code ="\n";
1543+
foreach ($value->getValues()as$k =>$v) {
1544+
$code .=sprintf(" %s => function () { return %s; },\n",$this->dumpValue($k,$interpolate),$this->dumpValue($v,$interpolate));
1545+
}
1546+
$code .='';
1547+
1548+
returnsprintf('new ServiceLocator(array(%s))',$code);
15391549
}elseif ($valueinstanceof IteratorArgument) {
15401550
$countCode =array();
15411551
$countCode[] ='function () {';

‎src/Symfony/Component/DependencyInjection/Dumper/XmlDumper.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
useSymfony\Component\DependencyInjection\Argument\ClosureProxyArgument;
1515
useSymfony\Component\DependencyInjection\Argument\IteratorArgument;
16+
useSymfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
1617
useSymfony\Component\DependencyInjection\ContainerInterface;
1718
useSymfony\Component\DependencyInjection\Parameter;
1819
useSymfony\Component\DependencyInjection\Reference;
@@ -291,6 +292,9 @@ private function convertParameters(array $parameters, $type, \DOMElement $parent
291292
if (is_array($value)) {
292293
$element->setAttribute('type','collection');
293294
$this->convertParameters($value,$type,$element,'key');
295+
}elseif ($valueinstanceof ServiceLocatorArgument) {
296+
$element->setAttribute('type','service-locator');
297+
$this->convertParameters($value->getValues(),$type,$element);
294298
}elseif ($valueinstanceof IteratorArgument) {
295299
$element->setAttribute('type','iterator');
296300
$this->convertParameters($value->getValues(),$type,$element,'key');

‎src/Symfony/Component/DependencyInjection/Dumper/YamlDumper.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
useSymfony\Component\DependencyInjection\Argument\ArgumentInterface;
1818
useSymfony\Component\DependencyInjection\Argument\ClosureProxyArgument;
1919
useSymfony\Component\DependencyInjection\Argument\IteratorArgument;
20+
useSymfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
2021
useSymfony\Component\DependencyInjection\ContainerInterface;
2122
useSymfony\Component\DependencyInjection\Definition;
2223
useSymfony\Component\DependencyInjection\Parameter;
@@ -258,6 +259,8 @@ private function dumpValue($value)
258259
$tag ='iterator';
259260
}elseif ($valueinstanceof ClosureProxyArgument) {
260261
$tag ='closure_proxy';
262+
}elseif ($valueinstanceof ServiceLocatorArgument) {
263+
$tag ='service_locator';
261264
}else {
262265
thrownewRuntimeException(sprintf('Unspecified Yaml tag for type "%s".',get_class($value)));
263266
}

‎src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111

1212
namespaceSymfony\Component\DependencyInjection\Loader;
1313

14-
useSymfony\Component\Config\Resource\FileResource;
1514
useSymfony\Component\Config\Util\XmlUtils;
1615
useSymfony\Component\DependencyInjection\ContainerInterface;
1716
useSymfony\Component\DependencyInjection\Alias;
1817
useSymfony\Component\DependencyInjection\Argument\ClosureProxyArgument;
1918
useSymfony\Component\DependencyInjection\Argument\IteratorArgument;
19+
useSymfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
2020
useSymfony\Component\DependencyInjection\Definition;
2121
useSymfony\Component\DependencyInjection\ChildDefinition;
2222
useSymfony\Component\DependencyInjection\Reference;
@@ -498,6 +498,15 @@ private function getArgumentsAsPhp(\DOMElement $node, $name, $lowercase = true,
498498
case'iterator':
499499
$arguments[$key] =newIteratorArgument($this->getArgumentsAsPhp($arg,$name,false));
500500
break;
501+
case'service-locator':
502+
$values =$this->getArgumentsAsPhp($arg,$name,false);
503+
foreach ($valuesas$v) {
504+
if (!$vinstanceof Reference) {
505+
thrownewInvalidArgumentException('"service-locator" argument values must be services.');
506+
}
507+
}
508+
$arguments[$key] =newServiceLocatorArgument($args);
509+
break;
501510
case'string':
502511
$arguments[$key] =$arg->nodeValue;
503512
break;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp