Completed
Push — master ( 14cf53...5ba71e )
by Iurii
01:22
created

Main::hookConstructController()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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\Module,
13
    gplcart\core\Logger,
14
    gplcart\core\Config,
15
    gplcart\core\Library;
16
use gplcart\core\exceptions\Dependency as DependencyException;
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
        require_once $this->getKintFile();
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/raveren/kint',
100
            'download' => 'https://github.com/kint-php/kint/archive/2.0-alpha4.zip',
101
            'type' => 'php',
102
            'version' => '2.0-alpha4',
103
            'module' => 'dev',
104
            'files' => array(
105
                'vendor/kint-php/kint/init.php'
106
            )
107
        );
108
    }
109
110
    /**
111
     * Implements hook "route.list"
112
     * @param array $routes
113
     */
114
    public function hookRouteList(array &$routes)
115
    {
116
        $routes['admin/module/settings/dev'] = array(
117
            'access' => '__superadmin',
118
            'handlers' => array(
119
                'controller' => array('gplcart\\modules\\dev\\controllers\\Settings', 'editSettings')
120
            )
121
        );
122
    }
123
124
    /**
125
     * Implements hook "module.enable.after"
126
     */
127
    public function hookModuleEnableAfter()
128
    {
129
        $this->library->clearCache();
130
    }
131
132
    /**
133
     * Implements hook "module.disable.after"
134
     */
135
    public function hookModuleDisableAfter()
136
    {
137
        $this->library->clearCache();
138
    }
139
140
    /**
141
     * Implements hook "module.install.after"
142
     */
143
    public function hookModuleInstallAfter()
144
    {
145
        $this->library->clearCache();
146
    }
147
148
    /**
149
     * Implements hook "module.uninstall.after"
150
     */
151
    public function hookModuleUninstallAfter()
152
    {
153
        $this->library->clearCache();
154
    }
155
156
    /**
157
     * Returns a path to Kint's init file
158
     * @return string
159
     */
160
    public function getKintFile()
161
    {
162
        $file = __DIR__ . '/vendor/kint-php/kint/init.php';
163
164
        if (is_file($file)) {
165
            return $file;
166
        }
167
168
        throw new DependencyException("Kint file $file not found");
169
    }
170
171
    /**
172
     * Sets module specific assets
173
     * @param \gplcart\core\Controller $controller
174
     */
175
    protected function setModuleAssets($controller)
176
    {
177
        if (!$controller->isInternalRoute()) {
178
            $settings = $this->module->getSettings('dev');
179
            if (!empty($settings['status'])) {
180
                $controller->setJsSettings('dev', array('key' => $settings['key']));
181
                $controller->setJs(__DIR__ . '/js/common.js', array('position' => 'bottom'));
182
                $controller->setCss(__DIR__ . '/css/common.css');
183
            }
184
        }
185
    }
186
187
    /**
188
     * Adds toolbar
189
     * @param string $html
190
     * @param \gplcart\core\Controller $controller
191
     */
192
    protected function setDevToolbar(&$html, $controller)
193
    {
194
        if (!$controller->isInternalRoute()) {
195
            $settings = $this->module->getSettings('dev');
196
            if (!empty($settings['status'])) {
197
198
                $data = array(
199
                    'key' => $settings['key'],
200
                    'time' => microtime(true) - GC_START,
201
                    'queries' => $this->db->getExecutedQueries()
202
                );
203
204
                $toolbar = $controller->render('dev|toolbar', $data);
205
                $html = substr_replace($html, $toolbar, strpos($html, '</body>'), 0);
206
            }
207
        }
208
    }
209
210
    /**
211
     * Configure system logger
212
     */
213
    protected function setLogger()
214
    {
215
        $settings = $this->module->getSettings('dev');
216
217
        $this->logger->printError(!empty($settings['print_error']))
218
                ->errorToException(!empty($settings['error_to_exception']))
219
                ->printBacktrace(!empty($settings['print_error_backtrace']));
220
    }
221
222
}
223