|
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 bool */ |
|
21
|
|
|
private $debugMode; |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* KernelFactory constructor. |
|
26
|
|
|
* @param BehatKernelEventDispatcher $behatKernelEventDispatcher |
|
27
|
|
|
* @param array $kernelConfig |
|
28
|
|
|
* @param boolean $debugMode |
|
29
|
|
|
*/ |
|
30
|
3 |
|
public function __construct( |
|
31
|
|
|
BehatKernelEventDispatcher $behatKernelEventDispatcher, |
|
32
|
|
|
LoggerInterface $logger, |
|
33
|
|
|
array $kernelConfig, |
|
34
|
|
|
$debugMode = false |
|
35
|
|
|
) { |
|
36
|
3 |
|
$this->behatKernelEventDispatcher = $behatKernelEventDispatcher; |
|
37
|
3 |
|
$this->logger = $logger; |
|
38
|
3 |
|
$this->debugMode = $debugMode; |
|
39
|
3 |
|
$this->kernelConfig = $kernelConfig; |
|
|
|
|
|
|
40
|
3 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @return KernelInterface |
|
44
|
|
|
* |
|
45
|
|
|
* @throws \Exception |
|
46
|
|
|
*/ |
|
47
|
3 |
|
public function load() |
|
48
|
|
|
{ |
|
49
|
|
|
// Write the custom kernel file at same level than original one for autoloading purpose |
|
50
|
3 |
|
$originAppKernelDir = dirname($this->kernelConfig['path']); |
|
51
|
3 |
|
$bridgeId = uniqid(); |
|
52
|
3 |
|
$kernelBridgeClassName = self::KERNEL_BRIDGE_CLASS_NAME.$bridgeId; |
|
53
|
3 |
|
$customAppKernelPath = sprintf('%s/%s.php', $originAppKernelDir, $kernelBridgeClassName); |
|
54
|
|
|
|
|
55
|
3 |
|
$this->logger->debug('Kernel bridge file path : "{file_path}"', ['file_path' => $customAppKernelPath]); |
|
56
|
|
|
try { |
|
57
|
|
|
/* /!\ YoanmBehat3SymfonyKernelBridge.php is just template file /!\ */ |
|
58
|
3 |
|
$kernelBridgeTemplateFile = __DIR__.'/../Bridge/YoanmBehat3SymfonyKernelBridge.php'; |
|
59
|
3 |
|
$this->logger->debug( |
|
60
|
3 |
|
'Loading kernel bridge template: "{file_path}"', |
|
61
|
3 |
|
['file_path' => $kernelBridgeTemplateFile] |
|
62
|
3 |
|
); |
|
63
|
3 |
|
$template = file_get_contents($kernelBridgeTemplateFile); |
|
64
|
|
|
|
|
65
|
3 |
|
$this->logger->debug('Writing kernel bridge class'); |
|
66
|
3 |
|
file_put_contents( |
|
67
|
3 |
|
$customAppKernelPath, |
|
68
|
3 |
|
$template = str_replace( |
|
69
|
3 |
|
['__BridgeId__', '__OriginalKernelClassNameToReplace__', self::KERNEL_BRIDGE_TEMPLATE_COMMENT], |
|
70
|
3 |
|
[$bridgeId, $this->kernelConfig['class'], ''], |
|
71
|
|
|
$template |
|
72
|
3 |
|
) |
|
73
|
3 |
|
); |
|
74
|
|
|
|
|
75
|
3 |
|
$this->logger->debug('Loading kernel bridge ...'); |
|
76
|
3 |
|
require($customAppKernelPath); |
|
77
|
3 |
|
$this->logger->debug('Loading kernel bridge : DONE'); |
|
78
|
|
|
|
|
79
|
3 |
|
$this->cleanKernelBridgeFile($customAppKernelPath); |
|
80
|
|
|
|
|
81
|
3 |
|
$this->logger->debug( |
|
82
|
3 |
|
'Instanciate kernel bridge with [env: {env}, debug: {debug}]', |
|
83
|
|
|
[ |
|
84
|
3 |
|
'env' => $this->kernelConfig['env'], |
|
85
|
3 |
|
'debug' => $this->kernelConfig['debug'], |
|
86
|
|
|
] |
|
87
|
3 |
|
); |
|
88
|
|
|
|
|
89
|
3 |
|
$kernelBridge = new $kernelBridgeClassName($this->kernelConfig['env'], $this->kernelConfig['debug']); |
|
90
|
2 |
|
$kernelBridge->setBehatKernelEventDispatcher($this->behatKernelEventDispatcher); |
|
91
|
|
|
|
|
92
|
2 |
|
$this->logger->debug('Kernel bridge init DONE'); |
|
93
|
|
|
|
|
94
|
2 |
|
return $kernelBridge; |
|
95
|
1 |
|
} catch (\Exception $e) { |
|
96
|
1 |
|
$this->logger->error('Exception during kernel bridge init : "{message}"', ['message' => $e->getMessage()]); |
|
97
|
1 |
|
$this->cleanKernelBridgeFile($customAppKernelPath); |
|
98
|
1 |
|
throw new \Exception('An exception occured during Kernel decoration : '.$e->getMessage(), 0, $e); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
3 |
|
protected function cleanKernelBridgeFile($file) |
|
103
|
|
|
{ |
|
104
|
3 |
|
if (false === $this->debugMode) { |
|
105
|
2 |
|
if (file_exists($file)) { |
|
106
|
2 |
|
$this->logger->debug('Removing kernel bridge file: "{file_path}"', ['file_path' => $file]); |
|
107
|
2 |
|
unlink($file); |
|
108
|
2 |
|
} |
|
109
|
|
|
//clean old bridge files too |
|
110
|
2 |
|
array_map( |
|
111
|
2 |
|
'unlink', |
|
112
|
2 |
|
glob(sprintf( |
|
113
|
2 |
|
'%s/%s*.php', |
|
114
|
2 |
|
dirname($this->kernelConfig['path']), |
|
115
|
|
|
KernelFactory::KERNEL_BRIDGE_CLASS_NAME |
|
116
|
2 |
|
)) |
|
117
|
2 |
|
); |
|
118
|
2 |
|
} |
|
119
|
3 |
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: