Passed
Push — master ( 6429db...ea3910 )
by Iman
04:24
created

CbInstaller::symlinkForAsset()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
nop 0
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace crocodicstudio\crudbooster\CBCoreModule\Installer;
4
5
use Symfony\Component\Process\Process;
6
7
class CbInstaller
8
{
9
    private $console;
10
11
    /**
12
     * ConsolePrinter constructor.
13
     *
14
     * @param $console
15
     */
16
    public function __construct($console)
17
    {
18
        $this->console = $console;
19
    }
20
21
    public function removeDefaultMigrations()
22
    {
23
        $this->console->info('I remove some default migration files from laravel...');
24
        @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

24
        /** @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...
25
        @unlink(base_path('database/migrations/2014_10_12_100000_create_password_resets_table.php'));
26
    }
27
28
    public function createVendorAtPublic()
29
    {
30
        $this->console->info('Checking public/vendor directory...');
31
        if (! file_exists(public_path('vendor'))) {
32
            mkdir(public_path('vendor'), 0777);
33
        }
34
35
        if (! is_writable(public_path('vendor'))) {
36
            $this->console->info('Setup aborted !');
37
            $this->console->info('Please set public/vendor directory to writable 0777');
38
            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...
39
        }
40
    }
41
42
    public function symlinkForUpload()
43
    {
44
        $this->console->info('Checking public/uploads symlink...');
45
        if (! file_exists(public_path('uploads'))) {
46
            $this->console->info('Creating public/uploads symlink...');
47
            app('files')->link(storage_path('app'), public_path('uploads'));
48
49
            return;
50
        }
51
        $uploadPath = public_path('uploads');
52
        $this->console->info('Upload Path: '.$uploadPath);
53
        if (realpath($uploadPath) == $uploadPath) {
54
            $this->console->info('Remove the existing uploads dir, and create a symlink for it...');
55
            $this->rrmdir(public_path('uploads'));
56
            app('files')->link(storage_path('app'), public_path('uploads'));
57
        }
58
    }
59
60
    private function rrmdir($dir)
61
    {
62
        if (! is_dir($dir)) {
63
            return;
64
        }
65
        foreach (scandir($dir) as $object) {
66
            if (in_array($object, ['.', '..'])) {
67
                continue;
68
            }
69
70
            $objPath = $dir."/".$object;
71
72
            if (is_dir($objPath)) {
73
                $this->rrmdir($objPath);
74
            } else {
75
                unlink($objPath);
76
            }
77
        }
78
        rmdir($dir);
79
    }
80
81
    public function symlinkForAsset()
82
    {
83
        $this->console->info('Checking public/vendor/crudbooster symlink...');
84
85
        $vendorPath = public_path('vendor'.DIRECTORY_SEPARATOR.'crudbooster');
86
87
        if (! file_exists($vendorPath)) {
88
            $this->console->info('Creating public/vendor/crudbooster symlink...');
89
            app('files')->link(__DIR__.'/../assets', public_path('vendor/crudbooster'));
90
91
            return;
92
        }
93
94
        $this->console->info('Vendor Path: '.$vendorPath);
95
96
        if (realpath($vendorPath) == $vendorPath) {
97
            $this->console->info('Removing public/vendor/crudbooster dir, instead of creating a symlink...');
98
            $this->rrmdir($vendorPath);
99
            app('files')->link(__DIR__.'/../assets', $vendorPath);
100
        }
101
    }
102
103
    public function installCrudbooster()
104
    {
105
        $this->console->info('Publishing CRUDBooster needs file...');
106
        $this->console->callSilent('vendor:publish', ['--provider' => 'crocodicstudio\\crudbooster\\CRUDBoosterServiceProvider', '--force' => true]);
107
        $this->console->callSilent('vendor:publish', ['--tag' => 'cb_migration', '--force' => true]);
108
        $this->console->callSilent('vendor:publish', ['--tag' => 'cb_lfm', '--force' => true]);
109
        $this->console->callSilent('vendor:publish', ['--tag' => 'cb_localization', '--force' => true]);
110
111
        $this->console->info('Dumping the autoloaded files and reloading all new files...');
112
        $composer = $this->findComposer();
113
        $process = new Process($composer.' dumpautoload');
114
        $process->setWorkingDirectory(base_path())->run();
115
116
        $this->console->info('Migrating database...');
117
        $this->console->call('migrate', ['--path' => '\database\migrations\crudbooster']);
118
119
        if (! class_exists('CBSeeder')) {
120
            require_once __DIR__.'/../database/seeds/CBSeeder.php';
121
        }
122
        $this->console->callSilent('db:seed', ['--class' => 'CBSeeder']);
123
        $this->console->call('config:clear');
124
        if (app()->version() < 5.6) {
0 ignored issues
show
introduced by
The method version() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

124
        if (app()->/** @scrutinizer ignore-call */ version() < 5.6) {
Loading history...
125
            $this->console->call('optimize');
126
        }
127
128
        $this->console->info('Installing CRUDBooster Is Completed ! Thank You :)');
129
    }
130
131
132
    /*
133
    * http://stackoverflow.com/questions/3338123/how-do-i-recursively-delete-a-directory-and-its-entire-contents-files-sub-dir
134
    */
135
136
    /**
137
     * Get the composer command for the environment.
138
     *
139
     * @return string
140
     */
141
    protected function findComposer()
142
    {
143
        if (file_exists(getcwd().'/composer.phar')) {
144
            return '"'.PHP_BINARY.'" '.getcwd().'/composer.phar';
145
        }
146
147
        return 'composer';
148
    }
149
}