Skip to content

Commit 2a5028a

Browse files
authored
Give New Application to Routes (#899)
* Give New Application to Routes * Tweak
1 parent 6010183 commit 2a5028a

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

src/Listeners/GiveNewApplicationInstanceToRouter.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Laravel\Octane\Listeners;
44

5+
use Illuminate\Routing\RouteCollection;
6+
57
class GiveNewApplicationInstanceToRouter
68
{
79
/**
@@ -16,5 +18,11 @@ public function handle($event): void
1618
}
1719

1820
$event->sandbox->make('router')->setContainer($event->sandbox);
21+
22+
if ($event->sandbox->resolved('routes') && $event->sandbox->make('routes') instanceof RouteCollection) {
23+
foreach ($event->sandbox->make('routes') as $route) {
24+
$route->setContainer($event->sandbox);
25+
}
26+
}
1927
}
2028
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Laravel\Octane\Listeners;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Routing\Route;
7+
use Laravel\Octane\Tests\TestCase;
8+
9+
use function Livewire\invade;
10+
11+
class GiveNewApplicationInstanceToRouterTest extends TestCase
12+
{
13+
public function test_router_has_new_instance()
14+
{
15+
[$app, $worker, $client] = $this->createOctaneContext([
16+
Request::create('/first', 'GET'),
17+
Request::create('/second', 'GET'),
18+
]);
19+
20+
$app['router']->middleware('web')->get('/first', function () {
21+
$route = collect(app('router')->getRoutes()->getRoutes())->firstWhere(fn (Route $route) => $route->uri() === 'second');
22+
23+
return [
24+
spl_object_id(app()),
25+
spl_object_id(invade($route)->container),
26+
];
27+
});
28+
29+
$app['router']->middleware('web')->get('/second', function () {
30+
$route = collect(app('router')->getRoutes()->getRoutes())->firstWhere(fn (Route $route) => $route->uri() === 'first');
31+
32+
return [
33+
spl_object_id(app()),
34+
spl_object_id(invade($route)->container),
35+
];
36+
});
37+
38+
$worker->run();
39+
40+
$this->assertEquals($client->responses[0]->getData()[0], $client->responses[0]->getData()[1]);
41+
$this->assertEquals($client->responses[1]->getData()[0], $client->responses[1]->getData()[1]);
42+
}
43+
}

0 commit comments

Comments
 (0)