Completed
Push — master ( aa08df...50a885 )
by Iurii
01:04
created

Module::setLogger()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
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\Logger,
13
    gplcart\core\Config,
14
    gplcart\core\Library,
15
    gplcart\core\Module as CoreModule;
16
use gplcart\core\exceptions\Dependency as DependencyException;
17
18
/**
19
 * Main class for Dev module
20
 */
21
class Module
22
{
23
24
    /**
25
     * Config class instance
26
     * @var \gplcart\core\Config
27
     */
28
    protected $config;
29
30
    /**
31
     * Database class instance
32
     * @var \gplcart\core\Database $db
33
     */
34
    protected $db;
35
36
    /**
37
     * Module class instance
38
     * @var \gplcart\core\Module $module
39
     */
40
    protected $module;
41
42
    /**
43
     * Library class instance
44
     * @var \gplcart\core\Library $library
45
     */
46
    protected $library;
47
48
    /**
49
     * Logger class instance
50
     * @var \gplcart\core\Logger $logger
51
     */
52
    protected $logger;
53
54
    /**
55
     * @param Logger $logger
56
     * @param Config $config
57
     * @param Library $library
58
     * @param CoreModule $module
59
     */
60
    public function __construct(Logger $logger, Config $config, Library $library, CoreModule $module)
61
    {
62
        $this->module = $module;
63
        $this->logger = $logger;
64
        $this->config = $config;
65
        $this->library = $library;
66
        $this->db = $this->config->getDb();
67
    }
68
69
    /**
70
     * Implements hook "construct"
71
     */
72
    public function hookConstruct()
73
    {
74
        $this->setLogger();
75
        require_once $this->getKintFile();
76
    }
77
78
    /**
79
     * Implements hook "construct.controller"
80
     * @param \gplcart\core\Controller $controller
81
     */
82
    public function hookConstructController($controller)
83
    {
84
        $this->setModuleAssets($controller);
85
    }
86
87
    /**
88
     * Implements hook "template.output"
89
     * @param string $html
90
     * @param \gplcart\core\Controller $controller
91
     */
92
    public function hookTemplateOutput(&$html, $controller)
93
    {
94
        $this->setDevToolbar($html, $controller);
95
    }
96
97
    /**
98
     * Implements hook "library.list"
99
     * @param array $libraries
100
     */
101
    public function hookLibraryList(array &$libraries)
102
    {
103
        $libraries['kint'] = array(
104
            'name' => 'Kint',
105
            'description' => 'A powerful and modern PHP debugging tool',
106
            'url' => 'https://github.com/raveren/kint',
107
            'download' => 'https://github.com/kint-php/kint/archive/2.0-alpha4.zip',
108
            'type' => 'php',
109
            'version' => '2.0-alpha4',
110
            'module' => 'dev',
111
            'files' => array(
112
                'vendor/kint-php/kint/init.php'
113
            )
114
        );
115
    }
116
117
    /**
118
     * Implements hook "route.list"
119
     * @param array $routes
120
     */
121
    public function hookRouteList(array &$routes)
122
    {
123
        $routes['admin/module/settings/dev'] = array(
124
            'access' => '__superadmin',
125
            'handlers' => array(
126
                'controller' => array('gplcart\\modules\\dev\\controllers\\Settings', 'editSettings')
127
            )
128
        );
129
    }
130
131
    /**
132
     * Implements hook "module.enable.after"
133
     */
134
    public function hookModuleEnableAfter()
135
    {
136
        $this->library->clearCache();
137
    }
138
139
    /**
140
     * Implements hook "module.disable.after"
141
     */
142
    public function hookModuleDisableAfter()
143
    {
144
        $this->library->clearCache();
145
    }
146
147
    /**
148
     * Implements hook "module.install.after"
149
     */
150
    public function hookModuleInstallAfter()
151
    {
152
        $this->library->clearCache();
153
    }
154
155
    /**
156
     * Implements hook "module.uninstall.after"
157
     */
158
    public function hookModuleUninstallAfter()
159
    {
160
        $this->library->clearCache();
161
    }
162
163
    /**
164
     * Returns a path to Kint's init file
165
     * @return string
166
     */
167
    public function getKintFile()
168
    {
169
        $file = __DIR__ . '/vendor/kint-php/kint/init.php';
170
171
        if (is_file($file)) {
172
            return $file;
173
        }
174
175
        throw new DependencyException("Kint file $file not found");
176
    }
177
178
    /**
179
     * Sets module specific assets
180
     * @param \gplcart\core\Controller $controller
181
     */
182
    protected function setModuleAssets($controller)
183
    {
184
        if (!$controller->isInternalRoute()) {
185
            $settings = $this->module->getSettings('dev');
186
            if (!empty($settings['status'])) {
187
                $controller->setJsSettings('dev', array('key' => $settings['key']));
188
                $controller->setJs(__DIR__ . '/js/common.js', array('position' => 'bottom'));
189
                $controller->setCss(__DIR__ . '/css/common.css');
190
            }
191
        }
192
    }
193
194
    /**
195
     * Adds toolbar
196
     * @param string $html
197
     * @param \gplcart\core\Controller $controller
198
     */
199
    protected function setDevToolbar(&$html, $controller)
200
    {
201
        if (!$controller->isInternalRoute()) {
202
            $settings = $this->module->getSettings('dev');
203
            if (!empty($settings['status'])) {
204
205
                $data = array(
206
                    'key' => $settings['key'],
207
                    'time' => microtime(true) - GC_START,
208
                    'queries' => $this->db->getLogs()
209
                );
210
211
                $toolbar = $controller->render('dev|toolbar', $data);
212
                $html = substr_replace($html, $toolbar, strpos($html, '</body>'), 0);
213
            }
214
        }
215
    }
216
217
    /**
218
     * Configure system logger
219
     */
220
    protected function setLogger()
221
    {
222
        $settings = $this->module->getSettings('dev');
223
224
        $this->logger->printError(!empty($settings['print_error']))
225
                ->errorToException(!empty($settings['error_to_exception']))
226
                ->printBacktrace(!empty($settings['print_error_backtrace']));
227
    }
228
229
}
230