Passed
Push — master ( efe7b1...d0dc7d )
by Iman
03:55
created

CbInstaller::seedDatabase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 9
rs 9.6666
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
        $ds = DIRECTORY_SEPARATOR;
79
        $vendorPath = public_path('vendor'.$ds.'crudbooster');
80
        $assetPath = base_path('vendor'.$ds.'crocodicstudio'.$ds.'crudbooster'.$ds.'src'.$ds.'assets');
81
82
        if (! file_exists($vendorPath)) {
83
            $this->console->info('Creating public/vendor/crudbooster symlink...');
84
            app('files')->link($assetPath, public_path('vendor/crudbooster'));
85
86
            return;
87
        }
88
89
        $this->console->info('Vendor Path: '.$vendorPath);
90
91
        if (realpath($vendorPath) == $vendorPath) {
92
            $this->console->info('Removing public/vendor/crudbooster dir, instead of creating a symlink...');
93
            $this->rrmdir($vendorPath);
94
            app('files')->link($assetPath, $vendorPath);
95
        }
96
    }
97
98
    public function installCrudbooster()
99
    {
100
        $this->publishFiles();
101
        $this->composerDumpAutoload();
102
        $this->migrateDatabase();
103
        $this->seedDatabase();
104
        $this->console->call('config:clear');
105
        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

105
        if (app()->/** @scrutinizer ignore-call */ version() < 5.6) {
Loading history...
106
            $this->console->call('optimize');
107
        }
108
109
        $this->console->info('Installing CRUDBooster Is Completed ! Thank You :)');
110
    }
111
112
113
    /**
114
     *
115
     * http://stackoverflow.com/questions/3338123/how-do-i-recursively-delete-a-directory-and-its-entire-contents-files-sub-dir
116
     */
117
118
    /**
119
     * Get the composer command for the environment.
120
     *
121
     * @return string
122
     */
123
    private function findComposer()
124
    {
125
        if (file_exists(getcwd().'/composer.phar')) {
126
            return '"'.PHP_BINARY.'" '.getcwd().'/composer.phar';
127
        }
128
129
        return 'composer';
130
    }
131
132
    private function publishFiles()
133
    {
134
        $this->console->info('Publishing CRUDBooster needs file...');
135
        $this->console->callSilent('vendor:publish', ['--provider' => 'crocodicstudio\\crudbooster\\CRUDBoosterServiceProvider', '--force' => true]);
136
        $this->console->callSilent('vendor:publish', ['--tag' => 'cb_migration', '--force' => true]);
137
        $this->console->callSilent('vendor:publish', ['--tag' => 'cb_lfm', '--force' => true]);
138
        $this->console->callSilent('vendor:publish', ['--tag' => 'cb_localization', '--force' => true]);
139
    }
140
141
    private function migrateDatabase()
142
    {
143
        $this->console->info('Migrating database...');
144
        $this->console->call('migrate', ['--path' => '\database\migrations\crudbooster']);
145
        $this->console->call('migrate');
146
    }
147
148
    private function seedDatabase()
149
    {
150
        //if (! class_exists('CBSeeder')) {
151
        //    $ds = DIRECTORY_SEPARATOR;
152
        //    require_once base_path('vendor'.$ds.'crocodicstudio'.$ds.'crudbooster'.$ds.'src'.$ds.'database'.$ds.'seeds'.$ds.'CBSeeder.php');
153
        //}
154
        $this->console->info('Please wait updating the data...');
155
        $this->console->callSilent('db:seed', ['--class' => 'CBSeeder']);
156
        $this->command->info('Updating the data completed !');
0 ignored issues
show
Bug Best Practice introduced by
The property command does not exist on crocodicstudio\crudboost...e\Installer\CbInstaller. Did you maybe forget to declare it?
Loading history...
157
    }
158
159
    private function composerDumpAutoload()
160
    {
161
        $this->console->info('Dumping the autoloaded files and reloading all new files...');
162
        $composer = $this->findComposer();
163
        $process = new Process($composer.' dumpautoload');
164
        $process->setWorkingDirectory(base_path())->run();
165
    }
166
}