-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathSymfony.php
138 lines (119 loc) · 4.36 KB
/
Symfony.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
declare(strict_types=1);
namespace Codeception\Lib\Connector;
use InvalidArgumentException;
use ReflectionClass;
use ReflectionMethod;
use ReflectionProperty;
use Symfony\Bundle\FrameworkBundle\Test\TestContainer;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelBrowser;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\HttpKernel\Profiler\Profiler;
use function array_keys;
use function codecept_debug;
class Symfony extends HttpKernelBrowser
{
private bool $hasPerformedRequest = false;
private ?ContainerInterface $container;
public function __construct(
Kernel $kernel,
public array $persistentServices = [],
private readonly bool $rebootable = true
) {
parent::__construct($kernel);
$this->followRedirects();
$this->container = $this->getContainer();
$this->rebootKernel();
}
/** @param Request $request */
protected function doRequest(object $request): Response
{
if ($this->hasPerformedRequest && $this->rebootable) {
$this->rebootKernel();
} else {
$this->hasPerformedRequest = true;
}
return parent::doRequest($request);
}
/**
* Reboots the kernel.
*
* Services from the list of persistent services
* are updated from service container before kernel shutdown
* and injected into newly initialized container after kernel boot.
*/
public function rebootKernel(): void
{
if ($this->container) {
foreach (array_keys($this->persistentServices) as $serviceName) {
if ($service = $this->getService($serviceName)) {
$this->persistentServices[$serviceName] = $service;
}
}
}
$this->persistDoctrineConnections();
$this->ensureKernelShutdown();
$this->kernel->boot();
$this->container = $this->getContainer();
foreach ($this->persistentServices as $serviceName => $service) {
try {
$this->container->set($serviceName, $service);
} catch (InvalidArgumentException $e) {
codecept_debug("[Symfony] Can't set persistent service {$serviceName}: " . $e->getMessage());
}
}
if ($profiler = $this->getProfiler()) {
$profiler->enable();
}
}
protected function ensureKernelShutdown(): void
{
$this->kernel->boot();
$this->kernel->shutdown();
}
private function getContainer(): ?ContainerInterface
{
/** @var ContainerInterface $container */
$container = $this->kernel->getContainer();
return $container->has('test.service_container')
? $container->get('test.service_container')
: $container;
}
private function getProfiler(): ?Profiler
{
return $this->container->has('profiler')
? $this->container->get('profiler')
: null;
}
private function getService(string $serviceName): ?object
{
return $this->container->has($serviceName)
? $this->container->get($serviceName)
: null;
}
private function persistDoctrineConnections(): void
{
if (!$this->container->hasParameter('doctrine.connections')) {
return;
}
if ($this->container instanceof TestContainer) {
$reflectedTestContainer = new ReflectionMethod($this->container, 'getPublicContainer');
$reflectedTestContainer->setAccessible(true);
$publicContainer = $reflectedTestContainer->invoke($this->container);
} else {
$publicContainer = $this->container;
}
$reflectedContainer = new ReflectionClass($publicContainer);
$reflectionTarget = $reflectedContainer->hasProperty('parameters')
? $publicContainer
: $publicContainer->getParameterBag();
$reflectedParameters = new ReflectionProperty($reflectionTarget, 'parameters');
$reflectedParameters->setAccessible(true);
$parameters = $reflectedParameters->getValue($reflectionTarget);
unset($parameters['doctrine.connections']);
$reflectedParameters->setValue($reflectionTarget, $parameters);
}
}