Completed
Push — master ( 14267d...603af5 )
by Iurii
01:37
created

Dev::hookDestructController()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.2
c 0
b 0
f 0
cc 4
eloc 4
nc 4
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\Library;
14
15
/**
16
 * Main class for Dev module
17
 */
18
class Dev extends Module
19
{
20
21
    /**
22
     * Library class instance
23
     * @var \gplcart\core\Library
24
     */
25
    protected $library;
26
27
    /**
28
     * @param Library $library
29
     */
30
    public function __construct(Library $library)
31
    {
32
        parent::__construct();
33
34
        $this->library = $library;
35
    }
36
37
    /**
38
     * Implements hook "construct"
39
     */
40
    public function hookConstruct()
41
    {
42
        require __DIR__ . '/vendor/kint-php/kint/init.php';
43
    }
44
45
    /**
46
     * Implements hook "construct.controller"
47
     * @param \gplcart\core\Controller $object
48
     */
49
    public function hookConstructController($object)
50
    {
51
        $settings = $this->config->module('dev');
52
        if (!empty($settings['status']) && !empty($settings['key']) && !$object->isBackend()) {
53
            $object->setJsSettings('dev', array('key' => $settings['key']));
54
        }
55
    }
56
57
    /**
58
     * Implements hook "destruct.controller"
59
     * @param \gplcart\core\Controller $controller
60
     */
61
    public function hookDestructController($controller)
62
    {
63
        $disabled = $controller->isPosted() || $controller->isAjax();
64
65
        if (!$disabled && $this->config->module('dev', 'status')) {
66
            $this->outputToolbar($controller);
67
        }
68
    }
69
70
    /**
71
     * Render and print dev toolbar
72
     * @param \gplcart\core\Controller $controller
73
     */
74
    protected function outputToolbar($controller)
75
    {
76
        /* @var $db \gplcart\core\Database */
77
        $db = $this->config->getDb();
78
79
        $data = array(
80
            'queries' => $db->getLogs(),
81
            'time' => microtime(true) - GC_START,
82
            'key' => $this->config->module('dev', 'key')
83
        );
84
85
        echo $controller->render('dev|toolbar', $data);
86
    }
87
88
    /**
89
     * Implements hook "library.list"
90
     * @param array $libraries
91
     */
92
    public function hookLibraryList(array &$libraries)
93
    {
94
        $libraries['kint'] = array(
95
            'name' => 'Kint',
96
            'description' => 'A powerful and modern PHP debugging tool',
97
            'url' => 'https://github.com/raveren/kint',
98
            'download' => 'https://github.com/kint-php/kint/archive/2.0-alpha4.zip',
99
            'type' => 'php',
100
            'version' => '2.0-alpha4',
101
            'module' => 'dev',
102
            'files' => array(
103
                'vendor/kint-php/kint/init.php'
104
            )
105
        );
106
    }
107
108
    /**
109
     * Implements hook "route.list"
110
     * @param array $routes
111
     */
112
    public function hookRouteList(array &$routes)
113
    {
114
        $routes['admin/module/settings/dev'] = array(
115
            'access' => '__superadmin',
116
            'handlers' => array(
117
                'controller' => array('gplcart\\modules\\dev\\controllers\\Settings', 'editSettings')
118
            )
119
        );
120
    }
121
122
    /**
123
     * Implements hook "module.enable.after"
124
     */
125
    public function hookModuleEnableAfter()
126
    {
127
        $this->library->clearCache();
128
    }
129
130
    /**
131
     * Implements hook "module.disable.after"
132
     */
133
    public function hookModuleDisableAfter()
134
    {
135
        $this->library->clearCache();
136
    }
137
138
    /**
139
     * Implements hook "module.install.after"
140
     */
141
    public function hookModuleInstallAfter()
142
    {
143
        $this->library->clearCache();
144
    }
145
146
    /**
147
     * Implements hook "module.uninstall.after"
148
     */
149
    public function hookModuleUninstallAfter()
150
    {
151
        $this->library->clearCache();
152
    }
153
154
}
155