forked from octobercms/library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExecutionContextProvider.php
More file actions
67 lines (57 loc) · 1.69 KB
/
ExecutionContextProvider.php
File metadata and controls
67 lines (57 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php namespace October\Rain\Foundation\Providers;
use Illuminate\Support\ServiceProvider;
/**
* ExecutionContextProvider sets the execution context globally
*/
class ExecutionContextProvider extends ServiceProvider
{
/**
* register the service provider.
*/
public function register()
{
$this->app->scoped('execution.context', function ($app) {
return $this->determineContext($app);
});
}
/**
* boot the service provider.
*/
public function boot()
{
// Refresh execution context when Octane receives a new request
if (class_exists(\Laravel\Octane\Events\RequestReceived::class)) {
$this->app['events']->listen(\Laravel\Octane\Events\RequestReceived::class, function ($event) {
$event->sandbox->forgetInstance('execution.context');
});
}
}
/**
* determineContext evaluates the execution context from the current request.
*/
protected function determineContext($app): string
{
$requestPath = $this->normalizeUrl($app['request']->path());
$backendUri = $this->normalizeUrl($app['config']->get('backend.uri', 'backend'));
if (str_starts_with($requestPath, $backendUri)) {
return 'backend';
}
return 'frontend';
}
/**
* normalizeUrl adds leading slash from a URL.
*
* @param string $url URL to normalize.
* @return string Returns normalized URL.
*/
protected function normalizeUrl($url)
{
if (substr($url, 0, 1) !== '/') {
$url = '/'.$url;
}
if (!strlen($url)) {
$url = '/';
}
return $url;
}
}