ContainerPanel::getContainerProperty()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 10
ccs 0
cts 6
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
/*
4
 * This file is part of Biurad opensource projects.
5
 *
6
 * @copyright 2019 Biurad Group (https://biurad.com/)
7
 * @license   https://opensource.org/licenses/BSD-3-Clause License
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Flange\Debug\Tracy;
14
15
use Nette;
16
use Rade\DI\Container;
17
use Tracy;
18
19
/**
20
 * Dependency injection container panel for Debugger Bar.
21
 */
22
class ContainerPanel implements Tracy\IBarPanel
23
{
24
    use Nette\SmartObject;
25
26
    public static ?float $compilationTime = null;
27
28
    private Container $container;
29
    private ?float $elapsedTime;
30
31
    public function __construct(Container $container)
32
    {
33
        $this->container = $container;
34
        $this->elapsedTime = self::$compilationTime ? \microtime(true) - self::$compilationTime : null;
35
    }
36
37
    /**
38
     * Renders tab.
39
     */
40
    public function getTab(): string
41
    {
42
        return Nette\Utils\Helpers::capture(function (): void {
43
            $elapsedTime = $this->elapsedTime;
44
45
            require __DIR__.'/templates/ContainerPanel.tab.phtml';
46
        });
47
    }
48
49
    /**
50
     * Renders panel.
51
     *
52
     * @throws \ReflectionException
53
     * @throws \Throwable
54
     */
55
    public function getPanel(): string
56
    {
57
        $rc = new \ReflectionClass($this->container);
58
        $types = [];
59
        $wiring = $this->getContainerProperty('types', $rc);
60
        $ids = \array_keys(($methodsMap = $this->getContainerProperty('methodsMap', $rc)) + $this->container->getDefinitions());
61
62
        foreach ($ids as $id) {
63
            foreach ($wiring as $type => $names) {
64
                if (\in_array($id, $names, true)) {
65
                    $types[$id] = $type;
66
67
                    continue 2;
68
                }
69
            }
70
        }
71
72
        \ksort($types);
73
74
        return Nette\Utils\Helpers::capture(function () use ($types, $wiring, $methodsMap, $rc): void {
75
            $file = $rc->getFileName();
76
            $instances = $this->getContainerProperty('definitions', $rc) + $methodsMap;
77
            $services = $this->getContainerProperty('services', $rc);
78
            $configs = $this->getContainerProperty('parameters', $rc);
79
80
            require __DIR__.'/templates/ContainerPanel.panel.phtml';
81
        });
82
    }
83
84
    /**
85
     * @return mixed
86
     */
87
    private function getContainerProperty(string $property, \ReflectionClass $appRef)
88
    {
89
        if (!$appRef->hasProperty($property)) {
90
            $appRef = $appRef->getParentClass();
91
        }
92
93
        $prop = $appRef->getProperty($property);
94
        $prop->setAccessible(true);
95
96
        return $prop->getValue($this->container);
97
    }
98
}
99