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

Dev.php (2 issues)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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"
59
     */
60
    public function hookDestruct()
0 ignored issues
show
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...
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...
61
    {
62
        $disabled = !empty($_POST)//
63
                || (isset($_SERVER['HTTP_X_REQUESTED_WITH'])//
64
                && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest');
65
66
        if (!$disabled && $this->config->module('dev', 'status')) {
67
            $this->outputToolbar();
68
        }
69
    }
70
71
    /**
72
     * Render and print dev toolbar
73
     */
74
    protected function outputToolbar()
75
    {
76
        /* @var $db \gplcart\core\Database */
77
        $db = $this->config->getDb();
78
79
        $queries = $db->getLogs();
80
        $time = microtime(true) - GC_START;
81
        $key = $this->config->module('dev', 'key');
82
83
        ob_start();
84
        include __DIR__ . '/templates/toolbar.php';
85
        echo ob_get_clean();
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