Skip to content

Commit

Permalink
Give New Application to Routes (#899)
Browse files Browse the repository at this point in the history
* Give New Application to Routes

* Tweak
  • Loading branch information
dbpolito authored May 30, 2024
1 parent 6010183 commit 2a5028a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Listeners/GiveNewApplicationInstanceToRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Laravel\Octane\Listeners;

use Illuminate\Routing\RouteCollection;

class GiveNewApplicationInstanceToRouter
{
/**
Expand All @@ -16,5 +18,11 @@ public function handle($event): void
}

$event->sandbox->make('router')->setContainer($event->sandbox);

if ($event->sandbox->resolved('routes') && $event->sandbox->make('routes') instanceof RouteCollection) {
foreach ($event->sandbox->make('routes') as $route) {
$route->setContainer($event->sandbox);
}
}
}
}
43 changes: 43 additions & 0 deletions tests/Listeners/GiveNewApplicationInstanceToRouterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Laravel\Octane\Listeners;

use Illuminate\Http\Request;
use Illuminate\Routing\Route;
use Laravel\Octane\Tests\TestCase;

use function Livewire\invade;

class GiveNewApplicationInstanceToRouterTest extends TestCase
{
public function test_router_has_new_instance()
{
[$app, $worker, $client] = $this->createOctaneContext([
Request::create('/first', 'GET'),
Request::create('/second', 'GET'),
]);

$app['router']->middleware('web')->get('/first', function () {
$route = collect(app('router')->getRoutes()->getRoutes())->firstWhere(fn (Route $route) => $route->uri() === 'second');

return [
spl_object_id(app()),
spl_object_id(invade($route)->container),
];
});

$app['router']->middleware('web')->get('/second', function () {
$route = collect(app('router')->getRoutes()->getRoutes())->firstWhere(fn (Route $route) => $route->uri() === 'first');

return [
spl_object_id(app()),
spl_object_id(invade($route)->container),
];
});

$worker->run();

$this->assertEquals($client->responses[0]->getData()[0], $client->responses[0]->getData()[1]);
$this->assertEquals($client->responses[1]->getData()[0], $client->responses[1]->getData()[1]);
}
}

0 comments on commit 2a5028a

Please sign in to comment.