BreadcrumbsServiceProvider::boot()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Rivalex\Breadcrumbs;
4
5
// Not available until Laravel 5.8
6
//use Illuminate\Contracts\Support\DeferrableProvider;
7
use Illuminate\Support\ServiceProvider;
8
9
/**
10
 * The Laravel service provider, which registers, configures and bootstraps the package.
11
 */
12
class BreadcrumbsServiceProvider extends ServiceProvider //implements DeferrableProvider
13
{
14
    public function isDeferred()
15
    {
16
        // Remove this and uncomment DeferrableProvider after dropping support
17
        // for Laravel 5.7 and below
18
        return true;
19
    }
20
21
    /**
22
     * Get the services provided for deferred loading.
23
     *
24
     * @return array
25
     */
26
    public function provides(): array
27
    {
28
        return [BreadcrumbsManager::class];
29
    }
30
31
    /**
32
     * Register the service provider.
33
     *
34
     * @return void
35
     */
36
    public function register(): void
37
    {
38
        // Load the default config values
39
        $this->mergeConfigFrom(__DIR__ . '/../config/breadcrumbs.php', 'breadcrumbs');
40
41
        // Register Manager class singleton with the app container
42
        $this->app->singleton(BreadcrumbsManager::class, config('breadcrumbs.manager-class'));
43
44
        // Register Generator class so it can be overridden
45
        $this->app->bind(BreadcrumbsGenerator::class, config('breadcrumbs.generator-class'));
46
    }
47
48
    /**
49
     * Bootstrap the application events.
50
     *
51
     * @return void
52
     */
53
    public function boot(): void
54
    {
55
        // Register 'breadcrumbs::' view namespace
56
        $this->loadViewsFrom(__DIR__ . '/../views/', 'breadcrumbs');
57
58
        // Publish the config/breadcrumbs.php file
59
        $this->publishes([
60
            __DIR__ . '/../config/breadcrumbs.php' => config_path('breadcrumbs.php'),
61
        ], 'breadcrumbs-config');
62
63
        // Load the routes/breadcrumbs.php file
64
        $this->registerBreadcrumbs();
65
    }
66
67
    /**
68
     * Load the routes/breadcrumbs.php file (if it exists) which registers available breadcrumbs.
69
     *
70
     * This method can be overridden in a child class. It is called by the boot() method, which Laravel calls
71
     * automatically when bootstrapping the application.
72
     *
73
     * @return void
74
     */
75
    public function registerBreadcrumbs(): void
76
    {
77
        // Load the routes/breadcrumbs.php file, or other configured file(s)
78
        $files = config('breadcrumbs.files');
79
80
        if (! $files) {
81
            return;
82
        }
83
84
        // If it is set to the default value and that file doesn't exist, skip loading it rather than causing an error
85
        if ($files === base_path('routes/breadcrumbs.php') && ! is_file($files)) {
86
            return;
87
        }
88
89
        // Support both Breadcrumbs:: and $breadcrumbs-> syntax by making $breadcrumbs variable available
90
        /** @noinspection PhpUnusedLocalVariableInspection */
91
        $breadcrumbs = $this->app->make(BreadcrumbsManager::class);
92
93
        // Support both a single string filename and an array of filenames (e.g. returned by glob())
94
        foreach ((array) $files as $file) {
95
            require $file;
96
        }
97
    }
98
}
99