-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php declare(strict_types=1); | ||
|
||
require_once __DIR__ . '/../vendor/autoload.php'; | ||
|
||
session_start(); | ||
ob_start(); | ||
|
||
$clientIdentifier = 'PDS Interop OAuth Example App'; | ||
|
||
$request = \Laminas\Diactoros\ServerRequestFactory::fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES); | ||
|
||
$host = vsprintf('%s://%s%s', [ | ||
'Scheme' => $request->getUri()->getScheme(), | ||
'Host' => $request->getUri()->getHost(), | ||
'Port' => $request->getUri()->getPort() ? ':'.$request->getUri()->getPort() : '', | ||
]); | ||
|
||
|
||
// Set up routes | ||
$response = new \Laminas\Diactoros\Response(); | ||
$router = new \League\Route\Router(); | ||
|
||
$routes = [ | ||
'/' => new \Pdsinterop\Authentication\Router($response), | ||
]; | ||
|
||
array_walk($routes, function ($handler, $route) use (&$router) { | ||
$router->group($route, $handler->route()); | ||
}); | ||
|
||
// Create response for requested route | ||
try { | ||
$response = $router->dispatch($request); | ||
} catch (\League\Route\Http\Exception\NotFoundException $exception) { | ||
$error = $exception->getMessage(); | ||
$description = vsprintf("The requested route '%s' does not exist on this server.", [$request->getRequestTarget()]); | ||
} catch (\League\Route\Http\Exception\HttpExceptionInterface $exception) { | ||
$error = $exception->getMessage(); | ||
} catch (Throwable $exception) { | ||
// @FIXME: An exception here means a developer mistake (or "bug") as all exceptions should have been caught in the called route handler, how to handle? | ||
$error = 'dispatch_error'; | ||
} finally { | ||
if (isset($exception)) { | ||
$message = '<h1>DISPATCH ERROR</h1><pre>' . $exception . '</pre>'; | ||
$statusCode = 500; | ||
|
||
if (method_exists($exception, 'getStatusCode')) { | ||
$statusCode = $exception->getStatusCode(); | ||
} | ||
|
||
$response->getBody()->write($message); | ||
$response = $response->withStatus($statusCode); | ||
} | ||
} | ||
|
||
|
||
// Send the response to the browser | ||
$emitter = new \Laminas\HttpHandlerRunner\Emitter\SapiEmitter(); | ||
|
||
// Any output means a developer mistake (or "bug") | ||
ob_clean(); | ||
|
||
try { | ||
$emitter->emit($response); | ||
} catch (\Laminas\HttpHandlerRunner\Exception\EmitterException $exception) { | ||
// @FIXME: An exception here means a developer mistake (or "bug") as all exceptions should have been caught in the called emitter, how to handle? | ||
http_response_code(500); | ||
$message = '<h1>EMITTER ERROR</h1><pre>' . $exception . '</pre>'; | ||
|
||
echo $message; | ||
} | ||
|
||
exit; |