1
|
|
|
<?php namespace GeneaLabs\LaravelCaffeine\Providers; |
2
|
|
|
|
3
|
|
|
use GeneaLabs\LaravelCaffeine\Console\Commands\Publish; |
4
|
|
|
use GeneaLabs\LaravelCaffeine\Http\Middleware\LaravelCaffeineDripMiddleware; |
5
|
|
|
use Illuminate\Contracts\Http\Kernel; |
6
|
|
|
use Illuminate\Routing\Route; |
7
|
|
|
use Illuminate\Support\ServiceProvider; |
8
|
|
|
|
9
|
|
|
class Service extends ServiceProvider |
10
|
|
|
{ |
11
|
|
|
protected $defer = false; |
12
|
|
|
|
13
|
|
|
public function boot() |
14
|
|
|
{ |
15
|
|
|
app('router')->group($this->middlewareGroupExists('web') |
16
|
|
|
? ['middleware' => 'web'] |
17
|
|
|
: [], function () { |
18
|
|
|
require __DIR__ . '/../../routes/web.php'; |
19
|
|
|
|
20
|
|
|
if (app('env') === 'testing') { |
21
|
|
|
require __DIR__ . '/../../tests/routes/web.php'; |
22
|
|
|
} |
23
|
|
|
}); |
24
|
|
|
|
25
|
|
|
$configPath = __DIR__ . '/../../config/genealabs-laravel-caffeine.php'; |
26
|
|
|
$this->mergeConfigFrom($configPath, 'genealabs-laravel-caffeine'); |
27
|
|
|
$this->loadViewsFrom( |
28
|
|
|
__DIR__ . '/../../resources/views', |
29
|
|
|
'genealabs-laravel-caffeine' |
30
|
|
|
); |
31
|
|
|
|
32
|
|
|
if (app('env') === 'testing') { |
33
|
|
|
$this->loadViewsFrom( |
34
|
|
|
__DIR__ . '/../../tests/resources/views', |
35
|
|
|
'genealabs-laravel-caffeine' |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$this->publishes([ |
40
|
|
|
$configPath => config_path('genealabs-laravel-caffeine.php') |
41
|
|
|
], 'config'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function register() |
45
|
|
|
{ |
46
|
|
|
$this->commands(Publish::class); |
47
|
|
|
$this->mergeConfigFrom(__DIR__ . '/../../config/genealabs-laravel-caffeine.php', 'genealabs-laravel-caffeine'); |
48
|
|
|
|
49
|
|
|
if ($this->shouldRegisterMiddleware()) { |
50
|
|
|
app(Kernel::class)->pushMiddleware('\\' . LaravelCaffeineDripMiddleware::class); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function provides() : array |
55
|
|
|
{ |
56
|
|
|
return ['genealabs-laravel-caffeine']; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
protected function middlewareGroupExists(string $group) : bool |
60
|
|
|
{ |
61
|
|
|
$routes = collect(app('router')->getRoutes()->getRoutes()); |
62
|
|
|
|
63
|
|
|
return $routes->reduce(function ($carry, Route $route) use ($group) { |
64
|
|
|
$carry = ($carry ?? false) ?: false; |
65
|
|
|
$actions = (array) $route->getAction(); |
66
|
|
|
|
67
|
|
|
if (array_key_exists('middleware', $actions) |
68
|
|
|
&& in_array($group, (array) $actions['middleware']) |
69
|
|
|
) { |
70
|
|
|
return true; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $carry; |
74
|
|
|
}) ?? false; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
protected function shouldRegisterMiddleware() : bool |
78
|
|
|
{ |
79
|
|
|
return (! request()->ajax() |
80
|
|
|
&& (php_sapi_name() === 'fpm-fcgi' || app('env') === 'testing')); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|