forked from Codeception/module-symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCacheTrait.php
More file actions
85 lines (67 loc) · 2.4 KB
/
CacheTrait.php
File metadata and controls
85 lines (67 loc) · 2.4 KB
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
<?php
declare(strict_types=1);
namespace Codeception\Module\Symfony;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Profiler\Profile;
use function array_unique;
use function array_values;
trait CacheTrait
{
private ?object $cachedResponse = null;
private ?Profile $cachedProfile = null;
/** @var array<string, mixed> */
protected array $state = [];
public function _getContainer(): ContainerInterface
{
$container = $this->kernel->getContainer();
/** @var ContainerInterface $testContainer */
$testContainer = $container->has('test.service_container') ? $container->get('test.service_container') : $container;
return $testContainer;
}
/** @return list<non-empty-string> */
protected function getInternalDomains(): array
{
if (isset($this->state['internalDomains'])) {
/** @var list<non-empty-string> */
return $this->state['internalDomains'];
}
$domains = [];
foreach ($this->grabRouterService()->getRouteCollection() as $route) {
if ($route->getHost() !== '') {
$regex = $route->compile()->getHostRegex();
if ($regex !== null && $regex !== '') {
$domains[] = $regex;
}
}
}
/** @var list<non-empty-string> $domains */
$domains = array_values(array_unique($domains));
return $this->state['internalDomains'] = $domains;
}
protected function clearRouterCache(): void
{
unset($this->state['internalDomains'], $this->state['cachedRoutes']);
}
/**
* @template T of object
* @param class-string<T> $expectedClass
* @param string[] $serviceIds
* @return T|null
*/
protected function grabCachedService(string $expectedClass, array $serviceIds): ?object
{
$serviceId = $this->state[$expectedClass] ??= (function () use ($serviceIds, $expectedClass): ?string {
foreach ($serviceIds as $id) {
if ($this->getService($id) instanceof $expectedClass) {
return $id;
}
}
return null;
})();
if (!is_string($serviceId)) {
return null;
}
$service = $this->getService($serviceId);
return $service instanceof $expectedClass ? $service : null;
}
}