InstallCommand::handle()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Zendaemon\Services\Console;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Str;
7
8
class InstallCommand extends Command
9
{
10
    private const PROVIDER_NAME = 'ServiceLayerServiceProvider';
11
12
    /**
13
     * The name and signature of the console command.
14
     *
15
     * @var string
16
     */
17
    protected $signature = 'services:install';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'Install all of the service layer resources';
25
26
    /**
27
     * Execute the console command.
28
     *
29
     * @throws \Exception
30
     *
31
     * @return void
32
     */
33
    public function handle()
34
    {
35
        $this->registerServiceLayerServiceProvider();
36
37
        $this->info('Service Layer scaffolding installed successfully.');
38
    }
39
40
    /**
41
     * Register the ServiceLayer service provider in the application configuration file.
42
     *
43
     * @throws \Exception
44
     *
45
     * @return void
46
     */
47
    protected function registerServiceLayerServiceProvider()
48
    {
49
        $namespace = Str::replaceLast('\\', '', $this->laravel->getNamespace());
50
51
        $appConfig = file_get_contents(config_path('app.php'));
52
53
        if (Str::contains($appConfig, $namespace.'\\Providers\\'.self::PROVIDER_NAME.'::class')) {
54
            return;
55
        }
56
57
        if (!file_exists(app_path('Providers/'.self::PROVIDER_NAME.'.php'))) {
58
            throw new \Exception(self::PROVIDER_NAME.' not published.');
59
        }
60
61
        file_put_contents(app_path('Providers/'.self::PROVIDER_NAME.'.php'), str_replace(
62
            "namespace App\Providers;",
63
            "namespace {$namespace}\Providers;",
64
            file_get_contents(app_path('Providers/'.self::PROVIDER_NAME.'.php'))
65
        ));
66
67
        file_put_contents(config_path('app.php'), str_replace(
68
            "{$namespace}\\Providers\EventServiceProvider::class,".PHP_EOL,
69
            "{$namespace}\\Providers\EventServiceProvider::class,".PHP_EOL."        {$namespace}\Providers\\".self::PROVIDER_NAME.'::class,'.PHP_EOL,
70
            $appConfig
71
        ));
72
    }
73
}
74