Completed
Push — master ( b6284a...f27519 )
by Iurii
01:11
created

Dev::setModuleAssets()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
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\Config;
14
15
/**
16
 * Main class for Dev module
17
 */
18
class Dev extends Module
19
{
20
21
    /**
22
     * @param Config $config
23
     */
24
    public function __construct(Config $config)
25
    {
26
        parent::__construct($config);
27
    }
28
29
    /**
30
     * Implements hook "module.install.before"
31
     * @param null|string $result
32
     */
33
    public function hookModuleInstallBefore(&$result)
34
    {
35
        if (!is_file($this->getKintFile())) {
36
            $result = $this->getLanguage()->text('Kint file not found');
37
        }
38
    }
39
40
    /**
41
     * Implements hook "construct"
42
     */
43
    public function hookConstruct()
44
    {
45
        require_once $this->getKintFile();
46
    }
47
48
    /**
49
     * Implements hook "construct.controller"
50
     * @param \gplcart\core\Controller $controller
51
     */
52
    public function hookConstructController($controller)
53
    {
54
        $this->setModuleAssets($controller);
55
    }
56
57
    /**
58
     * Implements hook "template.output"
59
     * @param string $html
60
     * @param \gplcart\core\Controller $controller
61
     */
62
    public function hookTemplateOutput(&$html, $controller)
63
    {
64
        $this->setDevToolbar($html, $controller);
65
    }
66
67
    /**
68
     * Implements hook "library.list"
69
     * @param array $libraries
70
     */
71
    public function hookLibraryList(array &$libraries)
72
    {
73
        $libraries['kint'] = array(
74
            'name' => 'Kint',
75
            'description' => 'A powerful and modern PHP debugging tool',
76
            'url' => 'https://github.com/raveren/kint',
77
            'download' => 'https://github.com/kint-php/kint/archive/2.0-alpha4.zip',
78
            'type' => 'php',
79
            'version' => '2.0-alpha4',
80
            'module' => 'dev',
81
            'files' => array(
82
                'vendor/kint-php/kint/init.php'
83
            )
84
        );
85
    }
86
87
    /**
88
     * Implements hook "route.list"
89
     * @param array $routes
90
     */
91
    public function hookRouteList(array &$routes)
92
    {
93
        $routes['admin/module/settings/dev'] = array(
94
            'access' => '__superadmin',
95
            'handlers' => array(
96
                'controller' => array('gplcart\\modules\\dev\\controllers\\Settings', 'editSettings')
97
            )
98
        );
99
    }
100
101
    /**
102
     * Implements hook "module.enable.after"
103
     */
104
    public function hookModuleEnableAfter()
105
    {
106
        $this->getLibrary()->clearCache();
107
    }
108
109
    /**
110
     * Implements hook "module.disable.after"
111
     */
112
    public function hookModuleDisableAfter()
113
    {
114
        $this->getLibrary()->clearCache();
115
    }
116
117
    /**
118
     * Implements hook "module.install.after"
119
     */
120
    public function hookModuleInstallAfter()
121
    {
122
        $this->getLibrary()->clearCache();
123
    }
124
125
    /**
126
     * Implements hook "module.uninstall.after"
127
     */
128
    public function hookModuleUninstallAfter()
129
    {
130
        $this->getLibrary()->clearCache();
131
    }
132
133
    /**
134
     * Returns a path to Kint's init file
135
     * @return string
136
     */
137
    public function getKintFile()
138
    {
139
        return __DIR__ . '/vendor/kint-php/kint/init.php';
140
    }
141
142
    /**
143
     * Sets module specific assets
144
     * @param \gplcart\core\Controller $controller
145
     */
146
    protected function setModuleAssets($controller)
147
    {
148
        if (!$controller->isInternalRoute()) {
149
            $settings = $this->config->getFromModule('dev');
150
            if (!empty($settings['status'])) {
151
                $controller->setJsSettings('dev', array('key' => $settings['key']));
152
                $controller->setJs($this->getAsset('dev', 'common.js'), array('position' => 'bottom'));
153
                $controller->setCss($this->getAsset('dev', '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
            $settings = $this->config->getFromModule('dev');
167
            if (!empty($settings['status'])) {
168
169
                $data = array(
170
                    'key' => $settings['key'],
171
                    'time' => microtime(true) - GC_START,
172
                    'queries' => $this->config->getDb()->getLogs()
173
                );
174
175
                $toolbar = $controller->render('dev|toolbar', $data);
176
                $html = substr_replace($html, $toolbar, strpos($html, '</body>'), 0);
177
            }
178
        }
179
    }
180
181
}
182