Completed
Push — master ( 4fd356...5a0c11 )
by Pavel
03:54
created

InstallCommand::registerServiceProvider()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 12
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 20
rs 9.8666
1
<?php
2
3
namespace PavelMironchik\LaravelBackupPanel\Console;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Console\DetectsApplicationNamespace;
7
use Illuminate\Support\Str;
8
9
class InstallCommand extends Command
10
{
11
    use DetectsApplicationNamespace;
0 ignored issues
show
Deprecated Code introduced by
The trait Illuminate\Console\DetectsApplicationNamespace has been deprecated: Usage of this trait is deprecated and it will be removed in Laravel 7.0. ( Ignorable by Annotation )

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

11
    use /** @scrutinizer ignore-deprecated */ DetectsApplicationNamespace;

This trait has been deprecated. The supplier of the trait has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the trait will be removed and what other trait to use instead.

Loading history...
12
13
    /**
14
     * The name and signature of the console command.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'laravel-backup-panel:install';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Install all of the Laravel Backup Panel resources';
26
27
    /**
28
     * Create a new command instance.
29
     *
30
     * @return void
31
     */
32
    public function __construct()
33
    {
34
        parent::__construct();
35
    }
36
37
    /**
38
     * Execute the console command.
39
     *
40
     * @return mixed
41
     */
42
    public function handle()
43
    {
44
        $this->comment('Publishing Laravel Backup Panel service provider...');
45
        $this->callSilent('vendor:publish', ['--tag' => 'laravel-backup-panel-provider']);
46
47
        $this->comment('Publishing Laravel Backup Panel assets...');
48
        $this->callSilent('vendor:publish', ['--tag' => 'laravel-backup-panel-assets']);
49
50
        $this->comment('Publishing Laravel Backup Panel configuration...');
51
        $this->callSilent('vendor:publish', ['--tag' => 'laravel-backup-panel-config']);
52
53
        $this->registerServiceProvider();
54
55
        $this->info('Laravel Backup Panel resources installed successfully.');
56
    }
57
58
    protected function registerServiceProvider()
59
    {
60
        $namespace = Str::replaceLast('\\', '', $this->getAppNamespace());
61
62
        $appConfig = file_get_contents(config_path('app.php'));
63
64
        if (Str::contains($appConfig, $namespace.'\\Providers\\LaravelBackupPanelServiceProvider::class')) {
65
            return;
66
        }
67
68
        file_put_contents(config_path('app.php'), str_replace(
69
            "{$namespace}\\Providers\EventServiceProvider::class,".PHP_EOL,
70
            "{$namespace}\\Providers\EventServiceProvider::class,".PHP_EOL."        {$namespace}\Providers\LaravelBackupPanelServiceProvider::class,".PHP_EOL,
71
            $appConfig
72
        ));
73
74
        file_put_contents(app_path('Providers/LaravelBackupPanelServiceProvider.php'), str_replace(
75
            "namespace App\Providers;",
76
            "namespace {$namespace}\Providers;",
77
            file_get_contents(app_path('Providers/LaravelBackupPanelServiceProvider.php'))
78
        ));
79
    }
80
}
81