Main::hookRouteList()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
/**
4
 * @package Dev
5
 * @author Iurii Makukh
6
 * @copyright Copyright (c) 2017, Iurii Makukh
7
 * @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+
8
 */
9
10
namespace gplcart\modules\dev;
11
12
use gplcart\core\Config;
13
use gplcart\core\Library;
14
use gplcart\core\Logger;
15
use gplcart\core\Module;
16
use RuntimeException;
17
18
/**
19
 * Main class for Dev module
20
 */
21
class Main
22
{
23
24
    /**
25
     * Database class instance
26
     * @var \gplcart\core\Database $db
27
     */
28
    protected $db;
29
30
    /**
31
     * Module class instance
32
     * @var \gplcart\core\Module $module
33
     */
34
    protected $module;
35
36
    /**
37
     * Library class instance
38
     * @var \gplcart\core\Library $library
39
     */
40
    protected $library;
41
42
    /**
43
     * Logger class instance
44
     * @var \gplcart\core\Logger $logger
45
     */
46
    protected $logger;
47
48
    /**
49
     * @param Logger $logger
50
     * @param Config $config
51
     * @param Library $library
52
     * @param Module $module
53
     */
54
    public function __construct(Logger $logger, Config $config, Library $library, Module $module)
55
    {
56
        $this->module = $module;
57
        $this->logger = $logger;
58
        $this->library = $library;
59
        $this->db = $config->getDb();
60
    }
61
62
    /**
63
     * Implements hook "construct"
64
     */
65
    public function hookConstruct()
66
    {
67
        $this->setLogger();
68
        $this->loadLibrary();
69
    }
70
71
    /**
72
     * Implements hook "construct.controller"
73
     * @param \gplcart\core\Controller $controller
74
     */
75
    public function hookConstructController($controller)
76
    {
77
        $this->setModuleAssets($controller);
78
    }
79
80
    /**
81
     * Implements hook "template.output"
82
     * @param string $html
83
     * @param \gplcart\core\Controller $controller
84
     */
85
    public function hookTemplateOutput(&$html, $controller)
86
    {
87
        $this->setDevToolbar($html, $controller);
88
    }
89
90
    /**
91
     * Implements hook "library.list"
92
     * @param array $libraries
93
     */
94
    public function hookLibraryList(array &$libraries)
95
    {
96
        $libraries['kint'] = array(
97
            'name' => 'Kint',
98
            'description' => 'A powerful and modern PHP debugging tool',
99
            'url' => 'https://github.com/kint-php/kint',
100
            'download' => 'https://github.com/kint-php/kint/archive/2.2.zip',
101
            'type' => 'php',
102
            'vendor' => 'kint-php/kint',
103
            'version' => '2.2',
104
            'module' => 'dev',
105
            'files' => array(
106
                GC_FILE_AUTOLOAD,
107
                'init.php'
108
            )
109
        );
110
    }
111
112
    /**
113
     * Implements hook "route.list"
114
     * @param array $routes
115
     */
116
    public function hookRouteList(array &$routes)
117
    {
118
        $routes['admin/module/settings/dev'] = array(
119
            'access' => GC_PERM_SUPERADMIN,
120
            'handlers' => array(
121
                'controller' => array('gplcart\\modules\\dev\\controllers\\Settings', 'editSettings')
122
            )
123
        );
124
    }
125
126
    /**
127
     * Returns a path to Kint's init file
128
     * @return string
129
     * @throws RuntimeException
130
     */
131
    public function loadLibrary()
132
    {
133
        $this->library->load('kint');
134
135
        if (!class_exists('Kint')) {
136
            throw new RuntimeException('Kint library not found');
137
        }
138
    }
139
140
    /**
141
     * Sets module specific assets
142
     * @param \gplcart\core\Controller $controller
143
     */
144
    protected function setModuleAssets($controller)
145
    {
146
        if (!$controller->isInternalRoute()) {
147
148
            $settings = $this->module->getSettings('dev');
149
150
            if (!empty($settings['status'])) {
151
                $controller->setJsSettings('dev', array('key' => $settings['key']));
152
                $controller->setJs(__DIR__ . '/js/common.js', array('position' => 'bottom'));
153
                $controller->setCss(__DIR__ . '/css/common.css');
154
            }
155
        }
156
    }
157
158
    /**
159
     * Adds toolbar
160
     * @param string $html
161
     * @param \gplcart\core\Controller $controller
162
     */
163
    protected function setDevToolbar(&$html, $controller)
164
    {
165
        if (!$controller->isInternalRoute()) {
166
167
            $settings = $this->module->getSettings('dev');
168
169
            if (!empty($settings['status'])) {
170
171
                $data = array(
172
                    'key' => $settings['key'],
173
                    'time' => microtime(true) - GC_START,
174
                    'queries' => $this->db->getExecutedQueries()
175
                );
176
177
                $toolbar = $controller->render('dev|toolbar', $data);
178
                $html = substr_replace($html, $toolbar, strpos($html, '</body>'), 0);
179
            }
180
        }
181
    }
182
183
    /**
184
     * Configure system logger
185
     */
186
    protected function setLogger()
187
    {
188
        $settings = $this->module->getSettings('dev');
189
190
        $this->logger->printError(!empty($settings['print_error']))
191
            ->errorToException(!empty($settings['error_to_exception']))
192
            ->printBacktrace(!empty($settings['print_error_backtrace']));
193
    }
194
195
}
196