|
1
|
|
|
<?php |
|
2
|
|
|
namespace Yoanm\Behat3SymfonyExtension\Factory; |
|
3
|
|
|
|
|
4
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
|
5
|
|
|
use Yoanm\Behat3SymfonyExtension\Dispatcher\BehatKernelEventDispatcher; |
|
6
|
|
|
|
|
7
|
|
|
class KernelFactory |
|
8
|
|
|
{ |
|
9
|
|
|
/** @var BehatKernelEventDispatcher */ |
|
10
|
|
|
private $behatKernelEventDispatcher; |
|
11
|
|
|
/** @var string */ |
|
12
|
|
|
private $originalKernelPath; |
|
13
|
|
|
/** @var string */ |
|
14
|
|
|
private $originalKernelClassName; |
|
15
|
|
|
|
|
16
|
|
|
/** @var string */ |
|
17
|
|
|
private $kernelEnvironment; |
|
18
|
|
|
/** @var bool */ |
|
19
|
|
|
private $kernelDebug; |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param BehatKernelEventDispatcher $behatKernelEventDispatcher |
|
24
|
|
|
* @param string $originalKernelPath |
|
25
|
|
|
* @param string $originalKernelClassName |
|
26
|
|
|
* @param string $kernelEnvironment |
|
27
|
|
|
* @param bool $kernelDebug |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct( |
|
30
|
|
|
BehatKernelEventDispatcher $behatKernelEventDispatcher, |
|
31
|
|
|
$originalKernelPath, |
|
32
|
|
|
$originalKernelClassName, |
|
33
|
|
|
$kernelEnvironment, |
|
34
|
|
|
$kernelDebug |
|
35
|
|
|
) { |
|
36
|
|
|
$this->originalKernelPath = $originalKernelPath; |
|
37
|
|
|
$this->originalKernelClassName = $originalKernelClassName; |
|
38
|
|
|
$this->kernelEnvironment = $kernelEnvironment; |
|
39
|
|
|
$this->kernelDebug = $kernelDebug; |
|
40
|
|
|
$this->behatKernelEventDispatcher = $behatKernelEventDispatcher; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return KernelInterface |
|
45
|
|
|
* |
|
46
|
|
|
* @throws \Exception |
|
47
|
|
|
*/ |
|
48
|
|
|
public function load() |
|
49
|
|
|
{ |
|
50
|
|
|
$className = 'YoanmBehat3SymfonyKernelBridge'; |
|
51
|
|
|
$originalKernelClassName = $this->originalKernelClassName; |
|
52
|
|
|
$template = <<<TEMPLATE |
|
53
|
|
|
<?php |
|
54
|
|
|
/** |
|
55
|
|
|
* Autogenerated by Behat3SymfonyExtension. |
|
56
|
|
|
* Don't touch the content it will be erased ! |
|
57
|
|
|
* See Yoanm\Behat3SymfonyExtension\Factory\KernelFactory::load() |
|
58
|
|
|
* |
|
59
|
|
|
* This file should be automatically deleted after kernel load. Except if kernel.kernelDebug === true |
|
60
|
|
|
*/ |
|
61
|
|
|
use Yoanm\Behat3SymfonyExtension\Dispatcher\BehatKernelEventDispatcher; |
|
62
|
|
|
use ${originalKernelClassName} as ${className}BaseKernel; |
|
63
|
|
|
|
|
64
|
|
|
class $className extends ${className}BaseKernel |
|
65
|
|
|
{ |
|
66
|
|
|
/** @var BehatKernelEventDispatcher */ |
|
67
|
|
|
private \$behatKernelEventDispatcher; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param BehatKernelEventDispatcher \$behatKernelEventDispatcher |
|
71
|
|
|
*/ |
|
72
|
|
|
public function setBehatKernelEventDispatcher(BehatKernelEventDispatcher \$behatKernelEventDispatcher) |
|
73
|
|
|
{ |
|
74
|
|
|
\$this->behatKernelEventDispatcher = \$behatKernelEventDispatcher; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Will dispatch events related to kernel boot action |
|
79
|
|
|
* Rely on parent class method |
|
80
|
|
|
* |
|
81
|
|
|
* {@inheritdoc} |
|
82
|
|
|
*/ |
|
83
|
|
|
public function boot() |
|
84
|
|
|
{ |
|
85
|
|
|
\$this->behatKernelEventDispatcher->beforeBoot(\$this); |
|
86
|
|
|
parent::boot(); |
|
87
|
|
|
\$this->behatKernelEventDispatcher->afterBoot(\$this); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Will dispatch events related to kernel shutdown action |
|
92
|
|
|
* Rely on parent class method |
|
93
|
|
|
* |
|
94
|
|
|
* {@inheritdoc} |
|
95
|
|
|
*/ |
|
96
|
|
|
public function shutdown() |
|
97
|
|
|
{ |
|
98
|
|
|
\$this->behatKernelEventDispatcher->beforeShutdown(\$this); |
|
99
|
|
|
parent::shutdown(); |
|
100
|
|
|
\$this->behatKernelEventDispatcher->afterShutdown(\$this); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
TEMPLATE; |
|
105
|
|
|
|
|
106
|
|
|
return $this->createAndLoadCustomAppKernel($template, $className); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param $template |
|
111
|
|
|
* @param $className |
|
112
|
|
|
* @return mixed |
|
113
|
|
|
* @throws \Exception |
|
114
|
|
|
*/ |
|
115
|
|
|
protected function createAndLoadCustomAppKernel($template, $className) |
|
116
|
|
|
{ |
|
117
|
|
|
// Write the custom kernel file at same level than original one for autoloading purpose |
|
118
|
|
|
$originAppKernelDir = dirname($this->originalKernelPath); |
|
119
|
|
|
$customAppKernelPath = sprintf('%s/%s.php', $originAppKernelDir, $className); |
|
120
|
|
|
try { |
|
121
|
|
|
file_put_contents($customAppKernelPath, $template); |
|
122
|
|
|
|
|
123
|
|
|
require($customAppKernelPath); |
|
124
|
|
|
unlink($customAppKernelPath); |
|
125
|
|
|
|
|
126
|
|
|
$class = new $className($this->kernelEnvironment, $this->kernelDebug); |
|
127
|
|
|
$class->setBehatKernelEventDispatcher($this->behatKernelEventDispatcher); |
|
128
|
|
|
|
|
129
|
|
|
return $class; |
|
130
|
|
|
} catch (\Exception $e) { |
|
131
|
|
|
unlink($customAppKernelPath); |
|
132
|
|
|
throw new \Exception( |
|
133
|
|
|
'An exception occured during Kernel decoration : '.$e->getMessage(), |
|
134
|
|
|
0, |
|
135
|
|
|
$e |
|
136
|
|
|
); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|