ServiceProvider::publishMigrations()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
c 0
b 0
f 0
rs 9.9
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Neurony\Url;
4
5
use Illuminate\Contracts\Foundation\Application;
6
use Illuminate\Routing\ControllerDispatcher;
7
use Illuminate\Routing\Route as Router;
8
use Illuminate\Support\Facades\File;
9
use Illuminate\Support\Facades\Route;
10
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
11
use Neurony\Url\Contracts\UrlModelContract;
12
use Neurony\Url\Models\Url;
13
14
class ServiceProvider extends BaseServiceProvider
15
{
16
    /**
17
     * Create a new service provider instance.
18
     *
19
     * @param Application $app
20
     */
21
    public function __construct(Application $app)
22
    {
23
        parent::__construct($app);
24
    }
25
26
    /**
27
     * Bootstrap the application services.
28
     *
29
     * @return void
30
     */
31
    public function boot()
32
    {
33
        $this->publishMigrations();
34
        $this->registerRoutes();
35
        $this->registerRouteBindings();
36
    }
37
38
    /**
39
     * Register the application services.
40
     *
41
     * @return void
42
     */
43
    public function register()
44
    {
45
    }
46
47
    /**
48
     * @return void
49
     */
50
    protected function publishMigrations()
51
    {
52
        if (empty(File::glob(database_path('migrations/*_create_urls_table.php')))) {
53
            $timestamp = date('Y_m_d_His', time());
54
            $migration = database_path("migrations/{$timestamp}_create_urls_table.php");
55
56
            $this->publishes([
57
                __DIR__.'/../database/migrations/create_urls_table.php.stub' => $migration,
58
            ], 'migrations');
59
        }
60
    }
61
62
    /**
63
     * @return void
64
     */
65
    protected function registerRoutes()
66
    {
67
        Route::macro('customUrl', function () {
68
            Route::get('{all}', function ($url = '/') {
69
                $url = Url::whereUrl($url)->first();
70
71
                if (! $url) {
72
                    abort(404);
73
                }
74
75
                $model = $url->urlable;
76
77
                if (! $model) {
78
                    abort(404);
79
                }
80
81
                $controller = $model->getUrlOptions()->routeController;
82
                $action = $model->getUrlOptions()->routeAction;
83
84
                return (new ControllerDispatcher(app()))->dispatch(
85
                    app(Router::class)->setAction([
86
                        'uses' => $controller.'@'.$action,
87
                        'model' => $model,
88
                    ]), app($controller), $action
89
                );
90
            })->where('all', '(.*)');
91
        });
92
    }
93
94
    /**
95
     * @return void
96
     */
97
    protected function registerRouteBindings()
98
    {
99
        Route::model('url', UrlModelContract::class);
100
    }
101
}
102