Completed
Pull Request — master (#60)
by Robbie
01:36
created

SilverStripeCollector::getRequirementsData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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