1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
|
<?php /** * Whoops - php errors for cool kids * @author Filipe Dobreira <http://github.com/filp> */
namespace Whoops\Handler;
use InvalidArgumentException; use RuntimeException; use UnexpectedValueException; use Whoops\Exception\Formatter; use Whoops\Util\Misc; use Whoops\Util\TemplateHelper;
class PrettyPageHandler extends Handler { /** * Search paths to be scanned for resources, in the reverse * order they're declared. * * @var array */ private $searchPaths = array();
/** * Fast lookup cache for known resource locations. * * @var array */ private $resourceCache = array();
/** * The name of the custom css file. * * @var string */ private $customCss = null;
/** * @var array[] */ private $extraTables = array();
/** * @var bool */ private $handleUnconditionally = false;
/** * @var string */ private $pageTitle = "Whoops! There was an error.";
/** * A string identifier for a known IDE/text editor, or a closure * that resolves a string that can be used to open a given file * in an editor. If the string contains the special substrings * %file or %line, they will be replaced with the correct data. * * @example * "txmt://open?url=%file&line=%line" * @var mixed $editor */ protected $editor;
/** * A list of known editor strings * @var array */ protected $editors = array( "sublime" => "subl://open?url=file://%file&line=%line", "textmate" => "txmt://open?url=file://%file&line=%line", "emacs" => "emacs://open?url=file://%file&line=%line", "macvim" => "mvim://open/?url=file://%file&line=%line", "phpstorm" => "phpstorm://open?file=%file&line=%line", );
/** * Constructor. */ public function __construct() { if (ini_get('xdebug.file_link_format') || extension_loaded('xdebug')) { // Register editor using xdebug's file_link_format option. $this->editors['xdebug'] = function ($file, $line) { return str_replace(array('%f', '%l'), array($file, $line), ini_get('xdebug.file_link_format')); }; }
// Add the default, local resource search path: $this->searchPaths[] = __DIR__ . "/../Resources"; }
/** * @return int|null */ public function handle() { if (!$this->handleUnconditionally()) { // Check conditions for outputting HTML: // @todo: Make this more robust if (php_sapi_name() === 'cli') { // Help users who have been relying on an internal test value // fix their code to the proper method if (isset($_ENV['whoops-test'])) { throw new \Exception( 'Use handleUnconditionally instead of whoops-test' .' environment variable' ); }
return Handler::DONE; } }
// @todo: Make this more dynamic $helper = new TemplateHelper();
$templateFile = $this->getResource("views/layout.html.php"); $cssFile = $this->getResource("css/whoops.base.css"); $zeptoFile = $this->getResource("js/zepto.min.js"); $clipboard = $this->getResource("js/clipboard.min.js"); $jsFile = $this->getResource("js/whoops.base.js");
if ($this->customCss) { $customCssFile = $this->getResource($this->customCss); }
$inspector = $this->getInspector(); $frames = $inspector->getFrames();
$code = $inspector->getException()->getCode();
if ($inspector->getException() instanceof \ErrorException) { // ErrorExceptions wrap the php-error types within the "severity" property $code = Misc::translateErrorCode($inspector->getException()->getSeverity()); }
// List of variables that will be passed to the layout template. $vars = array( "page_title" => $this->getPageTitle(),
// @todo: Asset compiler "stylesheet" => file_get_contents($cssFile), "zepto" => file_get_contents($zeptoFile), "clipboard" => file_get_contents($clipboard), "javascript" => file_get_contents($jsFile),
// Template paths: "header" => $this->getResource("views/header.html.php"), "frame_list" => $this->getResource("views/frame_list.html.php"), "frame_code" => $this->getResource("views/frame_code.html.php"), "env_details" => $this->getResource("views/env_details.html.php"),
"title" => $this->getPageTitle(), "name" => explode("\\", $inspector->getExceptionName()), "message" => $inspector->getException()->getMessage(), "code" => $code, "plain_exception" => Formatter::formatExceptionPlain($inspector), "frames" => $frames, "has_frames" => !!count($frames), "handler" => $this, "handlers" => $this->getRun()->getHandlers(),
"tables" => array( "GET Data" => $_GET, "POST Data" => $_POST, "Files" => $_FILES, "Cookies" => $_COOKIE, "Session" => isset($_SESSION) ? $_SESSION : array(), "Server/Request Data" => $_SERVER, "Environment Variables" => $_ENV, ), );
if (isset($customCssFile)) { $vars["stylesheet"] .= file_get_contents($customCssFile); }
// Add extra entries list of data tables: // @todo: Consolidate addDataTable and addDataTableCallback $extraTables = array_map(function ($table) { return $table instanceof \Closure ? $table() : $table; }, $this->getDataTables()); $vars["tables"] = array_merge($extraTables, $vars["tables"]);
if (\Whoops\Util\Misc::canSendHeaders()) { header('Content-Type: text/html'); }
$helper->setVariables($vars); $helper->render($templateFile);
return Handler::QUIT; }
/** * Adds an entry to the list of tables displayed in the template. * The expected data is a simple associative array. Any nested arrays * will be flattened with print_r * @param string $label * @param array $data */ public function addDataTable($label, array $data) { $this->extraTables[$label] = $data; }
/** * Lazily adds an entry to the list of tables displayed in the table. * The supplied callback argument will be called when the error is rendered, * it should produce a simple associative array. Any nested arrays will * be flattened with print_r. * * @throws InvalidArgumentException If $callback is not callable * @param string $label * @param callable $callback Callable returning an associative array */ public function addDataTableCallback($label, /* callable */ $callback) { if (!is_callable($callback)) { throw new InvalidArgumentException('Expecting callback argument to be callable'); }
$this->extraTables[$label] = function () use ($callback) { try { $result = call_user_func($callback);
// Only return the result if it can be iterated over by foreach(). return is_array($result) || $result instanceof \Traversable ? $result : array(); } catch (\Exception $e) { // Don't allow failure to break the rendering of the original exception. return array(); } }; }
/** * Returns all the extra data tables registered with this handler. * Optionally accepts a 'label' parameter, to only return the data * table under that label. * @param string|null $label * @return array[]|callable */ public function getDataTables($label = null) { if ($label !== null) { return isset($this->extraTables[$label]) ? $this->extraTables[$label] : array(); }
return $this->extraTables; }
/** * Allows to disable all attempts to dynamically decide whether to * handle or return prematurely. * Set this to ensure that the handler will perform no matter what. * @param bool|null $value * @return bool|null */ public function handleUnconditionally($value = null) { if (func_num_args() == 0) { return $this->handleUnconditionally; }
$this->handleUnconditionally = (bool) $value; }
/** * Adds an editor resolver, identified by a string * name, and that may be a string path, or a callable * resolver. If the callable returns a string, it will * be set as the file reference's href attribute. * * @example * $run->addEditor('macvim', "mvim://open?url=file://%file&line=%line") * @example * $run->addEditor('remove-it', function($file, $line) { * unlink($file); * return "http://stackoverflow.com"; * }); * @param string $identifier * @param string $resolver */ public function addEditor($identifier, $resolver) { $this->editors[$identifier] = $resolver; }
/** * Set the editor to use to open referenced files, by a string * identifier, or a callable that will be executed for every * file reference, with a $file and $line argument, and should * return a string. * * @example * $run->setEditor(function($file, $line) { return "file:///{$file}"; }); * @example * $run->setEditor('sublime'); * * @throws InvalidArgumentException If invalid argument identifier provided * @param string|callable $editor */ public function setEditor($editor) { if (!is_callable($editor) && !isset($this->editors[$editor])) { throw new InvalidArgumentException( "Unknown editor identifier: $editor. Known editors:" . implode(",", array_keys($this->editors)) ); }
$this->editor = $editor; }
/** * Given a string file path, and an integer file line, * executes the editor resolver and returns, if available, * a string that may be used as the href property for that * file reference. * * @throws InvalidArgumentException If editor resolver does not return a string * @param string $filePath * @param int $line * @return string|bool */ public function getEditorHref($filePath, $line) { $editor = $this->getEditor($filePath, $line);
if (!$editor) { return false; }
// Check that the editor is a string, and replace the // %line and %file placeholders: if (!isset($editor['url']) || !is_string($editor['url'])) { throw new UnexpectedValueException( __METHOD__ . " should always resolve to a string or a valid editor array; got something else instead." ); }
$editor['url'] = str_replace("%line", rawurlencode($line), $editor['url']); $editor['url'] = str_replace("%file", rawurlencode($filePath), $editor['url']);
return $editor['url']; }
/** * Given a boolean if the editor link should * act as an Ajax request. The editor must be a * valid callable function/closure * * @throws UnexpectedValueException If editor resolver does not return a boolean * @param string $filePath * @param int $line * @return bool */ public function getEditorAjax($filePath, $line) { $editor = $this->getEditor($filePath, $line);
// Check that the ajax is a bool if (!isset($editor['ajax']) || !is_bool($editor['ajax'])) { throw new UnexpectedValueException( __METHOD__ . " should always resolve to a bool; got something else instead." ); } return $editor['ajax']; }
/** * Given a boolean if the editor link should * act as an Ajax request. The editor must be a * valid callable function/closure * * @throws UnexpectedValueException If editor resolver does not return a boolean * @param string $filePath * @param int $line * @return mixed */ protected function getEditor($filePath, $line) { if ($this->editor === null && !is_string($this->editor) && !is_callable($this->editor)) { return false; } else if(is_string($this->editor) && isset($this->editors[$this->editor]) && !is_callable($this->editors[$this->editor])) { return array( 'ajax' => false, 'url' => $this->editors[$this->editor], ); } else if(is_callable($this->editor) || (isset($this->editors[$this->editor]) && is_callable($this->editors[$this->editor]))) { if(is_callable($this->editor)) { $callback = call_user_func($this->editor, $filePath, $line); } else { $callback = call_user_func($this->editors[$this->editor], $filePath, $line); }
return array( 'ajax' => isset($callback['ajax']) ? $callback['ajax'] : false, 'url' => (is_array($callback) ? $callback['url'] : $callback), ); }
return false; }
/** * @param string $title * @return void */ public function setPageTitle($title) { $this->pageTitle = (string) $title; }
/** * @return string */ public function getPageTitle() { return $this->pageTitle; }
/** * Adds a path to the list of paths to be searched for * resources. * * @throws InvalidArgumnetException If $path is not a valid directory * * @param string $path * @return void */ public function addResourcePath($path) { if (!is_dir($path)) { throw new InvalidArgumentException( "'$path' is not a valid directory" ); }
array_unshift($this->searchPaths, $path); }
/** * Adds a custom css file to be loaded. * * @param string $name * @return void */ public function addCustomCss($name) { $this->customCss = $name; }
/** * @return array */ public function getResourcePaths() { return $this->searchPaths; }
/** * Finds a resource, by its relative path, in all available search paths. * The search is performed starting at the last search path, and all the * way back to the first, enabling a cascading-type system of overrides * for all resources. * * @throws RuntimeException If resource cannot be found in any of the available paths * * @param string $resource * @return string */ protected function getResource($resource) { // If the resource was found before, we can speed things up // by caching its absolute, resolved path: if (isset($this->resourceCache[$resource])) { return $this->resourceCache[$resource]; }
// Search through available search paths, until we find the // resource we're after: foreach ($this->searchPaths as $path) { $fullPath = $path . "/$resource";
if (is_file($fullPath)) { // Cache the result: $this->resourceCache[$resource] = $fullPath; return $fullPath; } }
// If we got this far, nothing was found. throw new RuntimeException( "Could not find resource '$resource' in any resource paths." . "(searched: " . join(", ", $this->searchPaths). ")" ); }
/** * @deprecated * * @return string */ public function getResourcesPath() { $allPaths = $this->getResourcePaths();
// Compat: return only the first path added return end($allPaths) ?: null; }
/** * @deprecated * * @param string $resourcesPath * @return void */ public function setResourcesPath($resourcesPath) { $this->addResourcePath($resourcesPath); } }
|