Completed
Push — master ( 194cbe...2ee496 )
by Yaro
05:39 queued 19s
created

Install::copyNavigationView()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
namespace Yaro\Jarboe\Console\Commands;
4
5
use Illuminate\Console\Command;
6
use SplFileObject;
7
8
class Install extends Command
9
{
10
    /**
11
     * The name and signature of the console command.
12
     *
13
     * @var string
14
     */
15
    protected $signature = 'jarboe:install';
16
17
    /**
18
     * The console command description.
19
     *
20
     * @var string
21
     */
22
    protected $description = 'Configure package';
23
24
    /**
25
     * Execute the console command.
26
     *
27
     * @return mixed
28
     */
29
    public function handle()
30
    {
31
        $this->publishAssets();
32
        $this->publishConfigs();
33
        $this->copyNavigationView();
34
        $this->copyMigrationFiles();
35
        $this->copyThirdPartyMigrationFiles();
36
37
        if ($this->confirm("Add admin's guard and provider to auth config file?")) {
38
            $this->addAdminsGuardToConfigFile();
39
        }
40
41
        if ($this->confirm('Run migrations?')) {
42
            $this->call('migrate');
43
        }
44
45
        $this->info('Completed');
46
    }
47
48
    private function copyNavigationView()
49
    {
50
        $this->comment('Creating navigation view:');
51
        if ($this->isNavigationViewExists()) {
52
            $this->comment('  - already exists');
53
            return;
54
        }
55
56
        shell_exec(sprintf(
57
            'mkdir -p "%s" && cp "%s" "%s"',
58
            base_path('resources/views/vendor/jarboe/inc/'),
59
            base_path('vendor/yaro/jarboe/src/resources/views/inc/navigation.blade.php'),
60
            base_path('resources/views/vendor/jarboe/inc/navigation.blade.php')
61
        ));
62
    }
63
64
    private function isNavigationViewExists()
65
    {
66
        return file_exists(base_path('resources/views/vendor/jarboe/inc/navigation.blade.php'));
67
    }
68
69
    private function copyMigrationFiles()
70
    {
71
        $this->comment('Creating migration files:');
72
        if ($this->isMigrationFilesExist()) {
73
            $this->comment('  - already exists');
74
            return;
75
        }
76
77
        $name = date('Y_m_d_His') . '_create_admins_table.php';
78
        shell_exec(sprintf(
79
            'cp "%s" "%s"',
80
            base_path('vendor/yaro/jarboe/src/database/migrations/2018_06_28_152903_create_admins_table.php'),
81
            database_path('migrations/' . $name)
82
        ));
83
    }
84
85
    private function isMigrationFilesExist()
86
    {
87
        return (bool)glob(database_path('migrations/*_create_admins_table.php'));
88
    }
89
90
    private function copyThirdPartyMigrationFiles()
91
    {
92
        $this->comment('Publishing third-party migration files:');
93
        if ($this->isThirdPartyMigrationFilesExist()) {
94
            $this->comment('  - already exists');
95
            return;
96
        }
97
98
        $this->call('vendor:publish', [
99
            '--provider' => 'Spatie\Permission\PermissionServiceProvider',
100
            '--tag' => 'migrations',
101
        ]);
102
        $this->call('vendor:publish', [
103
            '--provider' => 'Spatie\Permission\PermissionServiceProvider',
104
            '--tag' => 'config',
105
        ]);
106
    }
107
108
    private function isThirdPartyMigrationFilesExist()
109
    {
110
        return (bool)glob(database_path('migrations/*_create_permission_tables.php'));
111
    }
112
113
    private function publishAssets()
114
    {
115
        $this->comment('Publishing public assets:');
116
        if ($this->isAssetsPublished()) {
117
            $this->comment('  - already exists');
118
            return;
119
        }
120
121
        $this->call('vendor:publish', [
122
            '--provider' => "Yaro\Jarboe\ServiceProvider",
123
            '--tag' => 'public',
124
            '--force' => true,
125
        ]);
126
    }
127
128
    private function isAssetsPublished()
129
    {
130
        return file_exists(public_path('vendor/jarboe'));
131
    }
132
133
    private function publishConfigs()
134
    {
135
        $this->comment('Publishing config files:');
136
        if ($this->isConfigsPublished()) {
137
            $this->comment('  - already exists');
138
            return;
139
        }
140
141
        $this->call('vendor:publish', [
142
            '--provider' => "Yaro\Jarboe\ServiceProvider",
143
            '--tag' => 'config',
144
        ]);
145
    }
146
147
    private function isConfigsPublished()
148
    {
149
        return file_exists(config_path('jarboe'));
150
    }
151
152
    private function addAdminsGuardToConfigFile()
153
    {
154
        $shouldAddGuardsSection = true;
155
        if (config('auth.guards.admin')) {
156
            $shouldAddGuardsSection = false;
157
            $this->comment('  - guard [admin] already exist');
158
        }
159
160
        $shouldAddProvidersSection = true;
161
        if (config('auth.guards.providers.admins')) {
162
            $shouldAddProvidersSection = false;
163
            $this->comment('  - provider [admins] already exist');
164
        }
165
166
        if (!$shouldAddGuardsSection && !$shouldAddProvidersSection) {
167
            return;
168
        }
169
170
        $configFile = config_path('auth.php');
171
172
        $output = '';
173
        $file = new SplFileObject($configFile, 'r');
174
        foreach ($file as $lineNumber => $line) {
175
            $output .= $line;
176
            if ($line == "    'guards' => [\n" && $shouldAddGuardsSection) {
177
                $output .= "        'admin' => [\n";
178
                $output .= "            'driver' => 'session',\n";
179
                $output .= "            'provider' => 'admins',\n";
180
                $output .= "        ],\n\n";
181
            }
182
183
            if ($line == "    'providers' => [\n" && $shouldAddProvidersSection) {
184
                $output .= "        'admins' => [\n";
185
                $output .= "            'driver' => 'eloquent',\n";
186
                $output .= "            'model' => \Yaro\Jarboe\Models\Admin::class,\n";
187
                $output .= "        ],\n\n";
188
            }
189
        }
190
191
        $file = new SplFileObject($configFile, 'w+');
192
        $file->fwrite($output);
193
    }
194
}
195