Completed
Pull Request — experimental/3.1 (#2640)
by
unknown
46:21
created

ComposerProcessService::getPHP()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
namespace Eccube\Service\Composer;
24
25
use Doctrine\ORM\EntityManagerInterface;
26
use Eccube\Annotation\Service;
27
28
/**
29
 * Class ComposerProcessService
30
 * @package Eccube\Service\Composer
31
 * @Service
32
 */
33
class ComposerProcessService implements ComposerServiceInterface
34
{
35
    /**
36
     * @var array
37
     */
38
    protected $appConfig;
39
40
    /**
41
     * @var EntityManagerInterface
42
     */
43
    protected $entityManager;
44
45
    private $workingDir;
46
    private $composerFile;
47
    private $composerSetup;
48
    private $pathPHP;
49
50
    public function __construct($appConfig, $entityManager, $pathPHP)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
51
    {
52
        $this->appConfig = $appConfig;
53
        $this->entityManager = $entityManager;
54
        $this->pathPHP = $pathPHP;
55
    }
56
57
    /**
58
     * This function to install a plugin by composer require
59
     *
60
     * @param string $packageName format foo/bar or foo/bar:1.0.0 or "foo/bar 1.0.0"
61
     * @return bool
62
     */
63 View Code Duplication
    public function execRequire($packageName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
    {
65
        set_time_limit(0);
66
        if(false === $this->init()){
67
            return false;
68
        }
69
        // Build command
70
        $command = $this->pathPHP.' '.$this->composerFile.' require '.$packageName;
71
        $command .= ' --prefer-dist --no-progress --no-suggest --no-scripts --ignore-platform-reqs --profile --no-ansi --no-interaction -d ';
72
        $command .= $this->workingDir.' 2>&1';
73
        log_info($command);
74
        $this->runCommand($command);
75
76
        return true;
77
    }
78
79
    /**
80
     * This function to remove a plugin by composer remove
81
     * Note: Remove with dependency, if not, please add " --no-update-with-dependencies"
82
     *
83
     * @param string $packageName format foo/bar or foo/bar:1.0.0 or "foo/bar 1.0.0"
84
     * @return bool
85
     */
86 View Code Duplication
    public function execRemove($packageName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
    {
88
        set_time_limit(0);
89
        if(false === $this->init()){
90
            return false;
91
        }
92
        // Build command
93
        $command = $this->pathPHP.' '.$this->composerFile.' remove '.$packageName;
94
        $command .= ' --no-progress --no-scripts --ignore-platform-reqs --profile --no-ansi --no-interaction --no-update-with-dependencies -d ';
95
        $command .= $this->workingDir.' 2>&1';
96
        log_info($command);
97
98
        // Execute command
99
        $this->runCommand($command);
100
101
        return true;
102
    }
103
104
    /**
105
     * Run command
106
     *
107
     * @param string $command
108
     * @return void
109
     */
110
    public function runCommand($command)
111
    {
112
        // Execute command
113
        $output = array();
114
        exec($command, $output);
115
        log_info(PHP_EOL.implode(PHP_EOL, $output).PHP_EOL);
116
    }
117
118
    /**
119
     * Set working dir
120
     * @param string $workingDir
121
     */
122
    public function setWorkingDir($workingDir)
123
    {
124
        $this->workingDir = $workingDir;
125
    }
126
127
    /**
128
     * Set init
129
     */
130
    private function init()
131
    {
132
        if (!$this->isPhpCommandLine()) {
133
            return false;
134
        }
135
136
        if (!$this->isSetCliMemoryLimit()) {
137
            $composerMemory = $this->appConfig['composer_memory_limit'];
138
            if ($this->getCliMemoryLimit() < $composerMemory && $this->getCliMemoryLimit() != -1) {
139
                return false;
140
            }
141
        }
142
143
        $em = $this->entityManager;
144
        if ($em->getConnection()->isTransactionActive()) {
145
            $em->getConnection()->commit();
146
            $em->getConnection()->beginTransaction();
147
        }
148
149
        @ini_set('memory_limit', '1536M');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
150
        // Config for some environment
151
        putenv('COMPOSER_HOME='.$this->appConfig['plugin_realdir'].'/.composer');
152
        $this->workingDir = $this->workingDir ? $this->workingDir : $this->appConfig['root_dir'];
153
        $this->setupComposer();
154
    }
155
156
    /**
157
     * Check composer file and setup it
158
     */
159
    private function setupComposer()
160
    {
161
        $this->composerFile = $this->workingDir.'/composer.phar';
162
        $this->composerSetup = $this->workingDir.'/composer-setup.php';
163
        if (!file_exists($this->composerFile)) {
164
            if (!file_exists($this->composerSetup)) {
165
                $result = copy('https://getcomposer.org/installer', $this->composerSetup);
166
                log_info($this->composerSetup.' : '.$result);
167
            }
168
            $command = $this->pathPHP.' '.$this->composerSetup;
169
            $this->runCommand($command);
170
171
            unlink($this->composerSetup);
172
        }
173
    }
174
175
    /**
176
     * Get grep memory_limit | Megabyte
177
     * @return int|string
178
     */
179
    public function getCliMemoryLimit(){
0 ignored issues
show
introduced by
Declare public methods first, then protected ones and finally private ones
Loading history...
180
        $grepMemory = exec($this->pathPHP.' -i | grep "memory_limit"');
181
        if($grepMemory){
182
            $grepMemory = explode('=>', $grepMemory);
183
184
            // -1 unlimited
185
            if (trim($grepMemory[2]) == -1) {
186
                return -1;
187
            }
188
189
            $exp = preg_split('#(?<=\d)(?=[a-z])#i', $grepMemory[2]);
190
            $memo = trim($exp[0]);
191
            if ($exp[1] == 'M') {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
192
193
                return $memo;
194
            } else {
195
                if ($exp[1] == 'GB') {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
196
197
                    return $memo * 1024;
198
                } else {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
199
200
                    return 0;
201
                }
202
            }
203
        }
204
205
        return 0;
206
    }
207
208
    /**
209
     * Check to set new value grep "memory_limit"
210
     * @return bool
211
     */
212
    public function isSetCliMemoryLimit()
213
    {
214
        $oldMemory = exec($this->pathPHP.' -i | grep "memory_limit"');
215
        $tmpMem = '1.5GB';
216
217
        if ($oldMemory) {
218
            $memory = explode('=>', $oldMemory);
219
            $originGrepMemmory = trim($memory[2]);
220
221
            if ($originGrepMemmory == $tmpMem) {
222
                $tmpMem = '1.49GB';
223
            }
224
225
            $newMemory = exec($this->pathPHP.' -d memory_limit='.$tmpMem.' -i | grep "memory_limit"');
226
            if ($newMemory) {
227
                $newMemory = explode('=>', $newMemory);
228
                $grepNewMemory = trim($newMemory[2]);
229
                if ($grepNewMemory != $originGrepMemmory) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
230
231
                    return true;
232
                }
233
            }
234
        }
235
236
        return false;
237
    }
238
239
    /**
240
     * Check php command line
241
     * @return bool
242
     */
243
    public function isPhpCommandLine()
244
    {
245
        $php = exec('which php');
246
        if (function_exists('exec') && null != $php) {
247
            if (strpos(strtolower($php), 'php') !== false) {
248
                return true;
249
            }
250
        }
251
252
        return false;
253
    }
254
}
255