Failed Conditions
Push — experimental/3.1 ( fc835a...c7d2b5 )
by chihiro
251:51 queued 244:35
created

ComposerApiService::runCommand()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 13
nc 2
nop 1
dl 0
loc 19
rs 9.4285
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 Composer\Console\Application;
26
use Eccube\Annotation\Inject;
27
use Eccube\Annotation\Service;
28
use Symfony\Component\Console\Input\ArrayInput;
29
use Symfony\Component\Console\Output\BufferedOutput;
30
31
/**
32
 * Class ComposerApiService
33
 * @package Eccube\Service\Composer
34
 * @Service
35
 */
36
class ComposerApiService implements ComposerServiceInterface
37
{
38
    /**
39
     * @Inject("config")
40
     * @var array
41
     */
42
    protected $appConfig;
43
44
    /**
45
     * @var Application $consoleApplication
46
     */
47
    private $consoleApplication;
48
49
    private $workingDir;
50
51
    /**
52
     * Run get info command
53
     *
54
     * @param string $pluginName format foo/bar or foo/bar:1.0.0 or "foo/bar 1.0.0"
55
     * @return array
56
     */
57
    public function execInfo($pluginName)
58
    {
59
        $output = $this->runCommand(array(
60
            'command' => 'info',
61
            'package' => $pluginName,
62
        ));
63
64
        return OutputParser::parseInfo($output);
65
    }
66
67
    /**
68
     * Run execute command
69
     *
70
     * @param string $packageName format "foo/bar foo/bar:1.0.0"
71
     * @return array
72
     */
73 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...
74
    {
75
        $packageName = explode(" ", trim($packageName));
76
        $output = $this->runCommand(array(
77
            'command' => 'require',
78
            'packages' => $packageName,
79
            '--no-interaction' => true,
80
            '--profile' => true,
81
            '--prefer-dist' => true,
82
            '--ignore-platform-reqs' => true,
83
        ));
84
85
        return OutputParser::parseRequire($output);
86
    }
87
88
    /**
89
     * Run remove command
90
     *
91
     * @param string $packageName format "foo/bar foo/bar:1.0.0"
92
     * @return bool
93
     */
94 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...
95
    {
96
        $packageName = explode(' ', trim($packageName));
97
        $this->runCommand(array(
98
            'command' => 'remove',
99
            'packages' => $packageName,
100
            '--ignore-platform-reqs' => true,
101
            '--no-interaction' => true,
102
            '--profile' => true,
103
        ));
104
105
        return true;
106
    }
107
108
    /**
109
     * Get require
110
     *
111
     * @param string $packageName
112
     * @param string $callback
113
     * @param null   $typeFilter
114
     */
115
    public function foreachRequires($packageName, $callback, $typeFilter = null)
116
    {
117
        $info = $this->execInfo($packageName);
118
        if (isset($info['requires'])) {
119
            foreach ($info['requires'] as $name => $version) {
120
                $package = $this->execInfo($name);
121
                if (is_null($typeFilter) || @$package['type'] === $typeFilter) {
122
                    $callback($package);
123
                }
124
            }
125
        }
126
    }
127
128
    /**
129
     * Run get config information
130
     *
131
     * @param string $key
132
     * @param null   $value
133
     * @return array|mixed
134
     */
135
    public function execConfig($key, $value = null)
136
    {
137
        $commands = array(
138
            'command' => 'config',
139
            'setting-key' => $key,
140
            'setting-value' => $value,
141
        );
142
        if ($value) {
143
            $commands['setting-value'] = $value;
144
        }
145
        $output = $this->runCommand($commands);
146
147
        return OutputParser::parseConfig($output);
148
    }
149
150
    /**
151
     * Get config list
152
     *
153
     * @return array
154
     */
155
    public function getConfig()
156
    {
157
        $output = $this->runCommand(array(
158
            'command' => 'config',
159
            '--list' => true,
160
        ));
161
162
        return OutputParser::parseList($output);
163
    }
164
165
    /**
166
     * Set work dir
167
     *
168
     * @param string $workingDir
169
     */
170
    public function setWorkingDir($workingDir)
171
    {
172
        $this->workingDir = $workingDir;
173
    }
174
175
    /**
176
     * Run composer command
177
     *
178
     * @param array $commands
179
     * @return string
180
     */
181
    public function runCommand($commands)
182
    {
183
        $this->init();
184
        $commands['--working-dir'] = $this->workingDir;
185
        $commands['--no-ansi'] = 1;
186
        $input = new ArrayInput($commands);
187
        $output = new BufferedOutput();
188
189
        $exitCode = $this->consoleApplication->run($input, $output);
190
191
        $log = $output->fetch();
192
        if ($exitCode) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $exitCode of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
193
            log_error($log);
194
            throw new \RuntimeException($log);
195
        }
196
        log_info($log, $commands);
197
198
        return $log;
199
    }
200
201
    /**
202
     * Init composer console application
203
     */
204
    private function init()
205
    {
206
        set_time_limit(0);
207
        @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...
208
        // Config for some environment
209
        putenv('COMPOSER_HOME='.$this->appConfig['plugin_realdir'].'/.composer');
210
        $consoleApplication = new Application();
211
        $consoleApplication->resetComposer();
212
        $consoleApplication->setAutoExit(false);
213
        $this->consoleApplication = $consoleApplication;
214
        $this->workingDir = $this->workingDir ? $this->workingDir : $this->appConfig['root_dir'];
215
    }
216
}
217