Completed
Push — master ( d86995...0e9673 )
by Iurii
02:32
created

Main::loadLibrary()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
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
     * Implements hook "module.enable.after"
128
     */
129
    public function hookModuleEnableAfter()
130
    {
131
        $this->library->clearCache();
132
    }
133
134
    /**
135
     * Implements hook "module.disable.after"
136
     */
137
    public function hookModuleDisableAfter()
138
    {
139
        $this->library->clearCache();
140
    }
141
142
    /**
143
     * Implements hook "module.install.after"
144
     */
145
    public function hookModuleInstallAfter()
146
    {
147
        $this->library->clearCache();
148
    }
149
150
    /**
151
     * Implements hook "module.uninstall.after"
152
     */
153
    public function hookModuleUninstallAfter()
154
    {
155
        $this->library->clearCache();
156
    }
157
158
    /**
159
     * Returns a path to Kint's init file
160
     * @return string
161
     * @throws RuntimeException
162
     */
163
    public function loadLibrary()
164
    {
165
        $this->library->load('kint');
166
167
        if (!class_exists('Kint')) {
168
            throw new RuntimeException('Kint library not found');
169
        }
170
    }
171
172
    /**
173
     * Sets module specific assets
174
     * @param \gplcart\core\Controller $controller
175
     */
176
    protected function setModuleAssets($controller)
177
    {
178
        if (!$controller->isInternalRoute()) {
179
180
            $settings = $this->module->getSettings('dev');
181
182
            if (!empty($settings['status'])) {
183
                $controller->setJsSettings('dev', array('key' => $settings['key']));
184
                $controller->setJs(__DIR__ . '/js/common.js', array('position' => 'bottom'));
185
                $controller->setCss(__DIR__ . '/css/common.css');
186
            }
187
        }
188
    }
189
190
    /**
191
     * Adds toolbar
192
     * @param string $html
193
     * @param \gplcart\core\Controller $controller
194
     */
195
    protected function setDevToolbar(&$html, $controller)
196
    {
197
        if (!$controller->isInternalRoute()) {
198
199
            $settings = $this->module->getSettings('dev');
200
201
            if (!empty($settings['status'])) {
202
203
                $data = array(
204
                    'key' => $settings['key'],
205
                    'time' => microtime(true) - GC_START,
206
                    'queries' => $this->db->getExecutedQueries()
207
                );
208
209
                $toolbar = $controller->render('dev|toolbar', $data);
210
                $html = substr_replace($html, $toolbar, strpos($html, '</body>'), 0);
211
            }
212
        }
213
    }
214
215
    /**
216
     * Configure system logger
217
     */
218
    protected function setLogger()
219
    {
220
        $settings = $this->module->getSettings('dev');
221
222
        $this->logger->printError(!empty($settings['print_error']))
223
            ->errorToException(!empty($settings['error_to_exception']))
224
            ->printBacktrace(!empty($settings['print_error_backtrace']));
225
    }
226
227
}
228