Test Failed
Push — feature/improve ( 4f0633...4583fc )
by Yo
02:10
created

KernelFactory::createAndLoadCustomAppKernel()   B

Complexity

Conditions 4
Paths 16

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

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