ActivateModuleCommand::checkPrerequisites()   A
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 30
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
c 0
b 0
f 0
dl 0
loc 30
rs 9.2222
cc 6
nc 4
nop 1
1
<?php
2
3
namespace App\Console\Commands;
4
5
use Nwidart\Modules\Facades\Module;
6
use PragmaRX\Version\Package\Version;
7
8
use Illuminate\Console\Command;
9
use Illuminate\Support\Facades\Log;
10
use Illuminate\Support\Facades\Storage;
11
12
class ActivateModuleCommand extends Command
13
{
14
    protected $signature   = 'tb_module:activate {module}';
15
    protected $description = 'Activate a new Tech Bench Add On Module';
16
17
    protected $module;
18
19
    /**
20
     * Create a new command instance
21
     */
22
    public function __construct()
23
    {
24
        parent::__construct();
25
    }
26
27
    /**
28
     * Execute the console command
29
     */
30
    public function handle()
31
    {
32
        $this->newLine(2);
33
34
        Log::notice('Activate Module Command running for './** @scrutinizer ignore-type */$this->argument('module'));
35
36
        //  Verify that the module is not already installed
37
        $activeModules = Module::allEnabled();
38
        foreach($activeModules as $module)
39
        {
40
            if($module->getName() === $this->argument('module'));
41
            {
42
                $this->error('The '.$this->argument('module').' is already active');
43
                Log::critical('The '.$this->argument('module').' is already active');
44
                return 1;
45
            }
46
        }
47
48
        $this->line('Activating Module '.$this->argument('module'));
49
        $this->newLine(2);
50
51
        //  Verify that the module exists and is valid
52
        $module = $this->findModule($this->argument('module'));
53
        if(!$module)
54
        {
55
            $this->error('Unable to find module - '.$this->argument('module'));
56
            $this->error('Please make sure that this is a proper Tech Bench Module and loaded into the Modules directory');
57
            Log::critical('Unable to find module - '.$this->argument('module'));
58
            return 1;
59
        }
60
61
        $this->line('Found Module.  Checking Prerequisites');
62
63
        //  Verify that the correct version of Tech Bench is running for this module
64
        if(!$this->checkPrerequisites($module->getRequires()))
65
        {
66
            $this->error('Tech Bench does not meet the prerequisites for installing this module');
67
            $this->error('Please install the missing requirements and try again');
68
            Log::critical('Tech Bench does not meet the prerequisites for installing this module');
69
            Log::critical('Please install the missing requirements and try again');
70
            return 1;
71
        }
72
73
        $this->line('Prerequisites passed.  Activating Module');
74
75
        //  Enable the module
76
        $this->call('module:enable', ['module' => $module->getStudlyName()]);
77
78
        //  Run any migrations
79
        $this->line('Setting up database');
80
        $this->call('module:migrate', ['module' => $module->getStudlyName()]);
81
82
        //  Install NPM and Composer packages
83
        $this->line('Copying Files');
84
        shell_exec('cd '.base_path().DIRECTORY_SEPARATOR.'Modules'.DIRECTORY_SEPARATOR.$module->getStudlyName().' && composer install --no-dev --no-interaction --optimize-autoloader');
85
        shell_exec('cd '.base_path().DIRECTORY_SEPARATOR.'Modules'.DIRECTORY_SEPARATOR.$module->getStudlyName().' && npm install --silent --only=production');
86
87
        //  Combine the new Javascript files
88
        $this->line('Creating Javascript files (this may take some time)');
89
        shell_exec('cd '.base_path().DIRECTORY_SEPARATOR.'Modules'.DIRECTORY_SEPARATOR.$module->getStudlyName().' && npm run production');
90
        shell_exec('cd '.base_path().DIRECTORY_SEPARATOR.' && npm run production');
91
92
        $this->info('Module has been activated');
93
        Log::notice('Module '.$this->argument('module').' has been activated');
94
95
        return Command::SUCCESS;
96
    }
97
98
    /**
99
     * Verify that the Module Files are in place and that this is a Tech Bench Module
100
     */
101
    protected function findModule($name)
102
    {
103
        $basePath = $name;
104
105
        //  Verify that the module files exists
106
        $moduleData = Storage::disk('modules')->allFiles($basePath);
107
        if(empty($moduleData))
108
        {
109
            Log::critical('No data returned while searching for folder '.$name);
110
            return false;
111
        }
112
113
        $module   = Module::find($name);
114
        $require  = $module->getRequires();
115
        $config   = Storage::disk('modules')->get($basePath.'/Config/config.php');
116
117
        if(!$module || !$config || !isset($require['Tech_Bench']))
118
        {
119
            Log::critical('Key files missing from the Module Directory', [
120
                'module_file' => $module,
121
                'config_file' => $config,
122
            ]);
123
            return false;
124
        }
125
126
        return $module;
127
    }
128
129
    /**
130
     * Verify the current version of Tech Bench meets the minimum requirements, and any other required modules are installed
131
     */
132
    protected function checkPrerequisites($requires)
133
    {
134
        $failed = false;
135
136
        //  Verify version
137
        $curVersion = (new Version)->compact();
138
        if($curVersion < $requires['Tech_Bench'])
139
        {
140
            $failed = true;
141
            $this->error('Tech Bench must be running Version '.$requires['Tech_Bench'].' or higher to use this module');
142
            $this->newLine();
143
            Log::critical('Tech Bench must be running Version '.$requires['Tech_Bench'].' or higher to use this module');
144
        }
145
146
        //  Check for other required modules
147
        if(isset($requires['Modules']) && count($requires['Modules']) > 0)
148
        {
149
            foreach($requires['Modules'] as $req)
150
            {
151
                $found = Module::find($req);
152
                if(!$found)
153
                {
154
                    $failed = true;
155
                    $this->error('The '.$req.' module is missing and must be installed to use this module');
156
                    Log::critical('The '.$req.'module is missing and must be installed to use this module');
157
                }
158
            }
159
        }
160
161
        return !$failed;
162
    }
163
}
164