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

Commit196e5c3

Browse files
[FrameworkBundle] Allow fetching private services from test clients
1 parent9e82562 commit196e5c3

File tree

10 files changed

+228
-9
lines changed

10 files changed

+228
-9
lines changed

‎src/Symfony/Bundle/FrameworkBundle/Client.php‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@ class Client extends BaseClient
3030
private$hasPerformedRequest =false;
3131
private$profiler =false;
3232
private$reboot =true;
33+
private$container;
3334

3435
/**
3536
* {@inheritdoc}
3637
*/
37-
publicfunction__construct(KernelInterface$kernel,array$server =array(),History$history =null,CookieJar$cookieJar =null)
38+
publicfunction__construct(KernelInterface$kernel,array$server =array(),History$history =null,CookieJar$cookieJar =null,ContainerInterface$container =null)
3839
{
3940
parent::__construct($kernel,$server,$history,$cookieJar);
41+
$this->container =$container;
4042
}
4143

4244
/**
@@ -46,7 +48,7 @@ public function __construct(KernelInterface $kernel, array $server = array(), Hi
4648
*/
4749
publicfunctiongetContainer()
4850
{
49-
return$this->kernel->getContainer();
51+
return$this->container ??$this->kernel->getContainer();
5052
}
5153

5254
/**
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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\Bundle\FrameworkBundle\DependencyInjection\Compiler;
13+
14+
useSymfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15+
useSymfony\Component\DependencyInjection\ContainerBuilder;
16+
useSymfony\Component\DependencyInjection\Reference;
17+
18+
/**
19+
* @author Nicolas Grekas <p@tchwork.com>
20+
*/
21+
class TestServiceContainerRealRefPassimplements CompilerPassInterface
22+
{
23+
publicfunctionprocess(ContainerBuilder$container)
24+
{
25+
if (!$container->hasDefinition('test.service_container')) {
26+
return;
27+
}
28+
29+
$testContainer =$container->getDefinition('test.service_container');
30+
$privateContainer =$container->getDefinition((string)$testContainer->getArgument(2));
31+
$definitions =$container->getDefinitions();
32+
33+
foreach ($privateContainer->getArgument(0)as$id =>$argument) {
34+
if (isset($definitions[$target = (string)$argument->getValues()[0]])) {
35+
$argument->setValues(array(newReference($target)));
36+
}
37+
}
38+
}
39+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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\Bundle\FrameworkBundle\DependencyInjection\Compiler;
13+
14+
useSymfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
15+
useSymfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16+
useSymfony\Component\DependencyInjection\ContainerBuilder;
17+
useSymfony\Component\DependencyInjection\Reference;
18+
19+
/**
20+
* @author Nicolas Grekas <p@tchwork.com>
21+
*/
22+
class TestServiceContainerWeakRefPassimplements CompilerPassInterface
23+
{
24+
publicfunctionprocess(ContainerBuilder$container)
25+
{
26+
if (!$container->hasDefinition('test.service_container')) {
27+
return;
28+
}
29+
30+
$privateServices =array();
31+
$definitions =$container->getDefinitions();
32+
33+
foreach ($definitionsas$id =>$definition) {
34+
if (!$definition->isPublic() && !$definition->getErrors() && !$definition->isAbstract()) {
35+
$privateServices[$id] =newServiceClosureArgument(newReference($id, ContainerBuilder::IGNORE_ON_UNINITIALIZED_REFERENCE));
36+
}
37+
}
38+
39+
$aliases =$container->getAliases();
40+
41+
foreach ($aliasesas$id =>$alias) {
42+
if (!$alias->isPublic()) {
43+
while (isset($aliases[$target = (string)$alias])) {
44+
$alias =$aliases[$target];
45+
}
46+
if (isset($definitions[$target]) && !$definitions[$target]->getErrors() && !$definitions[$target]->isAbstract()) {
47+
$privateServices[$id] =newServiceClosureArgument(newReference($target, ContainerBuilder::IGNORE_ON_UNINITIALIZED_REFERENCE));
48+
}
49+
}
50+
}
51+
52+
if ($privateServices) {
53+
$definitions[(string)$definitions['test.service_container']->getArgument(2)]->replaceArgument(0,$privateServices);
54+
}
55+
}
56+
}

‎src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
useSymfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\LoggingTranslatorPass;
2424
useSymfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddExpressionLanguageProvidersPass;
2525
useSymfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ContainerBuilderDebugDumpPass;
26+
useSymfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TestServiceContainerWeakRefPass;
27+
useSymfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TestServiceContainerRealRefPass;
2628
useSymfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\UnusedTagsPass;
2729
useSymfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\WorkflowGuardListenerPass;
2830
useSymfony\Component\Console\Application;
@@ -114,6 +116,8 @@ public function build(ContainerBuilder $container)
114116
$this->addCompilerPassIfExists($container, FormPass::class);
115117
$container->addCompilerPass(newWorkflowGuardListenerPass());
116118
$container->addCompilerPass(newResettableServicePass());
119+
$container->addCompilerPass(newTestServiceContainerWeakRefPass(), PassConfig::TYPE_BEFORE_REMOVING, -32);
120+
$container->addCompilerPass(newTestServiceContainerRealRefPass(), PassConfig::TYPE_AFTER_REMOVING);
117121

118122
if ($container->getParameter('kernel.debug')) {
119123
$container->addCompilerPass(newAddDebugLogProcessorPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -32);

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<argument>%test.client.parameters%</argument>
1717
<argumenttype="service"id="test.client.history" />
1818
<argumenttype="service"id="test.client.cookiejar" />
19+
<argumenttype="service"id="test.service_container" />
1920
</service>
2021

2122
<serviceid="test.client.history"class="Symfony\Component\BrowserKit\History"shared="false" />
@@ -33,5 +34,15 @@
3334
</service>
3435
</argument>
3536
</service>
37+
38+
<serviceid="test.service_container"class="Symfony\Bundle\FrameworkBundle\Test\TestContainer"public="true">
39+
<argumenttype="service"id="parameter_bag"on-invalid="null" />
40+
<argumenttype="service"id="service_container" />
41+
<argumenttype="service">
42+
<serviceclass="Symfony\Component\DependencyInjection\ServiceLocator">
43+
<argumenttype="collection" />
44+
</service>
45+
</argument>
46+
</service>
3647
</services>
3748
</container>

‎src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespaceSymfony\Bundle\FrameworkBundle\Test;
1313

1414
usePHPUnit\Framework\TestCase;
15+
useSymfony\Component\DependencyInjection\ContainerInterface;
1516
useSymfony\Component\DependencyInjection\ResettableContainerInterface;
1617
useSymfony\Component\HttpKernel\KernelInterface;
1718

@@ -29,6 +30,11 @@ abstract class KernelTestCase extends TestCase
2930
*/
3031
protectedstatic$kernel;
3132

33+
/**
34+
* @var ContainerInterface
35+
*/
36+
protectedstatic$container;
37+
3238
/**
3339
* @return string The Kernel class name
3440
*
@@ -59,6 +65,7 @@ protected static function bootKernel(array $options = array())
5965

6066
static::$kernel =static::createKernel($options);
6167
static::$kernel->boot();
68+
static::$container =static::$kernel->getContainer()->get('test.service_container');
6269

6370
returnstatic::$kernel;
6471
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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\Bundle\FrameworkBundle\Test;
13+
14+
usePsr\Container\ContainerInterfaceasPsrContainerInterface;
15+
useSymfony\Component\DependencyInjection\Container;
16+
useSymfony\Component\DependencyInjection\ContainerInterfaceasSymfonyContainerInterface;
17+
useSymfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
18+
19+
/**
20+
* @author Nicolas Grekas <p@tchwork.com>
21+
*/
22+
class TestContainerextends Container
23+
{
24+
private$publicContainer;
25+
private$privateContainer;
26+
27+
publicfunction__construct(?ParameterBagInterface$parameterBag,SymfonyContainerInterface$publicContainer,PsrContainerInterface$privateContainer)
28+
{
29+
$this->parameterBag =$parameterBag ??$publicContainer->getParameterBag();
30+
$this->publicContainer =$publicContainer;
31+
$this->privateContainer =$privateContainer;
32+
}
33+
34+
/**
35+
* {@inheritdoc}
36+
*/
37+
publicfunctioncompile()
38+
{
39+
$this->publicContainer->compile();
40+
}
41+
42+
/**
43+
* {@inheritdoc}
44+
*/
45+
publicfunctionisCompiled()
46+
{
47+
return$this->publicContainer->isCompiled();
48+
}
49+
50+
/**
51+
* {@inheritdoc}
52+
*/
53+
publicfunctionset($id,$service)
54+
{
55+
$this->publicContainer->set($id,$service);
56+
}
57+
58+
/**
59+
* {@inheritdoc}
60+
*/
61+
publicfunctionhas($id)
62+
{
63+
return$this->publicContainer->has($id) ||$this->privateContainer->has($id);
64+
}
65+
66+
/**
67+
* {@inheritdoc}
68+
*/
69+
publicfunctionget($id,$invalidBehavior =/* self::EXCEPTION_ON_INVALID_REFERENCE */1)
70+
{
71+
return$this->privateContainer->has($id) ?$this->privateContainer->get($id) :$this->publicContainer->get($id,$invalidBehavior);
72+
}
73+
74+
/**
75+
* {@inheritdoc}
76+
*/
77+
publicfunctioninitialized($id)
78+
{
79+
return$this->publicContainer->initialized($id);
80+
}
81+
82+
/**
83+
* {@inheritdoc}
84+
*/
85+
publicfunctionreset()
86+
{
87+
$this->publicContainer->reset();
88+
}
89+
90+
/**
91+
* {@inheritdoc}
92+
*/
93+
publicfunctiongetServiceIds()
94+
{
95+
return$this->publicContainer->getServiceIds();
96+
}
97+
98+
/**
99+
* {@inheritdoc}
100+
*/
101+
publicfunctiongetRemovedIds()
102+
{
103+
return$this->publicContainer->getRemovedIds();
104+
}
105+
}

‎src/Symfony/Bundle/FrameworkBundle/Tests/Functional/PropertyInfoTest.php‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ class PropertyInfoTest extends WebTestCase
1818
publicfunctiontestPhpDocPriority()
1919
{
2020
static::bootKernel(array('test_case' =>'Serializer'));
21-
$container =static::$kernel->getContainer();
2221

23-
$this->assertEquals(array(newType(Type::BUILTIN_TYPE_ARRAY,false,null,true,newType(Type::BUILTIN_TYPE_INT),newType(Type::BUILTIN_TYPE_INT))),$container->get('test.property_info')->getTypes('Symfony\Bundle\FrameworkBundle\Tests\Functional\Dummy','codes'));
22+
$this->assertEquals(array(newType(Type::BUILTIN_TYPE_ARRAY,false,null,true,newType(Type::BUILTIN_TYPE_INT),newType(Type::BUILTIN_TYPE_INT))),static::$container->get('property_info')->getTypes('Symfony\Bundle\FrameworkBundle\Tests\Functional\Dummy','codes'));
2423
}
2524
}
2625

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
imports:
22
-{ resource: ../config/default.yml }
33

4-
services:
5-
_defaults:{ public: true }
6-
test.property_info:'@property_info'
7-
84
framework:
95
serializer:{ enabled: true }
106
property_info:{ enabled: true }

‎src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/config/framework.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ framework:
44
validation:{ enabled: true, enable_annotations: true }
55
csrf_protection:true
66
form:true
7-
test:~
7+
test:true
88
default_locale:en
99
session:
1010
storage_id:session.storage.mock_file

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp