-
-
Notifications
You must be signed in to change notification settings - Fork 248
/
Copy pathServiceProvider.php
107 lines (89 loc) · 2.94 KB
/
ServiceProvider.php
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
namespace Inertia;
use Illuminate\Foundation\Testing\TestResponse as LegacyTestResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Router;
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
use Illuminate\Testing\TestResponse;
use Illuminate\View\FileViewFinder;
use Inertia\Ssr\Gateway;
use Inertia\Ssr\HttpGateway;
use Inertia\Testing\TestResponseMacros;
use LogicException;
use ReflectionException;
class ServiceProvider extends BaseServiceProvider
{
public function register(): void
{
$this->app->singleton(ResponseFactory::class);
$this->app->bind(Gateway::class, HttpGateway::class);
$this->mergeConfigFrom(
__DIR__.'/../config/inertia.php',
'inertia'
);
$this->registerBladeDirectives();
$this->registerRequestMacro();
$this->registerRouterMacro();
$this->registerTestingMacros();
$this->app->bind('inertia.testing.view-finder', function ($app) {
return new FileViewFinder(
$app['files'],
$app['config']->get('inertia.testing.page_paths'),
$app['config']->get('inertia.testing.page_extensions')
);
});
}
public function boot(): void
{
$this->registerConsoleCommands();
$this->publishes([
__DIR__.'/../config/inertia.php' => config_path('inertia.php'),
]);
}
protected function registerBladeDirectives(): void
{
$this->callAfterResolving('blade.compiler', function ($blade) {
$blade->directive('inertia', [Directive::class, 'compile']);
$blade->directive('inertiaHead', [Directive::class, 'compileHead']);
});
}
protected function registerConsoleCommands(): void
{
if (! $this->app->runningInConsole()) {
return;
}
$this->commands([
Console\CreateMiddleware::class,
]);
}
protected function registerRequestMacro(): void
{
Request::macro('inertia', function () {
return (bool) $this->header('X-Inertia');
});
}
protected function registerRouterMacro(): void
{
Router::macro('inertia', function ($uri, $component, $props = []) {
return $this->match(['GET', 'HEAD'], $uri, '\\'.Controller::class)
->defaults('component', $component)
->defaults('props', $props);
});
}
/**
* @throws ReflectionException|LogicException
*/
protected function registerTestingMacros(): void
{
if (class_exists(TestResponse::class)) {
TestResponse::mixin(new TestResponseMacros());
return;
}
// Laravel <= 6.0
if (class_exists(LegacyTestResponse::class)) {
LegacyTestResponse::mixin(new TestResponseMacros());
return;
}
throw new LogicException('Could not detect TestResponse class.');
}
}