Completed
Push — master ( 7eba8f...c52d76 )
by Iman
18s queued 10s
created

CrudboosterInstallationCommand::printHeader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
nc 1
nop 0
dl 0
loc 12
c 0
b 0
f 0
cc 1
rs 9.4285
1
<?php namespace crocodicstudio\crudbooster\commands;
2
3
use Illuminate\Console\Command;
4
use Illuminate\Foundation\Inspiring;
5
use Symfony\Component\Console\Input\InputOption;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Process\Process;
8
use DB;
9
use Cache;
10
use Request;
11
use CRUDBooster;
0 ignored issues
show
Bug introduced by
The type CRUDBooster was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use App;
13
14
class CrudboosterInstallationCommand extends Command
15
{
16
    /**
17
     * The console command name.
18
     *
19
     * @var string
20
     */
21
    protected $name = 'crudbooster:install';
22
23
    /**
24
     * The console command description.
25
     *
26
     * @var string
27
     */
28
    protected $description = 'CRUDBooster Installation Command';
29
30
    /**
31
     * Execute the console command.
32
     *
33
     * @return mixed
34
     */
35
    public function handle()
36
    {
37
        $printer = new ConsolePrinter();
38
39
        $printer->printHeader();
40
41
        $passes = (new RequirementChecker())->check();
42
        if(!$passes) {
43
            $this->info('Sorry unfortunately your system is not meet with our requirements !');
44
            $this->printFooter(false);
0 ignored issues
show
Bug introduced by
The method printFooter() does not exist on crocodicstudio\crudboost...sterInstallationCommand. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

44
            $this->/** @scrutinizer ignore-call */ 
45
                   printFooter(false);
Loading history...
45
            $this->info('--');
46
            exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
47
        }
48
49
        $this->info('Installing: ');
50
        /* Removing the default user and password reset, it makes you ambigous when using CRUDBooster */
51
        $this->removeDefaultMigrations();
52
53
        //Create vendor folder at public
54
        $this->createVendorAtPublic();
55
56
        //Create symlink for uploads path
57
        $this->symlinkForUpload();
58
59
        //Crate symlink for assets
60
        $this->symlinkForAsset();
61
      
62
63
		if($this->confirm('Do you have setting the database configuration at .env ?')) {
64
            $this->installCrudbooster();
65
		}else{
66
			$this->info('Setup Aborted !');
67
			$this->info('Please setting the database configuration for first !');
68
		}
69
70
        $printer->printFooter();
71
        exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
72
	}
73
74
    /**
75
     * Get the composer command for the environment.
76
     *
77
     * @return string
78
     */
79
    protected function findComposer()
80
    {
81
        if (file_exists(getcwd().'/composer.phar')) {
82
            return '"'.PHP_BINARY.'" '.getcwd().'/composer.phar';
83
        }
84
85
        return 'composer';
86
    }
87
88
    private function symlinkForUpload()
89
    {
90
        $this->info('Checking public/uploads symlink...');
91
        if (!file_exists(public_path('uploads'))) {
92
            $this->info('Creating public/uploads symlink...');
93
            app('files')->link(storage_path('app'), public_path('uploads'));
94
            return;
95
        }
96
        $uploadPath = public_path('uploads');
97
        $this->info('Upload Path: '.$uploadPath);
98
        if (realpath($uploadPath) == $uploadPath) {
99
            $this->info('Remove the existing uploads dir, and create a symlink for it...');
100
            $this->rrmdir(public_path('uploads'));
101
            app('files')->link(storage_path('app'), public_path('uploads'));
102
        }
103
    }
104
105
    private function symlinkForAsset()
106
    {
107
        $this->info('Checking public/vendor/crudbooster symlink...');
108
109
110
        $vendorPath = public_path('vendor'.DIRECTORY_SEPARATOR.'crudbooster');
111
112
        if (!file_exists($vendorPath)) {
113
            $this->info('Creating public/vendor/crudbooster symlink...');
114
            app('files')->link(__DIR__.'/../assets', public_path('vendor/crudbooster'));
115
            return ;
116
        }
117
118
        $this->info('Vendor Path: '.$vendorPath);
119
120
        if (realpath($vendorPath) == $vendorPath) {
121
            $this->info('Removing public/vendor/crudbooster dir, instead of creating a symlink...');
122
            $this->rrmdir($vendorPath);
123
            app('files')->link(__DIR__.'/../assets', $vendorPath);
124
        }
125
    }
126
127
    private function removeDefaultMigrations()
128
    {
129
        $this->info('I remove some default migration files from laravel...');
130
        @unlink(base_path('database/migrations/2014_10_12_000000_create_users_table.php'));
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

130
        /** @scrutinizer ignore-unhandled */ @unlink(base_path('database/migrations/2014_10_12_000000_create_users_table.php'));

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
131
        @unlink(base_path('database/migrations/2014_10_12_100000_create_password_resets_table.php'));
132
    }
133
134
    private function installCrudbooster()
135
    {
136
        $this->info('Publishing CRUDBooster needs file...');
137
        $this->callSilent('vendor:publish', ['--provider' => 'crocodicstudio\\crudbooster\\CRUDBoosterServiceProvider', '--force' => true]);
138
        $this->callSilent('vendor:publish', ['--tag' => 'cb_migration', '--force' => true]);
139
        $this->callSilent('vendor:publish', ['--tag' => 'cb_lfm', '--force' => true]);
140
        $this->callSilent('vendor:publish', ['--tag' => 'cb_localization', '--force' => true]);
141
142
        $this->info('Dumping the autoloaded files and reloading all new files...');
143
        $composer = $this->findComposer();
144
        $process = new Process($composer.' dumpautoload');
145
        $process->setWorkingDirectory(base_path())->run();
146
147
        $this->info('Migrating database...');
148
        $this->call('migrate', ['--path'=> '\database\migrations\crudbooster']);
149
150
        if (! class_exists('CBSeeder')) {
151
            require_once __DIR__.'/../database/seeds/CBSeeder.php';
152
        }
153
        $this->callSilent('db:seed', ['--class' => 'CBSeeder']);
154
        $this->call('config:clear');
155
        $this->call('optimize');
156
157
        $this->info('Installing CRUDBooster Is Completed ! Thank You :)');
158
    }
159
160
    private function createVendorAtPublic()
161
    {
162
        $this->info('Checking public/vendor directory...');
163
        if (! file_exists(public_path('vendor'))) {
164
            mkdir(public_path('vendor'), 0777);
165
        }
166
167
        if (! is_writable(public_path('vendor'))) {
168
            $this->info('Setup aborted !');
169
            $this->info('Please set public/vendor directory to writable 0777');
170
            exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
171
        }
172
    }
173
    /*
174
    * http://stackoverflow.com/questions/3338123/how-do-i-recursively-delete-a-directory-and-its-entire-contents-files-sub-dir
175
    */
176
    private function rrmdir($dir)
177
    {
178
        if (! is_dir($dir)) {
179
            return;
180
        }
181
        foreach (scandir($dir) as $object) {
182
            if (in_array($object, ['.', '..'])) {
183
                continue;
184
            }
185
186
            $objPath = $dir."/".$object;
187
188
            if (is_dir($objPath)) {
189
                $this->rrmdir($objPath);
190
            } else {
191
                unlink($objPath);
192
            }
193
        }
194
        rmdir($dir);
195
    }
196
}
197