Passed
Push — master ( e666cf...efe7b1 )
by Iman
07:49
created

CbInstaller::publishFiles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
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 createVendorAtPublic()
22
    {
23
        $this->console->info('Checking public/vendor directory...');
24
        if (! file_exists(public_path('vendor'))) {
25
            mkdir(public_path('vendor'), 0777);
26
        }
27
28
        if (! is_writable(public_path('vendor'))) {
29
            $this->console->info('Setup aborted !');
30
            $this->console->info('Please set public/vendor directory to writable 0777');
31
            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...
32
        }
33
    }
34
35
    public function symlinkForUpload()
36
    {
37
        $this->console->info('Checking public/uploads symlink...');
38
        if (! file_exists(public_path('uploads'))) {
39
            $this->console->info('Creating public/uploads symlink...');
40
            app('files')->link(storage_path('app'), public_path('uploads'));
41
42
            return;
43
        }
44
        $uploadPath = public_path('uploads');
45
        $this->console->info('Upload Path: '.$uploadPath);
46
        if (realpath($uploadPath) == $uploadPath) {
47
            $this->console->info('Remove the existing uploads dir, and create a symlink for it...');
48
            $this->rrmdir(public_path('uploads'));
49
            app('files')->link(storage_path('app'), public_path('uploads'));
50
        }
51
    }
52
53
    private function rrmdir($dir)
54
    {
55
        if (! is_dir($dir)) {
56
            return;
57
        }
58
        foreach (scandir($dir) as $object) {
59
            if (in_array($object, ['.', '..'])) {
60
                continue;
61
            }
62
63
            $objPath = $dir."/".$object;
64
65
            if (is_dir($objPath)) {
66
                $this->rrmdir($objPath);
67
            } else {
68
                unlink($objPath);
69
            }
70
        }
71
        rmdir($dir);
72
    }
73
74
    public function symlinkForAsset()
75
    {
76
        $this->console->info('Checking public/vendor/crudbooster symlink...');
77
78
        $vendorPath = public_path('vendor'.DIRECTORY_SEPARATOR.'crudbooster');
79
        $ds = DIRECTORY_SEPARATOR;
80
        if (! file_exists($vendorPath)) {
81
            $this->console->info('Creating public/vendor/crudbooster symlink...');
82
            app('files')->link(base_path('vendor'.$ds.'crocodicstudio'.$ds.'crudbooster'.$ds.'src'.$ds.'assets'), public_path('vendor/crudbooster'));
83
84
            return;
85
        }
86
87
        $this->console->info('Vendor Path: '.$vendorPath);
88
89
        if (realpath($vendorPath) == $vendorPath) {
90
            $this->console->info('Removing public/vendor/crudbooster dir, instead of creating a symlink...');
91
            $this->rrmdir($vendorPath);
92
            app('files')->link(base_path('vendor'.$ds.'crocodicstudio'.$ds.'crudbooster'.$ds.'src'.$ds.'assets'), $vendorPath);
93
        }
94
    }
95
96
    public function installCrudbooster()
97
    {
98
        $this->publishFiles();
99
100
        $this->console->info('Dumping the autoloaded files and reloading all new files...');
101
        $composer = $this->findComposer();
102
        $process = new Process($composer.' dumpautoload');
103
        $process->setWorkingDirectory(base_path())->run();
104
105
        $this->migrateDatabase();
106
107
        if (! class_exists('CBSeeder')) {
108
            $ds = DIRECTORY_SEPARATOR;
109
            require_once base_path('vendor'.$ds.'crocodicstudio'.$ds.'crudbooster'.$ds.'src'.$ds.'database'.$ds.'seeds'.$ds.'CBSeeder.php');
110
        }
111
        $this->console->callSilent('db:seed', ['--class' => 'CBSeeder']);
112
        $this->console->call('config:clear');
113
        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

113
        if (app()->/** @scrutinizer ignore-call */ version() < 5.6) {
Loading history...
114
            $this->console->call('optimize');
115
        }
116
117
        $this->console->info('Installing CRUDBooster Is Completed ! Thank You :)');
118
    }
119
120
121
    /**
122
     *
123
     * http://stackoverflow.com/questions/3338123/how-do-i-recursively-delete-a-directory-and-its-entire-contents-files-sub-dir
124
     */
125
126
    /**
127
     * Get the composer command for the environment.
128
     *
129
     * @return string
130
     */
131
    protected function findComposer()
132
    {
133
        if (file_exists(getcwd().'/composer.phar')) {
134
            return '"'.PHP_BINARY.'" '.getcwd().'/composer.phar';
135
        }
136
137
        return 'composer';
138
    }
139
140
    private function publishFiles()
141
    {
142
        $this->console->info('Publishing CRUDBooster needs file...');
143
        $this->console->callSilent('vendor:publish', ['--provider' => 'crocodicstudio\\crudbooster\\CRUDBoosterServiceProvider', '--force' => true]);
144
        $this->console->callSilent('vendor:publish', ['--tag' => 'cb_migration', '--force' => true]);
145
        $this->console->callSilent('vendor:publish', ['--tag' => 'cb_lfm', '--force' => true]);
146
        $this->console->callSilent('vendor:publish', ['--tag' => 'cb_localization', '--force' => true]);
147
    }
148
149
    private function migrateDatabase()
150
    {
151
        $this->console->info('Migrating database...');
152
        $this->console->call('migrate', ['--path' => '\database\migrations\crudbooster']);
153
        $this->console->call('migrate');
154
    }
155
}