Skip to content

Commit

Permalink
bootloader changes giving router better compatibility and addressing …
Browse files Browse the repository at this point in the history
…issue #276
  • Loading branch information
digital-joe-co committed Jun 14, 2023
1 parent 5d1bb21 commit 63ec2f4
Showing 1 changed file with 38 additions and 37 deletions.
75 changes: 38 additions & 37 deletions src/Roots/Acorn/Bootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,20 +197,18 @@ protected function bootHttp(ApplicationContract $app)
{
$kernel = $app->make(\Illuminate\Contracts\Http\Kernel::class);
$request = \Illuminate\Http\Request::capture();
$time = time();

// Create a default route for wordpress actions to go through
$app->make('router')->get('{any?}', function () use ($time) {
return response()->json(['message' => "wordpress_request_$time" ]);
})->where('any', '.*');

$app->instance('request', $request);
Facade::clearResolvedInstance('request');

$kernel->bootstrap($request);

try {
if (! $app->make('router')->getRoutes()->match($request)) {
throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException();
}
} catch (\Exception $e) {
return;
}

add_filter(
'do_parse_request',
fn ($do_parse, \WP $wp, $extra_query_vars) =>
Expand All @@ -219,17 +217,29 @@ protected function bootHttp(ApplicationContract $app)
3
);

add_action('parse_request', function () use ($kernel, $request) {
/** @var \Illuminate\Http\Response */
add_action('parse_request', function () use ($time, $kernel, $request) {
/** @var \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\BinaryFileResponse */
$response = $kernel->handle($request);

if (! $response->isServerError() && $response->status() >= 400) {
if (in_array(true, [
$response instanceof \Illuminate\Http\Response
&& ! $response->isServerError() && $response->status() >= 400,
$response instanceof \Symfony\Component\HttpFoundation\BinaryFileResponse
&& ! $response->isServerError() && $response->getStatusCode() >= 400,
])) {
return;
}

$body = $response->send();
if (in_array(false, [
$response instanceof \Illuminate\Http\JsonResponse,
is_string($response->getContent()),
$data = json_decode($response->getContent()),
isset($data->message) && $data->message == "wordpress_request_$time",
])) {
$body = $response->send();

$kernel->terminate($request, $body);
$kernel->terminate($request, $body);
}
});
}

Expand All @@ -255,30 +265,21 @@ public function getApplication(): ApplicationContract
{
$this->app ??= new Application($this->basePath(), $this->usePaths());

$this->app->singleton(
\Illuminate\Contracts\Http\Kernel::class,
Env::get('ACORN_ENABLE_EXPIRIMENTAL_ROUTER')
? \Roots\Acorn\Http\Kernel::class
: \Roots\Acorn\Kernel::class
);

$this->app->singleton(
\Illuminate\Contracts\Console\Kernel::class,
\Roots\Acorn\Console\Kernel::class
);

$this->app->singleton(
\Illuminate\Contracts\Debug\ExceptionHandler::class,
\Roots\Acorn\Exceptions\Handler::class
);

if (class_exists(\Whoops\Run::class)) {
$this->app->bind(
\Illuminate\Contracts\Foundation\ExceptionRenderer::class,
fn (\Illuminate\Contracts\Foundation\Application $app) =>
$app->make(\Roots\Acorn\Exceptions\Whoops\WhoopsExceptionRenderer::class)
);
}
$httpKernel = Env::get('ACORN_ENABLE_EXPIRIMENTAL_ROUTER')
? \Roots\Acorn\Http\Kernel::class
: \Roots\Acorn\Kernel::class;

collect(apply_filters('acorn/early/singletons', [
\Illuminate\Contracts\Http\Kernel::class => $httpKernel,
\Illuminate\Contracts\Console\Kernel::class => \Roots\Acorn\Console\Kernel::class,
\Illuminate\Contracts\Debug\ExceptionHandler::class => \Roots\Acorn\Exceptions\Handler::class,
]))
->each(fn($concrete, $abstract) => $this->app->singleton($abstract, $concrete));

collect(apply_filters('acorn/early/bindings', class_exists(\Whoops\Run::class)
? [\Illuminate\Contracts\Foundation\ExceptionRenderer::class => \Roots\Acorn\Exceptions\Whoops\WhoopsExceptionRenderer::class]
: []))
->each(fn($concrete, $abstract) => $this->app->bind($abstract, $concrete));

return $this->app;
}
Expand Down

0 comments on commit 63ec2f4

Please sign in to comment.