MenuServiceProvider::boot()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 1
rs 9.6666
c 1
b 0
f 0
1
<?php
2
3
namespace Malezha\Menu;
4
5
use Illuminate\Contracts\Config\Repository;
6
use Illuminate\Contracts\Container\Container;
7
use Illuminate\Support\ServiceProvider;
8
use Malezha\Menu\Contracts\Attributes as AttributesContract;
9
use Malezha\Menu\Contracts\Builder as BuilderContract;
10
use Malezha\Menu\Contracts\ComparativeUrl as ComparativeUrlContract;
11
use Malezha\Menu\Contracts\FromArrayBuilder as FromArrayBuilderContract;
12
use Malezha\Menu\Contracts\Menu as MenuContract;
13
use Malezha\Menu\Contracts\MenuRender;
14
use Malezha\Menu\Support\Attributes;
15
use Malezha\Menu\Support\ComparativeUrl;
16
use Malezha\Menu\Support\FromArrayBuilder;
17
18
/**
19
 * Class MenuServiceProvider
20
 * @package Malezha\Menu
21
 */
22
class MenuServiceProvider extends ServiceProvider
23
{
24
    /**
25
     * Indicates if loading of the provider is deferred.
26
     *
27
     * @var bool
28
     */
29
    protected $defer = false;
30
31
    /**
32
     * Bootstrap the application services.
33
     *
34
     * @return void
35
     */
36 53
    public function boot()
37
    {
38 53
        $this->loadViewsFrom(__DIR__ . '/../views', 'menu');
39
40 53
        $this->publishes([
41 53
            __DIR__ . '/../views' => base_path('resources/views/vendor/menu'),
42 53
            __DIR__ . '/../config/menu.php' => config_path('menu.php'),
43
        ]);
44 53
    }
45
46
    /**
47
     * Register the application services.
48
     *
49
     * @return void
50
     */
51 53
    public function register()
52
    {
53 53
        $this->mergeConfigFrom(__DIR__ . '/../config/menu.php', 'menu');
54
55 53
        $this->registerFromArrayBuilder();
56 53
        $this->registerComparativeUrl();
57 53
        $this->registerRenderSystem();
58 53
        $this->registerAttributes();
59 53
        $this->registerBuilder();
60 53
        $this->registerSingleton();
61 53
    }
62
63 53
    protected function registerSingleton()
64
    {
65
        $this->app->singleton('menu.instance', function(Container $app) {
66 8
            return new Menu($app);
67 53
        });
68 53
        $this->app->alias('menu.instance', MenuContract::class);
69 53
    }
70
71 53
    protected function registerBuilder()
72
    {
73 53
        $this->app->bind('menu.builder', Builder::class);
74 53
        $this->app->alias('menu.builder', BuilderContract::class);
75 53
    }
76
77 53
    protected function registerAttributes()
78
    {
79 53
        $this->app->bind('menu.attributes', Attributes::class);
80 53
        $this->app->alias('menu.attributes', AttributesContract::class);
81 53
    }
82
83 53
    protected function registerRenderSystem()
84
    {
85
        $this->app->bind('menu.render', function (Container $app) {
86 33
            $config = $app->make(Repository::class)->get('menu');
87 33
            $key = $config['default'];
88 33
            $available = $config['renders'];
89
90 33
            if (array_key_exists($key, $available)) {
91 33
                return new $available[$key]($app);
92
            }
93
94
            throw new \Exception('Can use template system: ' . $config['default']);
95 53
        });
96 53
        $this->app->alias('menu.render', MenuRender::class);
97 53
    }
98
99 53
    protected function registerComparativeUrl()
100
    {
101
        $this->app->singleton('menu.compare-url', function (Container $app) {
102 22
            return $app->make(ComparativeUrl::class, [
0 ignored issues
show
Unused Code introduced by
The call to Container::make() has too many arguments starting with array('skippedPaths' => ...t('menu.skippedPaths')).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
103 22
                'skippedPaths' => $app->make(Repository::class)->get('menu.skippedPaths'),
104
            ]);
105 53
        });
106 53
        $this->app->alias('menu.compare-url', ComparativeUrlContract::class);
107 53
    }
108
109
    protected function registerFromArrayBuilder()
110
    {
111 53
        $this->app->singleton('menu.from-array-builder', function (Container $app) {
112 53
            return $app->make(FromArrayBuilder::class);
113 53
        });
114 53
        $this->app->alias('menu.from-array-builder', FromArrayBuilderContract::class);
115
116 53
        FromArrayBuilder::setInstance($this->app->make(FromArrayBuilderContract::class));
117 53
    }
118
119
    /**
120
     * Get the services provided by the provider.
121
     *
122
     * @return array
123
     */
124
    public function provides()
125
    {
126
        return [];
127
    }
128
}
129