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