Completed
Push — master ( da89f6...8759f3 )
by recca
01:28
created

Composer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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