GatesServiceProvider::boot()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace Fireworkweb\Gates;
4
5
use Fireworkweb\Gates\Commands\RoutesWithoutGate;
6
use Fireworkweb\Gates\Traits\HasGates;
7
use Illuminate\Support\ServiceProvider;
8
use Illuminate\Support\Str;
9
use Symfony\Component\Finder\Finder;
10
11
class GatesServiceProvider extends ServiceProvider
12
{
13
    public function boot()
14
    {
15
        $this->publishes([
16
            __DIR__.'/../config/gates.php' => config_path('gates.php'),
17
        ], 'config');
18
19
        $this->commands([
20
            RoutesWithoutGate::class,
21
        ]);
22
23
        $this->loadGates();
24
    }
25
26
    public function register()
27
    {
28
        $this->mergeConfigFrom(
29
            __DIR__.'/../config/gates.php',
30
            'gates'
31
        );
32
    }
33
34
    protected function loadGates()
35
    {
36
        $paths = array_filter(config('gates.paths'), function ($path) {
37
            return is_dir($path);
38
        });
39
40
        $files = ! empty($paths) ? (new Finder)->in($paths)->files() : [];
41
42
        collect($files)
43
            ->map(function ($file) {
44
                return str_replace(
45
                    ['/', '.php'],
46
                    ['\\', ''],
47
                    Str::ucfirst(Str::after($file->getPathname(), realpath(base_path()).DIRECTORY_SEPARATOR))
48
                );
49
            })
50
            ->merge(config('gates.classes'))
51
            ->unique()
52
            ->filter(function ($class) {
53
                return in_array(HasGates::class, class_uses_recursive($class))
54
                    && ! (new \ReflectionClass($class))->isAbstract();
55
            })
56
            ->each(function ($class) {
57
                $class::gateRegister();
58
            });
59
    }
60
}
61