Registrator.php 4.39 KB
Newer Older
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
1
2
<?php

Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
3
4
declare(strict_types=1);

Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
5
6
namespace Praguebest\BarPanel;

Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
7
use ArrayObject;
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
8
use Nette\Utils\FileSystem;
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
9
use Nette\Utils\Strings;
10
use Praguebest\Tools;
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
11
use Praguebest\Tools\DBProfiler;
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
12
use Praguebest\Tools\WebSwitcherUrls;
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
13
14
use Smarty;
use Tracy\Debugger;
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
15
use Tracy\IBarPanel;
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
16
17
18

final class Registrator
{
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
19
20
    /** @var self */
    private static $instance;
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
21
    /** @var Smarty */
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
22
23
24
    private $smarty;
    /** @var IBarPanel[] */
    private $registeredPanels = [];
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
25
26
27
28
    /**
     * @var ArrayObject
     */
    private $settings;
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
29
30

    private function __construct(string $compileDir, string $cacheDir)
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
31
    {
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
32
        $this->smarty = new Smarty();
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
33
34
        $this->smarty->setCacheDir($cacheDir);
        $this->smarty->setCompileDir($compileDir);
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
35
36
37
38
39
40
41
42

        $this->settings = new ArrayObject();

        $settingsBarPanel = new SettingsBarPanel($this->smarty, $this->settings);

        $this->registeredPanels['settings'] = $settingsBarPanel;

        Debugger::getBar()->addPanel($settingsBarPanel);
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
43
44
    }

Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
45
    public static function getInstance(string $compileDir, string $cacheDir): Registrator
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
46
    {
47
48
        if (self::$instance === null) {
            self::$instance = new self($compileDir, $cacheDir);
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
49
50
        }

51
        return self::$instance;
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
52
    }
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
53

Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
54
55
56
57
58
    /**
     * @param string $errorDir
     * @param string $emailDeveloper
     * @return $this
     */
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
59
    public function registerDebugger(string $errorDir, string $emailDeveloper): Registrator
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
60
61
    {
        FileSystem::createDir($errorDir);
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
62

Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
63
64
65
66
        $currentSetting = new Tools\Setting('Debugger::$strictMode', 'Tracy strict mode', true);
        $this->settings->append($currentSetting);

        Debugger::$strictMode = $currentSetting->isOn();
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
67
68
        Debugger::$maxLength = 1024;
        Debugger::$maxDepth = 5;
69
        Debugger::enable('42@84.42.168.182,42@185.156.123.46', realpath($errorDir), Strings::replace($emailDeveloper, '/,;/', ','));
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
70

Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
71
72
73
74
75
76
77
        $this->addPanel(
            __METHOD__,
            function () use ($errorDir) {
                return new ErrorCountBar($errorDir, $this->smarty);
            }
        );

Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
78
79
80
        return $this;
    }

Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
81
82
83
84
85
86
87
88
89
90
91
    private function addPanel(string $method, callable $panelCreator): void
    {
        if (PHP_SAPI !== 'cli' && !array_key_exists($method, $this->registeredPanels) && Debugger::isEnabled()) {
            $panel = $panelCreator();
            assert($panel instanceof IBarPanel);
            $this->registeredPanels[$method] = $panel;

            Debugger::getBar()->addPanel($panel);
        }
    }

92
    public function registerAll(Tools\TraceableSmarty $smarty, WebSwitcherUrls $urls): Registrator
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
93
    {
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
94
        if (Debugger::isEnabled()) {
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
95
96
97
            $this->registerDBProfiler();
            $this->registerWebswitcher($urls);
            $this->registerSmarty($smarty);
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
98
        }
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
99
100

        return $this;
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
101
102
    }

Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
103
    public function registerDBProfiler(): Registrator
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
104
    {
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
105
106
107
        $this->addPanel(
            __METHOD__,
            function () {
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
108
                return new DBProfileBar(Tools\DBProfiler::getInstance(), $this->smarty);
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
109
110
            }
        );
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
111

Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
112
113
114
115
        $currentSetting = new Tools\Setting(DBProfiler::PROFILE_DB, 'Profile database');

        $this->settings->append($currentSetting);

Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
116
        return $this;
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
117
118
    }

Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
119
    public function registerWebswitcher(WebSwitcherUrls $urls): Registrator
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
120
    {
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
121
122
123
124
125
126
        $this->addPanel(
            __METHOD__,
            function () use ($urls) {
                return new WebSwitcher($urls, $this->smarty);
            }
        );
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
127

Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
128
        return $this;
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
129
130
    }

Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
131
    public function registerSmarty(Tools\TraceableSmarty $smarty): Registrator
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
132
    {
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
133
134
135
136
137
138
139
        $this->addPanel(
            __METHOD__,
            function () use ($smarty) {
                return new SmartyBarPanel($smarty, $this->smarty);
            }
        );

Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
140
141
142
143
        $currentSetting = new Tools\Setting(Tools\TraceableSmarty::SMARTY_VARIABLES, 'List Smarty variables');

        $this->settings->append($currentSetting);

Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
144
        return $this;
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
145
    }
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
146

Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
147
148
149
150
151
152
153
154
155
    public function registerInternalSmarty(Tools\TraceableSmarty $smarty): Registrator
    {
        $this->addPanel(
            __METHOD__,
            function () use ($smarty) {
                return new InternalSmartyBarPanel($smarty, $this->smarty);
            }
        );

Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
156
        $currentSetting = new Tools\Setting(Tools\TraceableSmarty::INTERNAL_VARIABLES, 'List internal Smarty variables');
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
157

Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
158
        $this->settings->append($currentSetting);
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
159

Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
160
        return $this;
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
161
    }
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
162

Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
163
}