1
|
|
|
<?php |
2
|
|
|
namespace Yoanm\Behat3SymfonyExtension\Factory; |
3
|
|
|
|
4
|
|
|
use Psr\Log\LoggerInterface; |
5
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
6
|
|
|
use Yoanm\Behat3SymfonyExtension\Dispatcher\BehatKernelEventDispatcher; |
7
|
|
|
|
8
|
|
|
class KernelFactory |
9
|
|
|
{ |
10
|
|
|
const KERNEL_BRIDGE_CLASS_NAME = 'YoanmBehat3SymfonyKernelBridge'; |
11
|
|
|
const KERNEL_BRIDGE_TEMPLATE_COMMENT = <<<COMMENT |
12
|
|
|
|
13
|
|
|
/******** WARNING : THIS FILE IS JUST A TEMPLATE, IT IS NOT LOADABLE AS IS ********/ |
14
|
|
|
COMMENT; |
15
|
|
|
|
16
|
|
|
/** @var BehatKernelEventDispatcher */ |
17
|
|
|
private $behatKernelEventDispatcher; |
18
|
|
|
/** @var LoggerInterface */ |
19
|
|
|
private $logger; |
20
|
|
|
/** @var array */ |
21
|
|
|
private $kernelConfig; |
22
|
|
|
/** @var bool */ |
23
|
|
|
private $debugMode; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* KernelFactory constructor. |
28
|
|
|
* @param BehatKernelEventDispatcher $behatKernelEventDispatcher |
29
|
|
|
* @param array $kernelConfig |
30
|
|
|
* @param boolean $debugMode |
31
|
|
|
*/ |
32
|
3 |
|
public function __construct( |
33
|
|
|
BehatKernelEventDispatcher $behatKernelEventDispatcher, |
34
|
|
|
LoggerInterface $logger, |
35
|
|
|
array $kernelConfig, |
36
|
|
|
$debugMode = false |
37
|
|
|
) { |
38
|
3 |
|
$this->behatKernelEventDispatcher = $behatKernelEventDispatcher; |
39
|
3 |
|
$this->logger = $logger; |
40
|
3 |
|
$this->debugMode = $debugMode; |
41
|
3 |
|
$this->kernelConfig = $kernelConfig; |
42
|
3 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return KernelInterface |
46
|
|
|
* |
47
|
|
|
* @throws \Exception |
48
|
|
|
*/ |
49
|
3 |
|
public function load() |
50
|
|
|
{ |
51
|
|
|
// Write the custom kernel file at same level than original one for autoloading purpose |
52
|
3 |
|
$originAppKernelDir = dirname($this->kernelConfig['path']); |
53
|
3 |
|
$bridgeId = uniqid(); |
54
|
3 |
|
$kernelBridgeClassName = self::KERNEL_BRIDGE_CLASS_NAME.$bridgeId; |
55
|
3 |
|
$customAppKernelPath = sprintf('%s/%s.php', $originAppKernelDir, $kernelBridgeClassName); |
56
|
|
|
|
57
|
3 |
|
$this->logger->debug('Kernel bridge file path : "{file_path}"', ['file_path' => $customAppKernelPath]); |
58
|
|
|
try { |
59
|
|
|
/* /!\ YoanmBehat3SymfonyKernelBridge.php is just template file /!\ */ |
60
|
3 |
|
$kernelBridgeTemplateFile = __DIR__.'/../Bridge/YoanmBehat3SymfonyKernelBridge.php'; |
61
|
3 |
|
$this->logger->debug( |
62
|
3 |
|
'Loading kernel bridge template: "{file_path}"', |
63
|
3 |
|
['file_path' => $kernelBridgeTemplateFile] |
64
|
3 |
|
); |
65
|
3 |
|
$template = file_get_contents($kernelBridgeTemplateFile); |
66
|
|
|
|
67
|
3 |
|
$this->logger->debug('Writing kernel bridge class'); |
68
|
3 |
|
file_put_contents( |
69
|
3 |
|
$customAppKernelPath, |
70
|
3 |
|
$template = str_replace( |
71
|
3 |
|
['__BridgeId__', '__OriginalKernelClassNameToReplace__', self::KERNEL_BRIDGE_TEMPLATE_COMMENT], |
72
|
3 |
|
[$bridgeId, $this->kernelConfig['class'], ''], |
73
|
|
|
$template |
74
|
3 |
|
) |
75
|
3 |
|
); |
76
|
|
|
|
77
|
3 |
|
$this->logger->debug('Loading kernel bridge ...'); |
78
|
3 |
|
require($customAppKernelPath); |
79
|
3 |
|
$this->logger->debug('Loading kernel bridge : DONE'); |
80
|
|
|
|
81
|
3 |
|
$this->cleanKernelBridgeFile($customAppKernelPath); |
82
|
|
|
|
83
|
3 |
|
$this->logger->debug( |
84
|
3 |
|
'Instanciate kernel bridge with [env: {env}, debug: {debug}]', |
85
|
|
|
[ |
86
|
3 |
|
'env' => $this->kernelConfig['env'], |
87
|
3 |
|
'debug' => $this->kernelConfig['debug'], |
88
|
|
|
] |
89
|
3 |
|
); |
90
|
|
|
|
91
|
3 |
|
$kernelBridge = new $kernelBridgeClassName($this->kernelConfig['env'], $this->kernelConfig['debug']); |
92
|
2 |
|
$kernelBridge->setBehatKernelEventDispatcher($this->behatKernelEventDispatcher); |
93
|
|
|
|
94
|
2 |
|
$this->logger->debug('Kernel bridge init DONE'); |
95
|
|
|
|
96
|
2 |
|
return $kernelBridge; |
97
|
1 |
|
} catch (\Exception $e) { |
98
|
1 |
|
$this->logger->error('Exception during kernel bridge init : "{message}"', ['message' => $e->getMessage()]); |
99
|
1 |
|
$this->cleanKernelBridgeFile($customAppKernelPath); |
100
|
1 |
|
throw new \Exception('An exception occured during Kernel decoration : '.$e->getMessage(), 0, $e); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
3 |
|
protected function cleanKernelBridgeFile($file) |
105
|
|
|
{ |
106
|
3 |
|
if (false === $this->debugMode) { |
107
|
2 |
|
if (file_exists($file)) { |
108
|
2 |
|
$this->logger->debug('Removing kernel bridge file: "{file_path}"', ['file_path' => $file]); |
109
|
2 |
|
unlink($file); |
110
|
2 |
|
} |
111
|
|
|
//clean old bridge files too |
112
|
2 |
|
array_map( |
113
|
2 |
|
'unlink', |
114
|
2 |
|
glob(sprintf( |
115
|
2 |
|
'%s/%s*.php', |
116
|
2 |
|
dirname($this->kernelConfig['path']), |
117
|
|
|
KernelFactory::KERNEL_BRIDGE_CLASS_NAME |
118
|
2 |
|
)) |
119
|
2 |
|
); |
120
|
2 |
|
} |
121
|
3 |
|
} |
122
|
|
|
} |
123
|
|
|
|