Skip to content

Commit 964d7e0

Browse files
committed
Fixes octane support
1 parent 37e33c1 commit 964d7e0

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

src/Foundation/Application.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,15 @@ public function runningInFrontend()
270270
return !$this->runningInBackend() && !$this->runningInConsole();
271271
}
272272

273+
/**
274+
* runningInOctane determines if the application is running under Laravel Octane.
275+
* @return bool
276+
*/
277+
public function runningInOctane()
278+
{
279+
return isset($_ENV['OCTANE_SERVER']) && $_ENV['OCTANE_SERVER'];
280+
}
281+
273282
/**
274283
* hasDatabase returns true if a database connection is present.
275284
* @return boolean

src/Foundation/Providers/ExecutionContextProvider.php

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,38 @@ class ExecutionContextProvider extends ServiceProvider
1212
*/
1313
public function register()
1414
{
15-
$this->app->singleton('execution.context', function ($app) {
15+
$this->app->scoped('execution.context', function ($app) {
16+
return $this->determineContext($app);
17+
});
18+
}
1619

17-
$requestPath = $this->normalizeUrl($app['request']->path());
20+
/**
21+
* boot the service provider.
22+
*/
23+
public function boot()
24+
{
25+
// Refresh execution context when Octane receives a new request
26+
if (class_exists(\Laravel\Octane\Events\RequestReceived::class)) {
27+
$this->app['events']->listen(\Laravel\Octane\Events\RequestReceived::class, function ($event) {
28+
$event->sandbox->forgetInstance('execution.context');
29+
});
30+
}
31+
}
1832

19-
$backendUri = $this->normalizeUrl($app['config']->get('backend.uri', 'backend'));
33+
/**
34+
* determineContext evaluates the execution context from the current request.
35+
*/
36+
protected function determineContext($app): string
37+
{
38+
$requestPath = $this->normalizeUrl($app['request']->path());
2039

21-
if (starts_with($requestPath, $backendUri)) {
22-
return 'backend';
23-
}
40+
$backendUri = $this->normalizeUrl($app['config']->get('backend.uri', 'backend'));
2441

25-
return 'frontend';
26-
});
42+
if (starts_with($requestPath, $backendUri)) {
43+
return 'backend';
44+
}
45+
46+
return 'frontend';
2747
}
2848

2949
/**

0 commit comments

Comments
 (0)