SilverStripeCollector   A
last analyzed

Complexity

Total Complexity 34

Size/Duplication

Total Lines 301
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 161
c 3
b 0
f 1
dl 0
loc 301
rs 9.68
wmc 34

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A getController() 0 3 1
A collect() 0 17 3
A setDebugData() 0 8 2
A getAssets() 0 7 1
A getRequestParameters() 0 18 5
B getWidgets() 0 109 6
A setController() 0 4 1
A getMiddlewares() 0 12 2
A getRequirementsData() 0 21 2
A getTemplateData() 0 6 1
A getSessionData() 0 24 6
A getConfigData() 0 6 2
A getCookieData() 0 3 1
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 SilverStripe\Admin\LeftAndMain;
11
use SilverStripe\Control\Controller;
12
use SilverStripe\Control\Cookie;
13
use SilverStripe\Control\Director;
14
use SilverStripe\i18n\i18n;
15
use SilverStripe\Security\Security;
16
use SilverStripe\SiteConfig\SiteConfig;
17
use SilverStripe\View\Requirements;
18
19
class SilverStripeCollector extends DataCollector implements Renderable, AssetProvider
20
{
21
    protected static $debug = [];
22
    protected static $controller;
23
24
    public function collect()
25
    {
26
        $data = array(
27
            'debugcount' => count(self::$debug),
28
            'debug' => self::$debug,
29
            'session' => self::getSessionData(),
30
            'config' => self::getConfigData(),
31
            'locale' => i18n::get_locale(),
32
            'version' => class_exists(LeftAndMain::class) ? LeftAndMain::create()->CMSVersion() : 'unknown',
33
            'cookies' => self::getCookieData(),
34
            'parameters' => self::getRequestParameters(),
35
            'requirements' => self::getRequirementsData(),
36
            'user' => Security::getCurrentUser() ? Security::getCurrentUser()->Title : 'Not logged in',
37
            'templates' => self::getTemplateData(),
38
            'middlewares' => self::getMiddlewares(),
39
        );
40
        return $data;
41
    }
42
43
    /**
44
     * Get all middlewares executed on this request
45
     *
46
     * @return array
47
     */
48
    public static function getMiddlewares()
49
    {
50
        $middlewares = Director::singleton()->getMiddlewares();
51
        if (!$middlewares) {
52
            return [
53
                'list' => array(),
54
                'count' => 0,
55
            ];
56
        }
57
        return array(
58
            'list' => array_keys($middlewares),
59
            'count' => count($middlewares)
60
        );
61
    }
62
63
    /**
64
     * Returns the names of all the templates rendered.
65
     *
66
     * @return array
67
     */
68
    public static function getTemplateData()
69
    {
70
        $templates = SSViewerProxy::getTemplatesUsed();
71
        return array(
72
            'templates' => $templates,
73
            'count' => count($templates)
74
        );
75
    }
76
77
    public static function getRequirementsData()
78
    {
79
        $backend = Requirements::backend();
80
81
        $requirements = array_merge(
82
            $backend->getCSS(),
83
            $backend->getJavascript()
84
        );
85
86
        $output = [];
87
        foreach ($requirements as $asset => $specs) {
88
            // Get the filename only
89
            $fileNames = explode('/', $asset);
90
            $fileName = end($fileNames);
91
            $specs['href'] = $asset;
92
            $output[$fileName] = json_encode($specs, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
93
        }
94
95
        return [
96
            'list' => $output,
97
            'count' => count($requirements)
98
        ];
99
    }
100
101
    public static function getRequestParameters()
102
    {
103
        if (!self::$controller) {
104
            return [];
105
        }
106
        $request = self::$controller->getRequest();
107
108
        $p = [];
109
        foreach ($request->getVars() as $k => $v) {
110
            $p["GET - $k"] = $v;
111
        }
112
        foreach ($request->postVars() as $k => $v) {
113
            $p["POST - $k"] = $v;
114
        }
115
        foreach ($request->params() as $k => $v) {
116
            $p["ROUTE - $k"] = $v;
117
        }
118
        return $p;
119
    }
120
121
    public static function getCookieData()
122
    {
123
        return Cookie::get_all();
124
    }
125
126
    public static function getSessionData()
127
    {
128
        $data = DebugBar::getRequest()->getSession()->getAll();
129
130
        if (empty($data)) {
131
            return [];
132
        }
133
134
        $filtered = [];
135
136
        // Filter not useful data
137
        foreach ($data as $k => $v) {
138
            if (strpos($k, 'gf_') === 0) {
139
                continue;
140
            }
141
            if ($k === 'PHPDEBUGBAR_STACK_DATA') {
142
                continue;
143
            }
144
            if (is_array($v)) {
145
                $v = json_encode($v, JSON_PRETTY_PRINT);
146
            }
147
            $filtered[$k] = $v;
148
        }
149
        return $filtered;
150
    }
151
152
    public static function getConfigData()
153
    {
154
        if (!class_exists(SiteConfig::class)) {
155
            return [];
156
        }
157
        return SiteConfig::current_site_config()->toMap();
158
    }
159
160
    /**
161
     * This method will try to matches all messages into a proper array
162
     *
163
     * @param string $data
164
     */
165
    public static function setDebugData($data)
166
    {
167
        $matches = null;
168
169
        preg_match_all("/<p class=\"message warning\">\n(.*?)<\/p>/s", $data, $matches);
170
171
        if (!empty($matches[1])) {
172
            self::$debug = $matches[1];
173
        }
174
    }
175
176
    /**
177
     * @param Controller $controller
178
     * @return $this
179
     */
180
    public function setController($controller)
181
    {
182
        self::$controller = $controller;
183
        return $this;
184
    }
185
186
    /**
187
     * @return Controller
188
     */
189
    public function getController()
190
    {
191
        return self::$controller;
192
    }
193
194
    public function getName()
195
    {
196
        return 'silverstripe';
197
    }
198
199
    public function getWidgets()
200
    {
201
        $name = $this->getName();
202
203
        $userIcon = 'user-times';
204
        $userText = 'Not logged in';
205
        if ($member = Security::getCurrentUser()) {
206
            $memberTag = $member->getTitle() . ' (#' . $member->ID . ')';
207
208
            $userIcon = 'user';
209
            $userText = 'Logged in as ' . $memberTag;
210
211
            // TODO: upgrade to newer version of the module
212
            // Masquerade integration
213
            if (DebugBar::getRequest()->getSession()->get('Masquerade.Old.loggedInAs')) {
214
                $userIcon = 'user-secret';
215
                $userText = 'Masquerading as member ' . $memberTag;
216
            }
217
        }
218
219
        $widgets = array(
220
            'user' => array(
221
                'icon' => $userIcon,
222
                'tooltip' => $userText,
223
                "default" => "",
224
            ),
225
            "version" => array(
226
                "icon" => "hashtag",
227
                "tooltip" => class_exists(LeftAndMain::class) ? LeftAndMain::create()->CMSVersion() : 'unknown',
228
                "default" => ""
229
            ),
230
            "locale" => array(
231
                "icon" => "globe",
232
                "tooltip" => i18n::get_locale(),
233
                "default" => "",
234
            ),
235
            "session" => array(
236
                "icon" => "archive",
237
                "widget" => "PhpDebugBar.Widgets.VariableListWidget",
238
                "map" => "$name.session",
239
                "default" => "{}"
240
            ),
241
            "cookies" => array(
242
                "icon" => "asterisk",
243
                "widget" => "PhpDebugBar.Widgets.VariableListWidget",
244
                "map" => "$name.cookies",
245
                "default" => "{}"
246
            ),
247
            "parameters" => array(
248
                "icon" => "arrow-right",
249
                "widget" => "PhpDebugBar.Widgets.VariableListWidget",
250
                "map" => "$name.parameters",
251
                "default" => "{}"
252
            ),
253
            "requirements" => array(
254
                "icon"    => "file-text-o",
255
                "widget"  => "PhpDebugBar.Widgets.VariableListWidget",
256
                "map"     => "$name.requirements.list",
257
                "default" => "{}"
258
            ),
259
            "requirements:badge" => array(
260
                "map"     => "$name.requirements.count",
261
                "default" => 0
262
            ),
263
            "middlewares" => array(
264
                "icon" => "file-text-o",
265
                "widget" => "PhpDebugBar.Widgets.ListWidget",
266
                "map" => "$name.middlewares.list",
267
                "default" => "{}"
268
            ),
269
            "middlewares:badge" => array(
270
                "map" => "$name.middlewares.count",
271
                "default" => 0
272
            ),
273
            'templates' => array(
274
                'icon' => 'file-code-o',
275
                'widget' => 'PhpDebugBar.Widgets.ListWidget',
276
                'map' => "$name.templates.templates",
277
                'default' => '{}'
278
            ),
279
            'templates:badge' => array(
280
                'map' => "$name.templates.count",
281
                'default' => 0
282
            )
283
        );
284
285
        if (!empty($this->getConfigData())) {
286
            $widgets["SiteConfig"] = array(
287
                "icon" => "sliders",
288
                "widget" => "PhpDebugBar.Widgets.VariableListWidget",
289
                "map" => "$name.config",
290
                "default" => "{}"
291
            );
292
        }
293
294
        if (!empty(self::$debug)) {
295
            $widgets["debug"] = array(
296
                "icon" => "list-alt",
297
                "widget" => "PhpDebugBar.Widgets.ListWidget",
298
                "map" => "$name.debug",
299
                "default" => "[]"
300
            );
301
            $widgets["debug:badge"] = array(
302
                "map" => "$name.debugcount",
303
                "default" => "null"
304
            );
305
        }
306
307
        return $widgets;
308
    }
309
310
    /**
311
     * @return array
312
     */
313
    public function getAssets()
314
    {
315
        return array(
316
            'base_path' => '/' . DebugBar::moduleResource('javascript')->getRelativePath(),
317
            'base_url' => Director::makeRelative(DebugBar::moduleResource('javascript')->getURL()),
318
            'css' => [],
319
            'js' => 'widgets.js',
320
        );
321
    }
322
}
323