Skip to content

Instantly share code, notes, and snippets.

@l0gicgate
Created April 22, 2020 20:37
Show Gist options
  • Save l0gicgate/f875d062789f29e3b881e0bb16475d98 to your computer and use it in GitHub Desktop.
Save l0gicgate/f875d062789f29e3b881e0bb16475d98 to your computer and use it in GitHub Desktop.
<?php
declare(strict_types=1);
use App\Application\Handlers\HttpErrorHandler;
use App\Application\Handlers\ShutdownHandler;
use App\Application\ResponseEmitter\ResponseEmitter;
use DI\ContainerBuilder;
use Slim\Factory\AppFactory;
use Slim\Factory\ServerRequestCreatorFactory;
require __DIR__ . '/../vendor/autoload.php';
// Instantiate PHP-DI ContainerBuilder
$containerBuilder = new ContainerBuilder();
if (false) { // Should be set to true in production
$containerBuilder->enableCompilation(__DIR__ . '/../var/cache');
}
// Set up settings
$settings = require __DIR__ . '/../app/settings.php';
$settings($containerBuilder);
// Set up dependencies
$dependencies = require __DIR__ . '/../app/dependencies.php';
$dependencies($containerBuilder);
// Set up repositories
$repositories = require __DIR__ . '/../app/repositories.php';
$repositories($containerBuilder);
// Build PHP-DI Container instance
$container = $containerBuilder->build();
// Instantiate the app
AppFactory::setContainer($container);
$app = AppFactory::create();
$callableResolver = $app->getCallableResolver();
// Register middleware
$middleware = require __DIR__ . '/../app/middleware.php';
$middleware($app);
// Register routes
$routes = require __DIR__ . '/../app/routes.php';
$routes($app);
/** @var bool $displayErrorDetails */
$displayErrorDetails = $container->get('settings')['displayErrorDetails'];
// Create Error Handler
$responseFactory = $app->getResponseFactory();
$errorHandler = new HttpErrorHandler($callableResolver, $responseFactory);
// Create Shutdown Handler
$shutdownHandler = new ShutdownHandler(null, $errorHandler, $displayErrorDetails);
register_shutdown_function($shutdownHandler);
// Create Request object from globals
$serverRequestCreator = ServerRequestCreatorFactory::create();
$request = $serverRequestCreator->createServerRequestFromGlobals();
// Set Request Object On Shutdown Handler
$shutdownHandler->setRequest($request);
// Add Routing Middleware
$app->addRoutingMiddleware();
// Add Error Middleware
$errorMiddleware = $app->addErrorMiddleware($displayErrorDetails, false, false);
$errorMiddleware->setDefaultErrorHandler($errorHandler);
// Run App & Emit Response
$response = $app->handle($request);
$responseEmitter = new ResponseEmitter();
$responseEmitter->emit($response);
<?php
declare(strict_types = 1);
use Psr\Http\Message\ServerRequestInterface;
use Slim\Exception\HttpInternalServerErrorException;
use Slim\ResponseEmitter;
class ShutdownHandler
{
private ServerRequestInterface $request = null;
private HttpErrorHandler $errorHandler;
private bool $displayErrorDetails;
/**
* @param ServerRequestInterface $request
* @param $errorHandler $errorHandler
* @param bool $displayErrorDetails
*/
public function __construct(
ServerRequestInterface $request = null,
HttpErrorHandler $errorHandler,
bool $displayErrorDetails
) {
$this->request = $request;
$this->errorHandler = $errorHandler;
$this->displayErrorDetails = $displayErrorDetails;
}
public function setRequest(ServerRequestInterface $request): void
{
$this->request = $request;
}
public function __invoke()
{
$error = error_get_last();
if ($error) {
$errorFile = $error['file'];
$errorLine = $error['line'];
$errorMessage = $error['message'];
$errorType = $error['type'];
$message = 'An error while processing your request. Please try again later.';
if ($this->displayErrorDetails) {
switch ($errorType) {
case E_USER_ERROR:
$message = "FATAL ERROR: {$errorMessage}. ";
$message .= " on line {$errorLine} in file {$errorFile}.";
break;
case E_USER_WARNING:
$message = "WARNING: {$errorMessage}";
break;
case E_USER_NOTICE:
$message = "NOTICE: {$errorMessage}";
break;
default:
$message = "ERROR: {$errorMessage}";
$message .= " on line {$errorLine} in file {$errorFile}.";
break;
}
}
$exception = new HttpInternalServerErrorException($this->request, $message);
$response = $this->errorHandler->__invoke($this->request, $exception, $this->displayErrorDetails, false, false);
$responseEmitter = new ResponseEmitter();
$responseEmitter->emit($response);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment