Completed
Pull Request — master (#78)
by Franco
01:34
created

SilverStripeCollector::getRequestParameters()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 12
nc 9
nop 0
1
<?php
2
3
namespace LeKoala\DebugBar\Collector;
4
5
use DebugBar\DataCollector\AssetProvider;
6
use DebugBar\DataCollector\DataCollector;
7
use DebugBar\DataCollector\Renderable;
8
use LeKoala\DebugBar\DebugBar;
9
use LeKoala\DebugBar\Proxy\SSViewerProxy;
10
use Psr\SimpleCache\CacheInterface;
11
use SilverStripe\Admin\LeftAndMain;
12
use SilverStripe\Control\Controller;
13
use SilverStripe\Control\Cookie;
14
use SilverStripe\Control\Session;
15
use SilverStripe\Core\Convert;
16
use SilverStripe\Core\Injector\Injector;
17
use SilverStripe\i18n\i18n;
18
use SilverStripe\Security\Member;
19
use SilverStripe\SiteConfig\SiteConfig;
20
use SilverStripe\View\Requirements;
21
22
class SilverStripeCollector extends DataCollector implements Renderable, AssetProvider
23
{
24
    protected static $debug = [];
25
    protected static $controller;
26
    protected static $template_cache_info = [];
27
28
    public function collect()
29
    {
30
        $data = array(
31
            'debugcount' => count(self::$debug),
32
            'debug' => self::$debug,
33
            'session' => self::getSessionData(),
34
            'config' => self::getConfigData(),
35
            'locale' => i18n::get_locale(),
36
            'version' => LeftAndMain::create()->CMSVersion(),
37
            'cookies' => self::getCookieData(),
38
            'parameters' => self::getRequestParameters(),
39
            'requirements' => self::getRequirementsData(),
40
            'user' => Member::currentUserID() ? Member::currentUser()->Title : 'Not logged in',
0 ignored issues
show
Deprecated Code introduced by
The method SilverStripe\Security\Member::currentUserID() has been deprecated with message: 5.0.0 use Security::getCurrentUser()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
Deprecated Code introduced by
The method SilverStripe\Security\Member::currentUser() has been deprecated with message: 5.0.0 use Security::getCurrentUser()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
41
            'templates' => self::getTemplateData(),
42
            'partialCache' => self::getPartialCacheInfo()
43
        );
44
        return $data;
45
    }
46
47
    /**
48
     * Returns the names of all the templates rendered.
49
     *
50
     * @return array
51
     */
52
    public static function getTemplateData()
53
    {
54
        $templates = SSViewerProxy::getTemplatesUsed();
55
        return array(
56
            'templates' => $templates,
57
            'count' => count($templates)
58
        );
59
    }
60
61
    public static function getRequirementsData()
62
    {
63
        $backend = Requirements::backend();
64
65
        $requirements = array_merge(
66
            $backend->getCSS(),
67
            $backend->getJavascript()
68
        );
69
70
        $output = [];
71
        foreach ($requirements as $asset => $specs) {
72
            $output[] = $asset . ': ' . Convert::raw2json($specs);
73
        }
74
        return $output;
75
    }
76
77
    public static function getRequestParameters()
78
    {
79
        if (!self::$controller) {
80
            return [];
81
        }
82
        $request = self::$controller->getRequest();
83
84
        $p = [];
85
        foreach ($request->getVars() as $k => $v) {
86
            $p["GET - $k"] = $v;
87
        }
88
        foreach ($request->postVars() as $k => $v) {
89
            $p["POST - $k"] = $v;
90
        }
91
        foreach ($request->params() as $k => $v) {
92
            $p["ROUTE - $k"] = $v;
93
        }
94
        return $p;
95
    }
96
97
    public static function getCookieData()
98
    {
99
        return Cookie::get_all();
100
    }
101
102
    public static function getSessionData()
103
    {
104
        $data = DebugBar::getRequest()->getSession()->getAll();
105
        $filtered = [];
106
107
        // Filter not useful data
108
        foreach ($data as $k => $v) {
109
            if (strpos($k, 'gf_') === 0) {
110
                continue;
111
            }
112
            if ($k === 'PHPDEBUGBAR_STACK_DATA') {
113
                continue;
114
            }
115
            if (is_array($v)) {
116
                $v = json_encode($v, JSON_PRETTY_PRINT);
117
            }
118
            $filtered[$k] = $v;
119
        }
120
        return $filtered;
121
    }
122
123
    public static function getConfigData()
124
    {
125
        return SiteConfig::current_site_config()->toMap();
126
    }
127
128
    /**
129
     * This method will try to matches all messages into a proper array
130
     *
131
     * @param string $data
132
     */
133
    public static function setDebugData($data)
134
    {
135
        $matches = null;
136
137
        preg_match_all("/<p class=\"message warning\">\n(.*?)<\/p>/s", $data, $matches);
138
139
        if (!empty($matches[1])) {
140
            self::$debug = $matches[1];
141
        }
142
    }
143
144
    /**
145
     * @param Controller $controller
146
     * @return $this
147
     */
148
    public function setController($controller)
149
    {
150
        self::$controller = $controller;
151
        return $this;
152
    }
153
154
    /**
155
     * @return Controller
156
     */
157
    public function getController()
158
    {
159
        return self::$controller;
160
    }
161
162
    public function getName()
163
    {
164
        return 'silverstripe';
165
    }
166
167
    public function getWidgets()
168
    {
169
        $name = $this->getName();
170
171
        $userIcon = 'user-times';
172
        $userText = 'Not logged in';
173
        if (Member::currentUserID()) {
0 ignored issues
show
Deprecated Code introduced by
The method SilverStripe\Security\Member::currentUserID() has been deprecated with message: 5.0.0 use Security::getCurrentUser()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
174
            $member = Member::currentUser();
0 ignored issues
show
Deprecated Code introduced by
The method SilverStripe\Security\Member::currentUser() has been deprecated with message: 5.0.0 use Security::getCurrentUser()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
175
            $memberTag = $member->getTitle() . ' (#' . $member->ID . ')';
176
177
            $userIcon = 'user';
178
            $userText = 'Logged in as ' . $memberTag;
179
180
            // Masquerade integration
181
            if (DebugBar::getRequest()->getSession()->get('Masquerade.Old.loggedInAs')) {
182
                $userIcon = 'user-secret';
183
                $userText = 'Masquerading as member ' . $memberTag;
184
            }
185
        }
186
187
        $widgets = array(
188
            'user' => array(
189
                'icon' => $userIcon,
190
                'tooltip' => $userText,
191
                "default" => "",
192
            ),
193
            "version" => array(
194
                "icon" => "desktop",
195
                "tooltip" => LeftAndMain::create()->CMSVersion(),
196
                "default" => ""
197
            ),
198
            "locale" => array(
199
                "icon" => "globe",
200
                "tooltip" => i18n::get_locale(),
201
                "default" => "",
202
            ),
203
            "session" => array(
204
                "icon" => "archive",
205
                "widget" => "PhpDebugBar.Widgets.VariableListWidget",
206
                "map" => "$name.session",
207
                "default" => "{}"
208
            ),
209
            "cookies" => array(
210
                "icon" => "asterisk",
211
                "widget" => "PhpDebugBar.Widgets.VariableListWidget",
212
                "map" => "$name.cookies",
213
                "default" => "{}"
214
            ),
215
            "parameters" => array(
216
                "icon" => "arrow-right",
217
                "widget" => "PhpDebugBar.Widgets.VariableListWidget",
218
                "map" => "$name.parameters",
219
                "default" => "{}"
220
            ),
221
            "SiteConfig" => array(
222
                "icon" => "gear",
223
                "widget" => "PhpDebugBar.Widgets.VariableListWidget",
224
                "map" => "$name.config",
225
                "default" => "{}"
226
            ),
227
            "requirements" => array(
228
                "icon" => "file-o ",
229
                "widget" => "PhpDebugBar.Widgets.ListWidget",
230
                "map" => "$name.requirements",
231
                "default" => "{}"
232
            ),
233
            'templates' => array(
234
                'icon' => 'edit',
235
                'widget' => 'PhpDebugBar.Widgets.ListWidget',
236
                'map' => "$name.templates.templates",
237
                'default' => '{}'
238
            ),
239
            'templates:badge' => array(
240
                'map' => "$name.templates.count",
241
                'default' => 0
242
            ),
243
            'partialCache' => array(
244
                'icon' => 'asterisk',
245
                'widget' => 'PhpDebugBar.Widgets.ConfigWidget',
246
                'map' => "$name.partialCache.calls",
247
                'default' => '{}'
248
            ),
249
            'partialCache:badge' => array(
250
                'map' => "$name.partialCache.count",
251
                'default' => 0
252
            )
253
        );
254
255
        if (!empty(self::$debug)) {
256
            $widgets["debug"] = array(
257
                "icon" => "list-alt",
258
                "widget" => "PhpDebugBar.Widgets.ListWidget",
259
                "map" => "$name.debug",
260
                "default" => "[]"
261
            );
262
            $widgets["debug:badge"] = array(
263
                "map" => "$name.debugcount",
264
                "default" => "null"
265
            );
266
        }
267
268
        return $widgets;
269
    }
270
271
    /**
272
     * @return array
273
     */
274 View Code Duplication
    public function getAssets()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
275
    {
276
        return array(
277
            'base_path' => '/' . DEBUGBAR_DIR . '/javascript',
278
            'base_url' => DEBUGBAR_DIR . '/javascript',
279
            'css' => [],
280
            'js' => 'widgets.js',
281
        );
282
    }
283
284
    /**
285
     * Returns a list of partial cache hits and misses as well as the total items contained in the array
286
     * @return array
287
     */
288
    public static function getPartialCacheInfo()
289
    {
290
        $templates = (Injector::inst()->get(CacheInterface::class . '.backend')->templateCache) ?: array();
291
        return array(
292
            'count' => count($templates),
293
            'calls' => $templates,
294
        );
295
    }
296
}
297