Passed
Push — master ( f7707e...61da39 )
by Vasyl
01:34
created

ServiceProvider::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Fomvasss\UrlAliases;
4
5
use Fomvasss\UrlAliases\Middleware\UrlAliasMiddleware;
6
use Illuminate\Contracts\Http\Kernel;
7
8
class ServiceProvider extends \Illuminate\Support\ServiceProvider
9
{
10
    protected $defer = false;
11
12
    /**
13
     * Bootstrap the application services.
14
     *
15
     * @return void
16
     */
17
    public function boot()
18
    {
19
        $this->publishConfig();
20
21
        $this->publishMigrations();
22
23
        $this->publishSeeder();
24
        
25
//        $this->registerMiddleware(UrlAliasMiddleware::class);
26
27
        $this->makeBladeDirective();
28
        
29
    }
30
31
    /**
32
     * Register the application services.
33
     *
34
     * @return void
35
     */
36
    public function register()
37
    {
38
        $this->mergeConfigFrom(__DIR__.'/../config/url-aliases.php', 'url-aliases');
39
    }
40
41
    protected function publishConfig()
42
    {
43
        $this->publishes([
44
            __DIR__ . '/../config/url-aliases.php' => config_path('url-aliases.php')
0 ignored issues
show
Bug introduced by
The function config_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

44
            __DIR__ . '/../config/url-aliases.php' => /** @scrutinizer ignore-call */ config_path('url-aliases.php')
Loading history...
45
        ], 'url-aliases-config');
46
    }
47
48
    protected function publishSeeder()
49
    {
50
        $seedPath = __DIR__ . '/../database/seeds/UrlAliasesTableSeeder.php.stub';
51
        $this->publishes([
52
            $seedPath => database_path('seeds/UrlAliasesTableSeeder.php')
0 ignored issues
show
Bug introduced by
The function database_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

52
            $seedPath => /** @scrutinizer ignore-call */ database_path('seeds/UrlAliasesTableSeeder.php')
Loading history...
53
        ], 'url-aliases-seeder');
54
    }
55
56
    protected function publishMigrations()
57
    {
58
        if (! class_exists('CreateUrlAliasesTable')) {
59
            $timestamp = date('Y_m_d_His', time());
60
61
            $migrationPath = __DIR__.'/../database/migrations/create_url_aliases_table.php';
62
                $this->publishes([$migrationPath => database_path('/migrations/' . $timestamp . '_create_url_aliases_table.php'),
0 ignored issues
show
Bug introduced by
The function database_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

62
                $this->publishes([$migrationPath => /** @scrutinizer ignore-call */ database_path('/migrations/' . $timestamp . '_create_url_aliases_table.php'),
Loading history...
63
            ], 'url-aliases-migrations');
64
        }
65
    }
66
67
    protected function registerMiddleware($middleware)
68
    {
69
        $kernel = $this->app[Kernel::class];
70
        $kernel->pushMiddleware($middleware);
71
    }
72
73
    /**
74
     * Make blade directive "@urlAliasCurrent()"
75
     */
76
    protected function makeBladeDirective()
77
    {
78
        \Blade::directive('urlAliasCurrent', function ($expression) {
79
80
            list($absolute) = explode(', ', $expression);
81
82
            $path = request()->server('ALIAS_REQUEST_URI', request()->path());
0 ignored issues
show
Bug introduced by
The function request was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

82
            $path = /** @scrutinizer ignore-call */ request()->server('ALIAS_REQUEST_URI', request()->path());
Loading history...
83
84
            if (($absolute || $absolute === '') && $absolute != 'false') {
85
                $path = url($path);
0 ignored issues
show
Bug introduced by
The function url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

85
                $path = /** @scrutinizer ignore-call */ url($path);
Loading history...
86
            }
87
88
            return "<?php echo('{$path}'); ?>";
89
        });
90
    }
91
92
    protected function checkMakeDir(string $path)
93
    {
94
        if (!is_dir($path)) {
95
            mkdir($path, 0755, true);
96
        }
97
        return $path;
98
    }
99
}
100