|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Zalas\BundleTest\HttpKernel; |
|
5
|
|
|
|
|
6
|
|
|
use Symfony\Component\HttpKernel\Bundle\BundleInterface; |
|
7
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
|
8
|
|
|
use Zalas\BundleTest\HttpKernel\Exception\ClassNotFoundException; |
|
9
|
|
|
use Zalas\BundleTest\HttpKernel\Exception\KernelNotSupportedException; |
|
10
|
|
|
|
|
11
|
|
|
final class KernelBuilder |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
private $kernelClass = TestKernel::class; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var KernelConfiguration |
|
20
|
|
|
*/ |
|
21
|
|
|
private $configuration; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct(string $namespace = KernelConfiguration::DEFAULT_NAMESPACE) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->configuration = new KernelConfiguration($namespace); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function createKernel(): KernelInterface |
|
29
|
|
|
{ |
|
30
|
|
|
return new $this->kernelClass($this->configuration); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function bootKernel(): KernelInterface |
|
34
|
|
|
{ |
|
35
|
|
|
$kernel = $this->createKernel(); |
|
36
|
|
|
$kernel->boot(); |
|
37
|
|
|
|
|
38
|
|
|
return $kernel; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function withEnvironment(string $environment): self |
|
42
|
|
|
{ |
|
43
|
|
|
$this->configuration = $this->configuration->withEnvironment($environment); |
|
44
|
|
|
|
|
45
|
|
|
return $this; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function withDebug(bool $debug): self |
|
49
|
|
|
{ |
|
50
|
|
|
$this->configuration = $this->configuration->withDebug($debug); |
|
51
|
|
|
|
|
52
|
|
|
return $this; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function withBundles(array $bundles): self |
|
56
|
|
|
{ |
|
57
|
|
|
$this->configuration = $this->configuration->withBundles($bundles); |
|
58
|
|
|
|
|
59
|
|
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function withBundle(BundleInterface $bundle): self |
|
63
|
|
|
{ |
|
64
|
|
|
$this->configuration = $this->configuration->withBundle($bundle); |
|
65
|
|
|
|
|
66
|
|
|
return $this; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function withBundleConfiguration(string $extensionName, array $configuration): self |
|
70
|
|
|
{ |
|
71
|
|
|
$this->configuration = $this->configuration->withBundleConfiguration($extensionName, $configuration); |
|
72
|
|
|
|
|
73
|
|
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function withPublicService(string $serviceId): self |
|
77
|
|
|
{ |
|
78
|
|
|
$this->configuration = $this->configuration->withPublicServiceId($serviceId); |
|
79
|
|
|
|
|
80
|
|
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function withPublicServices(array $serviceIds): self |
|
84
|
|
|
{ |
|
85
|
|
|
$this->configuration = $this->configuration->withPublicServiceIds($serviceIds); |
|
86
|
|
|
|
|
87
|
|
|
return $this; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function withTempDir(string $tempDir): self |
|
91
|
|
|
{ |
|
92
|
|
|
$this->configuration = $this->configuration->withTempDir($tempDir); |
|
93
|
|
|
|
|
94
|
|
|
return $this; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function withKernelClass(string $kernelClass): self |
|
98
|
|
|
{ |
|
99
|
|
|
$this->guardKernelClass($kernelClass); |
|
100
|
|
|
|
|
101
|
|
|
$this->kernelClass = $kernelClass; |
|
102
|
|
|
|
|
103
|
|
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
private function guardKernelClass(string $kernelClass): void |
|
107
|
|
|
{ |
|
108
|
|
|
if (!\class_exists($kernelClass)) { |
|
109
|
|
|
throw new ClassNotFoundException($kernelClass); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
if (TestKernel::class !== $kernelClass && !\in_array(TestKernel::class, \class_parents($kernelClass))) { |
|
113
|
|
|
throw new KernelNotSupportedException($kernelClass, TestKernel::class); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|