Completed
Push — master ( efa9b4...b05f68 )
by wen
05:55
created

AdminServiceProvider::getBasePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Sco\Admin\Providers;
4
5
use Illuminate\Support\ServiceProvider;
6
use Sco\Admin\Admin;
7
use Sco\Admin\Contracts\Form\ElementFactoryInterface;
8
use Sco\Admin\Contracts\Form\FormFactoryInterface;
9
use Sco\Admin\Contracts\RepositoryInterface;
10
use Sco\Admin\Contracts\Display\ColumnFactoryInterface;
11
use Sco\Admin\Contracts\Display\FilterFactoryInterface;
12
use Sco\Admin\Contracts\Display\DisplayFactoryInterface;
13
use Sco\Admin\Form\ElementFactory;
14
use Sco\Admin\Form\FormFactory;
15
use Sco\Admin\Repositories\Repository;
16
use Sco\Admin\Display\ColumnFactory;
17
use Sco\Admin\Display\FilterFactory;
18
use Sco\Admin\Display\DisplayFactory;
19
20
class AdminServiceProvider extends ServiceProvider
21
{
22
    protected $middlewares = [
23
        'admin.auth'      => \Sco\Admin\Http\Middleware\Authenticate::class,
24
        'admin.guest'     => \Sco\Admin\Http\Middleware\RedirectIfAuthenticated::class,
25
    ];
26
27 48
    public function getBasePath()
28
    {
29 48
        return dirname(dirname(__DIR__));
30
    }
31
32
    /**
33
     * Bootstrap the application services.
34
     *
35
     * @return void
36
     */
37 48
    public function boot()
38
    {
39 48
        if ($this->app->runningInConsole()) {
40 48
            $this->loadMigrationsFrom($this->getBasePath() . '/database/migrations');
41
        }
42 48
    }
43
44
    /**
45
     * Register the application services.
46
     *
47
     * @return void
48
     */
49 48
    public function register()
50
    {
51 48
        $this->mergeConfigFrom(
52 48
            $this->getBasePath() . '/config/admin.php',
53 48
            'admin'
54
        );
55
56 48
        $this->registerMiddleware();
57 48
        $this->registerFactory();
58 48
        $this->app->instance('admin.instance', new Admin($this->app));
59 48
        $this->app->bind(RepositoryInterface::class, Repository::class);
60 48
    }
61
62 48
    protected function registerMiddleware()
63
    {
64 48
        foreach ($this->middlewares as $key => $middleware) {
65 48
            $this->app['router']->aliasMiddleware($key, $middleware);
66
        }
67 48
    }
68
69
    protected function registerFactory()
70
    {
71 48
        $this->app->singleton('admin.form.factory', function () {
72
            return new FormFactory($this->app);
73 48
        });
74 48
        $this->app->alias('admin.form.factory', FormFactoryInterface::class);
75
76 48
        $this->app->singleton('admin.element.factory', function () {
77
            return new ElementFactory($this->app);
78 48
        });
79 48
        $this->app->alias('admin.element.factory', ElementFactoryInterface::class);
80
81 48
        $this->app->singleton('admin.display.factory', function () {
82
            return new DisplayFactory();
83 48
        });
84 48
        $this->app->alias('admin.display.factory', DisplayFactoryInterface::class);
85
86 48
        $this->app->singleton('admin.column.factory', function () {
87
            return new ColumnFactory();
88 48
        });
89 48
        $this->app->alias('admin.column.factory', ColumnFactoryInterface::class);
90
91 48
        $this->app->singleton('admin.display.filter.factory', function () {
92
            return new FilterFactory();
93 48
        });
94 48
        $this->app->alias('admin.display.filter.factory', FilterFactoryInterface::class);
95 48
    }
96
}
97