├── .gitignore ├── logo.png ├── composer.json ├── README.md └── src └── WhoopsMiddleWare.php /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | vendor/* 3 | .*~ 4 | .buildpath 5 | .project 6 | .settings -------------------------------------------------------------------------------- /logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thecodingmachine/whoops-stackphp/HEAD/logo.png -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name" : "mouf/whoops-stackphp", 3 | "description" : "This package provides a StackPHP middleware for using the Whoops error handling library", 4 | "type" : "library", 5 | "authors" : [{ 6 | "name" : "David Négrier", 7 | "email" : "d.negrier@thecodingmachine.com", 8 | "homepage" : "http://mouf-php.com" 9 | } 10 | ], 11 | "keywords" : [ 12 | "stackphp", 13 | "whoops", 14 | "mouf" 15 | ], 16 | "homepage" : "https://github.com/thecodingmachine/whoops-library", 17 | "license" : [ 18 | "MIT" 19 | ], 20 | "require" : { 21 | "php" : ">=5.5.0", 22 | "symfony/http-kernel" : "~2.0 | ~3.0", 23 | "mouf/utils.common.conditioninterface": "~2.0", 24 | "filp/whoops": "~2.0", 25 | "symfony/var-dumper": "~3.0" 26 | }, 27 | "autoload" : { 28 | "psr-4" : { 29 | "Whoops\\StackPhp\\" : "src/" 30 | } 31 | }, 32 | "extra" : { 33 | "mouf" : { 34 | "logo" : "logo.png" 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Whoops middleware for StackPHP 2 | ============================== 3 | 4 | This package contains a [StackPHP middleware](http://stackphp.com/) that catches all exceptions and redirects those to the [Whoops error handling library](http://filp.github.io/whoops/). 5 | 6 | Installation 7 | ------------ 8 | 9 | Through [Composer](https://getcomposer.org/) as [mouf/whoops-stackphp](https://packagist.org/packages/mouf/whoops-stackphp). 10 | 11 | Usage 12 | ----- 13 | 14 | Simply use the `WhoopsMiddleWare` class in your middleware stack: 15 | 16 | ```php 17 | use Whoops\StackPhp\WhoopsMiddleWare; 18 | 19 | $router = new WhoopsMiddleWare( 20 | new MyOtherRouter( 21 | new YetAnotherRouter())); 22 | 23 | ``` 24 | 25 | If an exception is thrown, or an error is raised, Whoops will display a nice error message: 26 | 27 | [![Sample error screen](http://filp.github.io/whoops/screen.png)](http://filp.github.io/whoops/demo/) 28 | 29 | The `WhoopsMiddleWare` constructor accepts 3 parameters: 30 | 31 | ```php 32 | public function __construct(HttpKernelInterface $router, $catchExceptions = true, $catchErrors = true); 33 | ``` 34 | 35 | - **$router**: this is the next router to be called on the Stack 36 | - **$catchExceptions**: Set to true to catch exception. Set to false to ignore exceptions (for production servers) 37 | - **$catchErrors**: Set to true to catch raised errors. Set to false to ignore raised errors (for production servers) 38 | 39 | Note: `$catchExceptions` and `$catchErrors` can be passed a boolean, a callable (that returns a boolean) or a [ConditionInterface](http://mouf-php.com/packages/mouf/utils.common.conditioninterface/README.md) that evaluates to true or false. 40 | 41 | -------------------------------------------------------------------------------- /src/WhoopsMiddleWare.php: -------------------------------------------------------------------------------- 1 | router = $router; 34 | $this->catchExceptions = $catchExceptions; 35 | $this->catchErrors = $catchErrors; 36 | } 37 | 38 | /** 39 | * Handles a Request to convert it to a Response. 40 | * 41 | * When $catch is true, the implementation must catch all exceptions 42 | * and do its best to convert them to a Response instance. 43 | * 44 | * @param Request $request A Request instance 45 | * @param int $type The type of the request 46 | * (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST) 47 | * @param bool $catch Whether to catch exceptions or not 48 | * 49 | * @return Response A Response instance 50 | * 51 | * @throws \Exception When an Exception occurs during processing (and $catch is set to false) 52 | */ 53 | public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true) { 54 | if ($this->resolveBool($this->catchErrors)) { 55 | $whoops = $this->getWhoops(); 56 | $whoops->register(); 57 | } 58 | 59 | if ($catch && $this->resolveBool($this->catchExceptions)) { 60 | try { 61 | return $this->router->handle($request, $type, false); 62 | } catch (\Exception $e) { 63 | $method = Run::EXCEPTION_HANDLER; 64 | 65 | ob_start(); 66 | $whoops = $this->getWhoops(); 67 | $whoops->$method($e); 68 | $response = ob_get_clean(); 69 | $code = $e instanceof HttpExceptionInterface ? $e->getStatusCode() : 500; 70 | return new Response($response, $code); 71 | } 72 | }else{ 73 | return $this->router->handle($request, $type); 74 | } 75 | } 76 | 77 | protected function resolveBool($value) { 78 | if ($value instanceof ConditionInterface) { 79 | return $value->isOk($this); 80 | } elseif (is_callable($value)) { 81 | return $value(); 82 | } else { 83 | return $value; 84 | } 85 | } 86 | 87 | /** 88 | * Returns a Whoops\Run instance (creates the instance if it does not exist). 89 | * @return \Whoops\Run 90 | */ 91 | protected function getWhoops() { 92 | if ($this->whoops === null) { 93 | $this->whoops = new Run(); 94 | $this->whoops->pushHandler(new PrettyPageHandler()); 95 | } 96 | return $this->whoops; 97 | } 98 | 99 | } --------------------------------------------------------------------------------