8000 [FrameworkBundle] Allow fetching private services from test clients · symfony/symfony@b944061 · GitHub
[go: up one dir, main page]

Skip to content

Commit b944061

Browse files
[FrameworkBundle] Allow fetching private services from test clients
1 parent 9e82562 commit b944061

File tree

5 files changed

+165
-2
lines changed

5 files changed

+165
-2
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-
public function __construct(KernelInterface $kernel, array $server = array(), History $history = null, CookieJar $cookieJar = null)
38+
public function __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
public function getContainer()
4850
{
49-
return $this->kernel->getContainer();
51+
return $this->container ?? $this->kernel->getContainer();
5052
}
5153

5254
/**
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;
13+
14+
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
15+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16+
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\DependencyInjection\Reference;
18+
19+
class TestServiceContainerPass implements CompilerPassInterface
20+
{
21+
public function process(ContainerBuilder $container)
22+
{
23+
if ($container->hasDefinition('test.service_container')) {
24+
return;
25+
}
26+
27+
$privateServices = array();
28+
29+
foreach ($container->getDefinitions() as $id => $definition) {
30+
if (!$definition->isPublic()) {
31+
$privateServices[$id] = new ServiceClosureArgument(new Reference($id));
32+
}
33+
}
34+
35+
foreach ($container->getAliases() as $id => $alias) {
36+
if (!$alias->isPublic()) {
37+
$privateServices[$id] = new ServiceClosureArgument(new Reference((string) $alias)));
38+
}
39+
}
40+
41+
$container->getDefinition('test.service_container')->getArgument(2)->replaceArgument(0, $privateServices);
42+
}
43+
}

src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Symfony\Bundle\FrameworkBundle\DependencyInje 1E0A ction\Compiler\LoggingTranslatorPass;
2424
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddExpressionLanguageProvidersPass;
2525
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ContainerBuilderDebugDumpPass;
26+
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TestServiceContainerPass;
2627
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\UnusedTagsPass;
2728
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\WorkflowGuardListenerPass;
2829
use Symfony\Component\Console\Application;
@@ -114,6 +115,7 @@ public function build(ContainerBuilder $container)
114115
$this->addCompilerPassIfExists($container, FormPass::class);
115116
$container->addCompilerPass(new WorkflowGuardListenerPass());
116117
$container->addCompilerPass(new ResettableServicePass());
118+
$container->addCompilerPass(new TestServiceContainerPass(), PassConfig::TYPE_BEFORE_REMOVING, -255);
117119

118120
if ($container->getParameter('kernel.debug')) {
119121
$container->addCompilerPass(new AddDebugLogProcessorPass(), 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
<argument type="service" id="test.client.history" />
1818
<argument type="service" id="test.client.cookiejar" />
19+
<argument type="service" id="test.service_container" />
1920
</service>
2021

2122
<service id="test.client.history" class="Symfony\Component\BrowserKit\History" shared="false" />
@@ -33,5 +34,15 @@
3334
</service>
3435
</argument>
3536
</service>
37+
38+
<service id="test.service_container" class="Symfony\Bundle\FrameworkBundle\Test\TestContainer">
39+
<argument type="service" id="parameter_bag" />
40+
<argument type="service" id="service_container" />
41+
<argument type="service">
42+
<service class="Symfony\Component\DependencyInjection\ServiceLocator">
43+
<argument type="collection" />
44+
</service>
45+
</argument>
46+
</service>
3647
</services>
3748
</container>
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+
namespace Symfony\Bundle\FrameworkBundle\Test;
13+
14+
use Psr\Container\ContainerInterface as PsrContainerInterface;
15+
use Symfony\Component\DependencyInjection\Container;
16+
use Symfony\Component\DependencyInjection\ContainerInterface as SymfonyContainerInterface;
17+
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
18+
19+
/**
20+
* @author Nicolas Grekas <p@tchwork.com>
21+
*/
22+
class TestContainer extends Container
23+
{
24+
private $publicContainer;
25+
private $privateContainer;
26+
27+
public function __construct(ParameterBagInterface $parameterBag, SymfonyContainerInterface $publicContainer, PsrContainerInterface $privateContainer)
28+
{
29+
$this->parameterBag = $parameterBag;
30+
$this->publicContainer = < 10000 span class=pl-c1>$publicContainer;
31+
$this->privateContainer = $privateContainer;
32+
}
33+
34+
/**
35+
* {@inheritdoc}
36+
*/
37+
public function compile()
38+
{
39+
return $this->publicContainer->isCompiled();
40+
}
41+
42+
/**
43+
* {@inheritdoc}
44+
*/
45+
public function isCompiled()
46+
{
47+
return $this->publicContainer->isCompiled();
48+
}
49+
50+
/**
51+
* {@inheritdoc}
52+
*/
53+
public function set($id, $service)
54+
{
55+
$this->publicContainer->set($id, $service);
56+
}
57+
58+
/**
59+
* {@inheritdoc}
60+
*/
61+
public function has($id)
62+
{
63+
return $this->publicContainer->has($id) || $this->privateContainer->has($id);
64+
}
65+
66+
/**
67+
* {@inheritdoc}
68+
*/
69+
public function get($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+
public function initialized($id)
78+
{
79+
return $this->publicContainer->initialized($id);
80+
}
81+
82+
/**
83+
* {@inheritdoc}
84+
*/
85+
public function reset()
86+
{
87+
return $this->publicContainer->reset();
88+
}
89+
90+
/**
91+
* {@inheritdoc}
92+
*/
93+
public function getServiceIds()
94+
{
95+
return $this->publicContainer->getServiceIds();
96+
}
97+
98+
/**
99+
* {@inheritdoc}
100+
*/
101+
public function getRemovedIds()
102+
{
103+
return $this->publicContainer->getRemovedIds();
104+
}
105+
}

0 commit comments

Comments
 (0)
0