Completed
Push — master ( 902cca...dde4bb )
by Paweł
138:22 queued 138:06
created

TestKernel::shutdown()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 15
rs 9.4286
cc 3
eloc 9
nc 3
nop 0
1
<?php
2
3
use Sylius\Bundle\CoreBundle\Kernel\Kernel;
4
5
/**
6
 * @author Kamil Kokot <[email protected]>
7
 */
8
class TestKernel extends Kernel
9
{
10
    /**
11
     * {@inheritdoc}
12
     */
13
    public function shutdown()
14
    {
15
        if (false === $this->booted) {
16
            return;
17
        }
18
19
        if (!in_array($this->environment, ['test', 'test_cached'])) {
20
            parent::shutdown();
21
            return;
22
        }
23
24
        $container = $this->container;
25
        parent::shutdown();
26
        $this->cleanupContainer($container);
27
    }
28
29
    /**
30
     * Remove all container references from all loaded services
31
     */
32
    protected function cleanupContainer($container)
33
    {
34
        $object = new \ReflectionObject($container);
35
        $property = $object->getProperty('services');
36
        $property->setAccessible(true);
37
38
        $services = $property->getValue($container) ?: [];
39
        foreach ($services as $id => $service) {
40
            if ('kernel' === $id) {
41
                continue;
42
            }
43
44
            $serviceObject = new \ReflectionObject($service);
45
            foreach ($serviceObject->getProperties() as $prop) {
46
                $prop->setAccessible(true);
47
                if ($prop->isStatic()) {
48
                    continue;
49
                }
50
51
                $prop->setValue($service, null);
52
            }
53
        }
54
        $property->setValue($container, null);
55
    }
56
}
57