Completed
Push — master ( 26578f...8b8176 )
by wen
02:36
created

AdminServiceProvider::getBasePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Sco\Admin\Providers;
4
5
use Carbon\Carbon;
6
use Illuminate\Contracts\Debug\ExceptionHandler;
7
use Illuminate\Foundation\AliasLoader;
8
use Illuminate\Support\ServiceProvider;
9
use KodiComponents\Navigation\Contracts\BadgeInterface;
10
use KodiComponents\Navigation\Contracts\NavigationInterface;
11
use KodiComponents\Navigation\Contracts\PageInterface;
12
use KodiComponents\Navigation\Navigation;
13
use Laracasts\Utilities\JavaScript\JavaScriptServiceProvider;
14
use Sco\ActionLog\LaravelServiceProvider;
15
use Sco\Admin\Admin;
16
use Sco\Admin\Config\ConfigFactory;
17
use Sco\Admin\Contracts\ColumnFactoryInterface;
18
use Sco\Admin\Contracts\ConfigFactoryInterface;
19
use Sco\Admin\Contracts\RepositoryInterface;
20
use Sco\Admin\Contracts\ViewFactoryInterface;
21
use Sco\Admin\Elements\ElementFactory;
22
use Sco\Admin\Exceptions\Handler;
23
use Sco\Admin\Facades\AdminColumnFacade;
24
use Sco\Admin\Facades\AdminFacade;
25
use Sco\Admin\Facades\AdminElementFacade;
26
use Sco\Admin\Facades\AdminNavigationFacade;
27
use Sco\Admin\Facades\AdminViewFacade;
28
use Sco\Admin\Navigation\Badge;
29
use Sco\Admin\Navigation\Page;
30
use Sco\Admin\Repositories\Repository;
31
use Sco\Admin\View\Columns\ColumnFactory;
32
use Sco\Admin\View\ViewFactory;
33
34
/**
35
 *
36
 */
37
class AdminServiceProvider extends ServiceProvider
38
{
39
    protected $commands = [
40
        \Sco\Admin\Console\InstallCommand::class,
41
    ];
42
43
    protected $middlewares = [
44
        'admin.guest'     => \Sco\Admin\Http\Middleware\RedirectIfAuthenticated::class,
45
        'admin.phptojs'   => \Sco\Admin\Http\Middleware\PHPVarToJavaScript::class,
46
        'admin.can.route' => \Sco\Admin\Http\Middleware\RouteAuthorize::class,
47
        'admin.can.model' => \Sco\Admin\Http\Middleware\ModelAuthorize::class,
48
    ];
49
50
    protected $providers = [
51
        LaravelServiceProvider::class,
52
        JavaScriptServiceProvider::class,
53
        ResourcesServiceProvider::class,
54
        ComponentServiceProvider::class,
55
        //NavigationServiceProvider::class,
56
    ];
57
58
    public function getBasePath()
59
    {
60
        return dirname(dirname(__DIR__));
61
    }
62
63
    /**
64
     * Bootstrap the application services.
65
     *
66
     * @return void
67
     */
68
    public function boot()
69
    {
70
        Carbon::setLocale($this->app['config']->get('app.locale'));
71
72
        if ($this->app->runningInConsole()) {
73
            $this->loadMigrationsFrom($this->getBasePath() . '/database/migrations');
74
        }
75
76
        $this->app->call([$this, 'registerNavigation']);
77
    }
78
79
    /**
80
     * Register the application services.
81
     *
82
     * @return void
83
     */
84
    public function register()
85
    {
86
        $this->mergeConfigFrom(
87
            $this->getBasePath() . '/config/admin.php',
88
            'admin'
89
        );
90
91
        $this->registerExceptionHandler();
92
        $this->registerAdmin();
93
        $this->registerAliases();
94
        $this->registerMiddleware();
95
96
        $this->app->bind(RepositoryInterface::class, Repository::class);
97
        $this->app->singleton(ConfigFactoryInterface::class, function () {
98
            return new ConfigFactory($this->app);
0 ignored issues
show
Compatibility introduced by
$this->app of type object<Illuminate\Contra...Foundation\Application> is not a sub-type of object<Illuminate\Foundation\Application>. It seems like you assume a concrete implementation of the interface Illuminate\Contracts\Foundation\Application to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
99
        });
100
        $this->app->singleton('admin.element.factory', function () {
101
            return new ElementFactory($this->app);
1 ignored issue
show
Compatibility introduced by
$this->app of type object<Illuminate\Contra...Foundation\Application> is not a sub-type of object<Illuminate\Foundation\Application>. It seems like you assume a concrete implementation of the interface Illuminate\Contracts\Foundation\Application to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
102
        });
103
104
        $this->app->singleton('admin.view.factory', function () {
105
            return new ViewFactory();
106
        });
107
108
        $this->app->singleton('admin.column.factory', function () {
109
            return new ColumnFactory();
110
        });
111
112
        $this->app->singleton('admin.navigation', function () {
113
            return new Navigation();
114
        });
115
        // overwrite Navigation Page Bind
116
        $this->app->bind(PageInterface::class, Page::class);
117
        $this->app->bind(BadgeInterface::class, Badge::class);
118
119
        $this->registerProviders();
120
        $this->registerCoreContainerAliases();
121
        $this->commands($this->commands);
122
    }
123
124
    protected function registerMiddleware()
125
    {
126
        foreach ($this->middlewares as $key => $middleware) {
127
            $this->app['router']->aliasMiddleware($key, $middleware);
128
        }
129
    }
130
131
    protected function registerAliases()
132
    {
133
        AliasLoader::getInstance([
134
            'Admin'           => AdminFacade::class,
135
            'AdminElement'    => AdminElementFacade::class,
136
            'AdminNavigation' => AdminNavigationFacade::class,
137
            'AdminColumn'     => AdminColumnFacade::class,
138
            'AdminView'       => AdminViewFacade::class,
139
        ]);
140
    }
141
142
    protected function registerProviders()
143
    {
144
        foreach ($this->providers as $provider) {
145
            $this->app->register($provider);
146
        }
147
    }
148
149
    protected function registerExceptionHandler()
150
    {
151
        $exceptHandler = app(ExceptionHandler::class);
152
        $this->app->singleton(
153
            ExceptionHandler::class,
154
            function () use ($exceptHandler) {
155
                return new Handler($exceptHandler);
156
            }
157
        );
158
    }
159
160
    protected function registerAdmin()
161
    {
162
        $this->app->instance('admin.instance', new Admin($this->app));
1 ignored issue
show
Compatibility introduced by
$this->app of type object<Illuminate\Contra...Foundation\Application> is not a sub-type of object<Illuminate\Foundation\Application>. It seems like you assume a concrete implementation of the interface Illuminate\Contracts\Foundation\Application to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
163
    }
164
165
    protected function registerCoreContainerAliases()
166
    {
167
        $aliases = [
168
            'admin.navigation'     => [Navigation::class, NavigationInterface::class],
169
            'admin.column.factory' => [
170
                ColumnFactory::class, ColumnFactoryInterface::class,
171
            ],
172
            'admin.view.factory'   => [
173
                ViewFactory::class, ViewFactoryInterface::class,
174
            ],
175
        ];
176
177
        foreach ($aliases as $key => $aliases) {
178
            foreach ($aliases as $alias) {
179
                $this->app->alias($key, $alias);
180
            }
181
        }
182
    }
183
184
    /**
185
     * @param NavigationInterface $navigation
186
     */
187
    public function registerNavigation(NavigationInterface $navigation)
188
    {
189
        require $this->getBasePath() . '/src/navigation.php';
190
    }
191
}
192