Completed
Push — master ( c021d2...10d62d )
by Iurii
02:02
created

Dev::info()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
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\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
        // Load KINT library asap
43
        require __DIR__ . '/vendor/kint-php/kint/init.php';
44
    }
45
46
    /**
47
     * Implements hook "construct.controller"
48
     * @param \gplcart\core\Controller $object
49
     */
50
    public function hookConstructController($object)
51
    {
52
        $settings = $this->config->module('dev');
53
        if (!empty($settings['status']) && !empty($settings['key']) && !$object->isBackend()) {
54
            $object->setJsSettings('dev', array('key' => $settings['key']));
55
        }
56
    }
57
58
    /**
59
     * Implements hook "destruct"
60
     */
61
    public function hookDestruct()
0 ignored issues
show
Coding Style introduced by
hookDestruct uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
hookDestruct uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
62
    {
63
        $disabled = !empty($_POST)//
64
                || (isset($_SERVER['HTTP_X_REQUESTED_WITH'])//
65
                && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest');
66
67
        if (!$disabled && $this->config->module('dev', 'status')) {
68
            $this->outputToolbar();
69
        }
70
    }
71
72
    /**
73
     * Render and print dev toolbar
74
     */
75
    protected function outputToolbar()
76
    {
77
        /* @var $db \gplcart\core\Database */
78
        $db = $this->config->getDb();
79
80
        $queries = $db->getLogs();
81
        $time = microtime(true) - GC_START;
82
        $key = $this->config->module('dev', 'key');
83
84
        ob_start();
85
        include __DIR__ . '/templates/toolbar.php';
86
        echo ob_get_clean();
87
    }
88
89
    /**
90
     * Implements hook "library.list"
91
     * @param array $libraries
92
     */
93
    public function hookLibraryList(array &$libraries)
94
    {
95
        $libraries['kint'] = array(
96
            'name' => 'Kint',
97
            'description' => 'A powerful and modern PHP debugging tool',
98
            'url' => 'https://github.com/raveren/kint',
99
            'download' => 'https://github.com/kint-php/kint/archive/2.0-alpha4.zip',
100
            'type' => 'php',
101
            'version' => '2.0-alpha4',
102
            'module' => 'dev',
103
            'files' => array(
104
                'vendor/kint-php/kint/init.php'
105
            )
106
        );
107
    }
108
109
    /**
110
     * Implements hook "route.list"
111
     * @param array $routes
112
     */
113
    public function hookRouteList(array &$routes)
114
    {
115
        $routes['admin/module/settings/dev'] = array(
116
            'access' => 'module_edit',
117
            'handlers' => array(
118
                'controller' => array('gplcart\\modules\\dev\\controllers\\Settings', 'editSettings')
119
            )
120
        );
121
    }
122
123
    /**
124
     * Implements hook "module.enable.after"
125
     */
126
    public function hookModuleEnableAfter()
127
    {
128
        $this->library->clearCache();
129
    }
130
131
    /**
132
     * Implements hook "module.disable.after"
133
     */
134
    public function hookModuleDisableAfter()
135
    {
136
        $this->library->clearCache();
137
    }
138
139
    /**
140
     * Implements hook "module.install.after"
141
     */
142
    public function hookModuleInstallAfter()
143
    {
144
        $this->library->clearCache();
145
    }
146
147
    /**
148
     * Implements hook "module.uninstall.after"
149
     */
150
    public function hookModuleUninstallAfter()
151
    {
152
        $this->library->clearCache();
153
    }
154
155
}
156