Composer   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 136
ccs 0
cts 58
cp 0
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 4

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A handle() 0 15 2
A install() 0 20 4
A getOptions() 0 6 1
B init() 0 40 8
1
<?php
2
3
namespace Recca0120\Terminal\Console\Commands;
4
5
use Illuminate\Filesystem\Filesystem;
6
use Phar;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Input\StringInput;
9
10
class Composer extends Command
11
{
12
    /**
13
     * The console command name.
14
     *
15
     * @var string
16
     */
17
    protected $name = 'composer';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'composer';
25
26
    /**
27
     * $files.
28
     *
29
     * @var \Illuminate\Filesystem\Filesystem
30
     */
31
    protected $files;
32
33
    /**
34
     * __construct.
35
     *
36
     * @param \Illuminate\Filesystem\Filesystem $files
37
     */
38
    public function __construct(Filesystem $files)
39
    {
40
        parent::__construct();
41
42
        $this->files = $files;
43
    }
44
45
    /**
46
     * Handle the command.
47
     *
48
     * @throws \InvalidArgumentException
49
     */
50
    public function handle()
51
    {
52
        $this->install();
53
54
        $command = trim($this->option('command'));
55
        if (empty($command) === true) {
56
            $command = 'help';
57
        }
58
59
        $input = new StringInput($command);
60
        $output = $this->getOutput();
61
        $application = new \Composer\Console\Application();
62
        $application->setAutoExit(false);
63
        $application->run($input, $output);
64
    }
65
66
    /**
67
     * install.
68
     */
69
    protected function install()
70
    {
71
        $storagePath = $this->getLaravel()->storagePath();
72
        $composerPath = $storagePath.'/app/composer/';
73
        if ($this->files->exists($composerPath.'vendor/autoload.php') === false) {
74
            if ($this->files->isDirectory($composerPath) === false) {
75
                $this->files->makeDirectory($composerPath, 0777);
76
            }
77
            $this->files->put($composerPath.'composer.phar', file_get_contents('https://getcomposer.org/composer.phar'));
78
            $composerPhar = new Phar($composerPath.'composer.phar');
79
            $composerPhar->extractTo($composerPath);
80
            unset($composerPhar);
81
            $this->files->delete($composerPath.'composer.phar');
82
        }
83
        if (empty(getenv('COMPOSER_HOME')) === true) {
84
            putenv('COMPOSER_HOME='.$composerPath);
85
        }
86
        $this->files->getRequire($composerPath.'vendor/autoload.php');
87
        $this->init();
88
    }
89
90
    /**
91
     * init.
92
     */
93
    protected function init()
94
    {
95
        error_reporting(-1);
96
97
        // $xdebug = new \Composer\XdebugHandler($this->getOutput());
98
        // $xdebug->check();
99
        // unset($xdebug);
100
        if (function_exists('ini_set')) {
101
            @ini_set('display_errors', 1);
102
            $memoryInBytes = function ($value) {
103
                $unit = strtolower(substr($value, -1, 1));
104
                $value = (int) $value;
105
                switch ($unit) {
106
                    case 'g':
107
                        $value *= 1024;
108
                        // no break (cumulative multiplier)
109
                    case 'm':
110
                        $value *= 1024;
111
                        // no break (cumulative multiplier)
112
                    case 'k':
113
                        $value *= 1024;
114
                }
115
116
                return $value;
117
            };
118
            $memoryLimit = trim(ini_get('memory_limit'));
119
            // Increase memory_limit if it is lower than 1GB
120
            if ($memoryLimit != -1 && $memoryInBytes($memoryLimit) < 1024 * 1024 * 1024) {
121
                @ini_set('memory_limit', '1G');
122
            }
123
            unset($memoryInBytes, $memoryLimit);
124
        }
125
126
        if (defined('STDIN') === false) {
127
            define('STDIN', fopen('php://stdin', 'r'));
128
        }
129
130
        $basePath = $this->getLaravel()->basePath();
131
        chdir($basePath);
132
    }
133
134
    /**
135
     * Get the console command options.
136
     *
137
     * @return array
138
     */
139
    protected function getOptions()
140
    {
141
        return [
142
            ['command', null, InputOption::VALUE_OPTIONAL],
143
        ];
144
    }
145
}
146