| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package WPEmerge |
| 4 |
* @author Atanas Angelov <hi@atanas.dev> |
| 5 |
* @copyright 2017-2019 Atanas Angelov |
| 6 |
* @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0 |
| 7 |
* @link https://wpemerge.com/ |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace WPEmerge\Exceptions; |
| 11 |
|
| 12 |
use Pimple\Container; |
| 13 |
use Whoops\Handler\PrettyPageHandler; |
| 14 |
use Whoops\Run; |
| 15 |
use WPEmerge\Exceptions\Whoops\DebugDataProvider; |
| 16 |
use WPEmerge\ServiceProviders\ExtendsConfigTrait; |
| 17 |
use WPEmerge\ServiceProviders\ServiceProviderInterface; |
| 18 |
|
| 19 |
/** |
| 20 |
* Provide exceptions dependencies. |
| 21 |
* |
| 22 |
* @codeCoverageIgnore |
| 23 |
*/ |
| 24 |
class ExceptionsServiceProvider implements ServiceProviderInterface { |
| 25 |
use ExtendsConfigTrait; |
| 26 |
|
| 27 |
/** |
| 28 |
* {@inheritDoc} |
| 29 |
*/ |
| 30 |
public function register( $container ) { |
| 31 |
$debug = defined( 'WP_DEBUG' ) && WP_DEBUG; |
| 32 |
|
| 33 |
$this->extendConfig( $container, 'debug', [ |
| 34 |
'enable' => $debug, |
| 35 |
'pretty_errors' => $debug, |
| 36 |
] ); |
| 37 |
|
| 38 |
$this->registerPrettyErrorHandler( $container ); |
| 39 |
$this->registerErrorHandler( $container ); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Register the pretty error handler service. |
| 44 |
* |
| 45 |
* @param Container $container |
| 46 |
*/ |
| 47 |
protected function registerPrettyErrorHandler( $container ) { |
| 48 |
$container[ DebugDataProvider::class ] = function ( $container ) { |
| 49 |
return new DebugDataProvider( $container ); |
| 50 |
}; |
| 51 |
|
| 52 |
$container[ PrettyPageHandler::class ] = function ( $container ) { |
| 53 |
$handler = new PrettyPageHandler(); |
| 54 |
$handler->addResourcePath( implode( DIRECTORY_SEPARATOR, [WPEMERGE_DIR, 'src', 'Exceptions', 'Whoops'] ) ); |
| 55 |
|
| 56 |
$handler->addDataTableCallback( 'WP Emerge: Route', function ( $inspector ) use ( $container ) { |
| 57 |
return $container[ DebugDataProvider::class ]->route( $inspector ); |
| 58 |
} ); |
| 59 |
|
| 60 |
return $handler; |
| 61 |
}; |
| 62 |
|
| 63 |
$container[ Run::class ] = function ( $container ) { |
| 64 |
if ( ! class_exists( Run::class ) ) { |
| 65 |
return null; |
| 66 |
} |
| 67 |
|
| 68 |
$run = new Run(); |
| 69 |
$run->allowQuit( false ); |
| 70 |
|
| 71 |
$handler = $container[ PrettyPageHandler::class ]; |
| 72 |
|
| 73 |
if ( $handler ) { |
| 74 |
$run->pushHandler( $handler ); |
| 75 |
} |
| 76 |
|
| 77 |
return $run; |
| 78 |
}; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Register the error handler service. |
| 83 |
* |
| 84 |
* @param Container $container |
| 85 |
*/ |
| 86 |
protected function registerErrorHandler( $container ) { |
| 87 |
$container[ WPEMERGE_EXCEPTIONS_ERROR_HANDLER_KEY ] = function ( $container ) { |
| 88 |
$debug = $container[ WPEMERGE_CONFIG_KEY ]['debug']; |
| 89 |
$whoops = $debug['pretty_errors'] ? $container[ Run::class ] : null; |
| 90 |
return new ErrorHandler( $container[ WPEMERGE_RESPONSE_SERVICE_KEY ], $whoops, $debug['enable'] ); |
| 91 |
}; |
| 92 |
|
| 93 |
$container[ WPEMERGE_EXCEPTIONS_CONFIGURATION_ERROR_HANDLER_KEY ] = function ( $container ) { |
| 94 |
$debug = $container[ WPEMERGE_CONFIG_KEY ]['debug']; |
| 95 |
$whoops = $debug['pretty_errors'] ? $container[ Run::class ] : null; |
| 96 |
return new ErrorHandler( $container[ WPEMERGE_RESPONSE_SERVICE_KEY ], $whoops, $debug['enable'] ); |
| 97 |
}; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* {@inheritDoc} |
| 102 |
*/ |
| 103 |
public function bootstrap( $container ) { |
| 104 |
// Nothing to bootstrap. |
| 105 |
} |
| 106 |
} |
| 107 |
|