Completed
Push — master ( deb819...6a78f1 )
by Takafumi
04:41
created

ServiceProvider::configPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Enomotodev\LaractiveAdmin;
4
5
use Collective\Html\HtmlServiceProvider;
6
use Collective\Html\FormFacade;
7
use Collective\Html\HtmlFacade;
8
use Intervention\Httpauth\Httpauth;
9
use Intervention\Httpauth\HttpauthServiceProvider;
10
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
11
use Enomotodev\LaractiveAdmin\Http\Middleware\LaractiveAdminAuthenticate;
12
use Enomotodev\LaractiveAdmin\Http\Middleware\HttpauthAuthenticate;
13
use Enomotodev\LaractiveAdmin\Console\InstallCommand;
14
use Enomotodev\LaractiveAdmin\Console\UninstallCommand;
15
use Enomotodev\LaractiveAdmin\Console\SeedCommand;
16
17
class ServiceProvider extends BaseServiceProvider
18
{
19
    /**
20
     * Bootstrap the application events.
21
     *
22
     * @return void
23
     */
24 11
    public function boot()
25
    {
26 11
        $this->publishes([$this->configPath() => config_path('laractive-admin.php')], 'config');
27
28 11
        $this->loadViewsFrom(__DIR__.'/../resources/views', 'laractive-admin');
29
30
        $routeConfig = [
31 11
            'middleware' => ['web', 'laractive-admin', 'httpauth'],
32 11
            'namespace' => 'App\Admin',
33 11
            'prefix' => $this->app['config']->get('laractive-admin.route_prefix'),
34 11
            'as' => 'admin.',
35
            'where' => [
36
                'id' => '[0-9]+',
37
            ],
38
        ];
39
40 11
        if (is_dir($directory = app_path('Admin'))) {
41 11
            $this->getRouter()->group($routeConfig, function ($router) {
42
                /** @var $router \Illuminate\Routing\Router */
43 11
                $files = $this->getFilesystem()->allFiles(app_path('Admin'));
44
45 11
                foreach ($files as $file) {
46 11
                    if ($file->getFilename() === 'DashboardController.php') {
47
                        continue;
48
                    }
49
50 11
                    $filename = $file->getFilename();
51 11
                    $className = substr($filename, 0, -4);
52 11
                    $adminClassName = "\App\Admin\\{$className}";
53 11
                    $adminClass = new $adminClassName;
54 11
                    $model = new $adminClass->model;
55 11
                    $routePrefix = $model->getTable();
56 11
                    $router->get("{$routePrefix}", [
57 11
                        'uses' => "\App\Admin\\{$className}@index",
58 11
                        'as' => "{$routePrefix}.index",
59
                    ]);
60 11
                    $router->get("{$routePrefix}/{id}", [
61 11
                        'uses' => "\App\Admin\\{$className}@show",
62 11
                        'as' => "{$routePrefix}.show",
63
                    ]);
64 11
                    $router->get("{$routePrefix}/new", [
65 11
                        'uses' => "\App\Admin\\{$className}@new",
66 11
                        'as' => "{$routePrefix}.new",
67
                    ]);
68 11
                    $router->post("{$routePrefix}", [
69 11
                        'uses' => "\App\Admin\\{$className}@create",
70 11
                        'as' => "{$routePrefix}.create",
71
                    ]);
72 11
                    $router->get("{$routePrefix}/{id}/edit", [
73 11
                        'uses' => "\App\Admin\\{$className}@edit",
74 11
                        'as' => "{$routePrefix}.edit",
75
                    ]);
76 11
                    $router->put("{$routePrefix}/{id}", [
77 11
                        'uses' => "\App\Admin\\{$className}@update",
78 11
                        'as' => "{$routePrefix}.update",
79
                    ]);
80 11
                    $router->delete("{$routePrefix}/{id}", [
81 11
                        'uses' => "\App\Admin\\{$className}@destroy",
82 11
                        'as' => "{$routePrefix}.destroy",
83
                    ]);
84 11
                    $router->post("{$routePrefix}/{id}/comments", [
85 11
                        'uses' => "\App\Admin\\{$className}@comments",
86 11
                        'as' => "{$routePrefix}.comments",
87
                    ]);
88
89 11
                    app(Menu::class)->setPage([
90 11
                        'name' => $className,
91 11
                        'url' => route("admin.{$routePrefix}.index"),
92
                    ]);
93
                }
94
95
                // Dashboard
96 11
                $router->get('/', [
97 11
                    'uses' => '\Enomotodev\LaractiveAdmin\Http\Controllers\DashboardController@index',
0 ignored issues
show
Bug introduced by
The controller App\Http\Controllers\\En...ers\DashboardController does not seem to exist.

If there is a route defined but the controller class cannot be found there are two options: 1. the controller class needs to be implemented or 2. the route is outdated and can be removed.

If ?FooController? was found and ?BarController? is missing for the following example, either the controller should be implemented or the route should be removed:

$app->group(['as' => 'foo', 'prefix' => 'foo', 'namespace' => 'Foo'], function($app) {
    $app->group(['as' => 'foo', 'prefix' => 'foo'], function($app) {
        $app->get('/all/{from}/{to}', ['as' => 'all', 'uses' => 'FooController@getFoo']);
        $app->get('/all/{from}/{to}', ['as' => 'all', 'uses' => 'BarController@getBar']);
    });
});
Loading history...
98
                    'as' => 'dashboard.index',
99
                ]);
100 11
            });
101
        }
102
103
        // Authentication
104 11
        $this->getRouter()->group([
105 11
            'middleware' => ['web', 'httpauth'],
106 11
            'prefix' => $this->app['config']->get('laractive-admin.route_prefix'),
107 11
        ], function($router) {
108
            /** @var $router \Illuminate\Routing\Router */
109 11
            $router->get('login', [
110 11
                'uses' => '\Enomotodev\LaractiveAdmin\Http\Controllers\Auth\LoginController@showLoginForm',
0 ignored issues
show
Bug introduced by
The controller App\Http\Controllers\\En...rs\Auth\LoginController does not seem to exist.

If there is a route defined but the controller class cannot be found there are two options: 1. the controller class needs to be implemented or 2. the route is outdated and can be removed.

If ?FooController? was found and ?BarController? is missing for the following example, either the controller should be implemented or the route should be removed:

$app->group(['as' => 'foo', 'prefix' => 'foo', 'namespace' => 'Foo'], function($app) {
    $app->group(['as' => 'foo', 'prefix' => 'foo'], function($app) {
        $app->get('/all/{from}/{to}', ['as' => 'all', 'uses' => 'FooController@getFoo']);
        $app->get('/all/{from}/{to}', ['as' => 'all', 'uses' => 'BarController@getBar']);
    });
});
Loading history...
111
                'as' => 'admin.login',
112
            ]);
113 11
            $router->post('login', [
114 11
                'uses' => '\Enomotodev\LaractiveAdmin\Http\Controllers\Auth\LoginController@login',
0 ignored issues
show
Bug introduced by
The controller App\Http\Controllers\\En...rs\Auth\LoginController does not seem to exist.

If there is a route defined but the controller class cannot be found there are two options: 1. the controller class needs to be implemented or 2. the route is outdated and can be removed.

If ?FooController? was found and ?BarController? is missing for the following example, either the controller should be implemented or the route should be removed:

$app->group(['as' => 'foo', 'prefix' => 'foo', 'namespace' => 'Foo'], function($app) {
    $app->group(['as' => 'foo', 'prefix' => 'foo'], function($app) {
        $app->get('/all/{from}/{to}', ['as' => 'all', 'uses' => 'FooController@getFoo']);
        $app->get('/all/{from}/{to}', ['as' => 'all', 'uses' => 'BarController@getBar']);
    });
});
Loading history...
115
            ]);
116 11
            $router->get('logout', [
117 11
                'uses' => '\Enomotodev\LaractiveAdmin\Http\Controllers\Auth\LoginController@logout',
0 ignored issues
show
Bug introduced by
The controller App\Http\Controllers\\En...rs\Auth\LoginController does not seem to exist.

If there is a route defined but the controller class cannot be found there are two options: 1. the controller class needs to be implemented or 2. the route is outdated and can be removed.

If ?FooController? was found and ?BarController? is missing for the following example, either the controller should be implemented or the route should be removed:

$app->group(['as' => 'foo', 'prefix' => 'foo', 'namespace' => 'Foo'], function($app) {
    $app->group(['as' => 'foo', 'prefix' => 'foo'], function($app) {
        $app->get('/all/{from}/{to}', ['as' => 'all', 'uses' => 'FooController@getFoo']);
        $app->get('/all/{from}/{to}', ['as' => 'all', 'uses' => 'BarController@getBar']);
    });
});
Loading history...
118
                'as' => 'admin.logout',
119
            ]);
120 11
        });
121
122 11
        \Illuminate\Database\Eloquent\Builder::macro('comments', function () {
123 4
            return $this->getModel()->morphMany(LaractiveAdminComment::class, 'commentable');
0 ignored issues
show
Bug introduced by
The method getModel() does not exist on Enomotodev\LaractiveAdmin\ServiceProvider. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

123
            return $this->/** @scrutinizer ignore-call */ getModel()->morphMany(LaractiveAdminComment::class, 'commentable');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
124 11
        });
125 11
    }
126
127
    /**
128
     * Register a service provider with the application.
129
     *
130
     * @return void
131
     */
132 11
    public function register()
133
    {
134 11
        $this->mergeConfigFrom($this->configPath(), 'laractive-admin');
135
136 11
        $this->app->register(HtmlServiceProvider::class);
137 11
        $this->app->register(HttpauthServiceProvider::class);
138
139 11
        $this->app->alias('Form', FormFacade::class);
140 11
        $this->app->alias('Html', HtmlFacade::class);
141
142 11
        $this->app->singleton(Menu::class);
143
144 11
        $this->app['config']['auth.guards'] += [
145
            'laractive-admin' => [
146
                'driver' => 'session',
147
                'provider' => 'admin_users',
148
            ],
149
        ];
150 11
        $this->app['config']['auth.providers'] += [
151
            'admin_users' => [
152
                'driver' => 'eloquent',
153
                'model' => AdminUser::class,
154
            ],
155
        ];
156
157 11
        $this->getRouter()->aliasMiddleware('laractive-admin', LaractiveAdminAuthenticate::class);
158 11
        $this->getRouter()->aliasMiddleware('httpauth', HttpauthAuthenticate::class);
159
160 11
        $this->app->singleton('command.laractive-admin.install', function ($app) {
161 11
            return new InstallCommand($app['files'], $app['composer']);
162 11
        });
163 11
        $this->app->singleton('command.laractive-admin.uninstall', function ($app) {
164 11
            return new UninstallCommand($app['files'], $app['composer']);
165 11
        });
166 11
        $this->app->singleton('command.laractive-admin.seed', function () {
167 11
            return new SeedCommand;
168 11
        });
169 11
        $this->app->singleton('httpauth', function ($app) {
170
            return new Httpauth($app['config']->get('laractive-admin.httpauth'));
171 11
        });
172
173 11
        $this->commands(['command.laractive-admin.install']);
174 11
        $this->commands(['command.laractive-admin.uninstall']);
175 11
        $this->commands(['command.laractive-admin.seed']);
176 11
    }
177
178
    /**
179
     * @return string
180
     */
181 11
    protected function configPath()
182
    {
183 11
        return __DIR__ . '/../config/laractive-admin.php';
184
    }
185
186
    /**
187
     * Get the active router.
188
     *
189
     * @return \Illuminate\Routing\Router
190
     */
191 11
    protected function getRouter()
192
    {
193 11
        return $this->app['router'];
194
    }
195
196
    /**
197
     * Get the filesystem.
198
     *
199
     * @return \Illuminate\Filesystem\Filesystem
200
     */
201 11
    protected function getFilesystem()
202
    {
203 11
        return $this->app['files'];
204
    }
205
}
206