Completed
Push — master ( 922165...738eef )
by Robbie
01:38
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\TemplateParserProxy;
10
use SilverStripe\Admin\LeftAndMain;
11
use SilverStripe\Control\Controller;
12
use SilverStripe\Control\Cookie;
13
use SilverStripe\Control\Session;
14
use SilverStripe\Core\Convert;
15
use SilverStripe\i18n\i18n;
16
use SilverStripe\Security\Member;
17
use SilverStripe\SiteConfig\SiteConfig;
18
use SilverStripe\View\Requirements;
19
20
class SilverStripeCollector extends DataCollector implements Renderable, AssetProvider
21
{
22
    protected static $debug = [];
23
    protected static $controller;
24
25
    public function collect()
26
    {
27
        $data = array(
28
            'debugcount' => count(self::$debug),
29
            'debug' => self::$debug,
30
            'session' => self::getSessionData(),
31
            'config' => self::getConfigData(),
32
            'locale' => i18n::get_locale(),
33
            'version' => LeftAndMain::create()->CMSVersion(),
34
            'cookies' => self::getCookieData(),
35
            'parameters' => self::getRequestParameters(),
36
            'requirements' => self::getRequirementsData(),
37
            '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...
38
            'templates' => self::getTemplateData(),
39
        );
40
        return $data;
41
    }
42
43
    /**
44
     * Returns the names of all the templates rendered.
45
     *
46
     * @return array
47
     */
48
    public static function getTemplateData()
49
    {
50
        if (TemplateParserProxy::getCached()) {
51
            return array(
52
                'templates' => array(
53
                    'NOTE: Rendered templates will not display when cached, please flush to view the list.'
54
                ),
55
                'count' => ''
56
            );
57
        }
58
59
        $templates = TemplateParserProxy::getTemplatesUsed();
60
        return array(
61
            'templates' => $templates,
62
            'count' => count($templates)
63
        );
64
    }
65
66
    public static function getRequirementsData()
67
    {
68
        $backend = Requirements::backend();
69
70
        $requirements = array_merge(
71
            $backend->getCSS(),
72
            $backend->getJavascript()
73
        );
74
75
        $output = [];
76
        foreach ($requirements as $asset => $specs) {
77
            $output[] = $asset . ': ' . Convert::raw2json($specs);
78
        }
79
        return $output;
80
    }
81
82
    public static function getRequestParameters()
83
    {
84
        if (!self::$controller) {
85
            return [];
86
        }
87
        $request = self::$controller->getRequest();
88
89
        $p = [];
90
        foreach ($request->getVars() as $k => $v) {
91
            $p["GET - $k"] = $v;
92
        }
93
        foreach ($request->postVars() as $k => $v) {
94
            $p["POST - $k"] = $v;
95
        }
96
        foreach ($request->params() as $k => $v) {
97
            $p["ROUTE - $k"] = $v;
98
        }
99
        return $p;
100
    }
101
102
    public static function getCookieData()
103
    {
104
        return Cookie::get_all();
105
    }
106
107
    public static function getSessionData()
108
    {
109
        $data = DebugBar::getRequest()->getSession()->getAll();
110
        $filtered = [];
111
112
        // Filter not useful data
113
        foreach ($data as $k => $v) {
114
            if (strpos($k, 'gf_') === 0) {
115
                continue;
116
            }
117
            if ($k === 'PHPDEBUGBAR_STACK_DATA') {
118
                continue;
119
            }
120
            if (is_array($v)) {
121
                $v = json_encode($v, JSON_PRETTY_PRINT);
122
            }
123
            $filtered[$k] = $v;
124
        }
125
        return $filtered;
126
    }
127
128
    public static function getConfigData()
129
    {
130
        return SiteConfig::current_site_config()->toMap();
131
    }
132
133
    /**
134
     * This method will try to matches all messages into a proper array
135
     *
136
     * @param string $data
137
     */
138
    public static function setDebugData($data)
139
    {
140
        $matches = null;
141
142
        preg_match_all("/<p class=\"message warning\">\n(.*?)<\/p>/s", $data, $matches);
143
144
        if (!empty($matches[1])) {
145
            self::$debug = $matches[1];
146
        }
147
    }
148
149
    /**
150
     * @param Controller $controller
151
     * @return $this
152
     */
153
    public function setController($controller)
154
    {
155
        self::$controller = $controller;
156
        return $this;
157
    }
158
159
    /**
160
     * @return Controller
161
     */
162
    public function getController()
163
    {
164
        return self::$controller;
165
    }
166
167
    public function getName()
168
    {
169
        return 'silverstripe';
170
    }
171
172
    public function getWidgets()
173
    {
174
        $name = $this->getName();
175
176
        $userIcon = 'user-times';
177
        $userText = 'Not logged in';
178
        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...
179
            $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...
180
            $memberTag = $member->getTitle() . ' (#' . $member->ID . ')';
181
182
            $userIcon = 'user';
183
            $userText = 'Logged in as ' . $memberTag;
184
185
            // Masquerade integration
186
            if (DebugBar::getRequest()->getSession()->get('Masquerade.Old.loggedInAs')) {
187
                $userIcon = 'user-secret';
188
                $userText = 'Masquerading as member ' . $memberTag;
189
            }
190
        }
191
192
        $widgets = array(
193
            'user' => array(
194
                'icon' => $userIcon,
195
                'tooltip' => $userText,
196
                "default" => "",
197
            ),
198
            "version" => array(
199
                "icon" => "desktop",
200
                "tooltip" => LeftAndMain::create()->CMSVersion(),
201
                "default" => ""
202
            ),
203
            "locale" => array(
204
                "icon" => "globe",
205
                "tooltip" => i18n::get_locale(),
206
                "default" => "",
207
            ),
208
            "session" => array(
209
                "icon" => "archive",
210
                "widget" => "PhpDebugBar.Widgets.VariableListWidget",
211
                "map" => "$name.session",
212
                "default" => "{}"
213
            ),
214
            "cookies" => array(
215
                "icon" => "asterisk",
216
                "widget" => "PhpDebugBar.Widgets.VariableListWidget",
217
                "map" => "$name.cookies",
218
                "default" => "{}"
219
            ),
220
            "parameters" => array(
221
                "icon" => "arrow-right",
222
                "widget" => "PhpDebugBar.Widgets.VariableListWidget",
223
                "map" => "$name.parameters",
224
                "default" => "{}"
225
            ),
226
            "config" => array(
227
                "icon" => "gear",
228
                "widget" => "PhpDebugBar.Widgets.VariableListWidget",
229
                "map" => "$name.config",
230
                "default" => "{}"
231
            ),
232
            "requirements" => array(
233
                "icon" => "file-o ",
234
                "widget" => "PhpDebugBar.Widgets.ListWidget",
235
                "map" => "$name.requirements",
236
                "default" => "{}"
237
            ),
238
            'templates' => array(
239
                'icon' => 'edit',
240
                'widget' => 'PhpDebugBar.Widgets.ListWidget',
241
                'map' => "$name.templates.templates",
242
                'default' => '{}'
243
            ),
244
        );
245
246
        // Add badge for number of templates if there are some
247
        if (!TemplateParserProxy::getCached()) {
248
            $widgets['templates:badge'] = array(
249
                'map' => "$name.templates.count",
250
                'default' => 0
251
            );
252
        }
253
254
        if (!empty(self::$debug)) {
255
            $widgets["debug"] = array(
256
                "icon" => "list-alt",
257
                "widget" => "PhpDebugBar.Widgets.ListWidget",
258
                "map" => "$name.debug",
259
                "default" => "[]"
260
            );
261
            $widgets["debug:badge"] = array(
262
                "map" => "$name.debugcount",
263
                "default" => "null"
264
            );
265
        }
266
267
        return $widgets;
268
    }
269
270
    /**
271
     * @return array
272
     */
273 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...
274
    {
275
        return array(
276
            'base_path' => '/' . DEBUGBAR_DIR . '/javascript',
277
            'base_url' => DEBUGBAR_DIR . '/javascript',
278
            'css' => [],
279
            'js' => 'widgets.js',
280
        );
281
    }
282
}
283