example.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * Whoops - php errors for cool kids
  4. * @author Filipe Dobreira <http://github.com/filp>
  5. *
  6. * Run this example file with the PHP 5.4 web server with:
  7. *
  8. * $ cd project_dir
  9. * $ php -S localhost:8080
  10. *
  11. * and access localhost:8080/examples/example.php through your browser
  12. *
  13. * Or just run it through apache/nginx/what-have-yous as usual.
  14. */
  15. namespace Whoops\Example;
  16. use Exception as BaseException;
  17. use Whoops\Handler\PrettyPageHandler;
  18. use Whoops\Run;
  19. require __DIR__ . '/../vendor/autoload.php';
  20. require __DIR__ . '/lib.php';
  21. class Exception extends BaseException
  22. {
  23. }
  24. $run = new Run();
  25. $handler = new PrettyPageHandler();
  26. // Add a custom table to the layout:
  27. $handler->addDataTable('Ice-cream I like', [
  28. 'Chocolate' => 'yes',
  29. 'Coffee & chocolate' => 'a lot',
  30. 'Strawberry & chocolate' => 'it\'s alright',
  31. 'Vanilla' => 'ew',
  32. ]);
  33. $handler->setApplicationPaths([__FILE__]);
  34. $handler->addDataTableCallback('Details', function(\Whoops\Exception\Inspector $inspector) {
  35. $data = array();
  36. $exception = $inspector->getException();
  37. if ($exception instanceof SomeSpecificException) {
  38. $data['Important exception data'] = $exception->getSomeSpecificData();
  39. }
  40. $data['Exception class'] = get_class($exception);
  41. $data['Exception code'] = $exception->getCode();
  42. return $data;
  43. });
  44. $run->pushHandler($handler);
  45. // Example: tag all frames inside a function with their function name
  46. $run->pushHandler(function ($exception, $inspector, $run) {
  47. $inspector->getFrames()->map(function ($frame) {
  48. if ($function = $frame->getFunction()) {
  49. $frame->addComment("This frame is within function '$function'", 'cpt-obvious');
  50. }
  51. return $frame;
  52. });
  53. });
  54. $run->register();
  55. function fooBar()
  56. {
  57. throw new Exception("Something broke!");
  58. }
  59. function bar()
  60. {
  61. whoops_add_stack_frame(function(){
  62. fooBar();
  63. });
  64. }
  65. bar();