Completed
Pull Request — experimental/3.1 (#2640)
by
unknown
79:35 queued 38:52
created

ComposerApiService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
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\Service;
27
use Symfony\Component\Console\Input\ArrayInput;
28
use Symfony\Component\Console\Output\BufferedOutput;
29
30
/**
31
 * Class ComposerApiService
32
 * @package Eccube\Service\Composer
33
 * @Service
34
 */
35
class ComposerApiService implements ComposerServiceInterface
36
{
37
    /**
38
     * @var array
39
     */
40
    protected $appConfig;
41
42
    /**
43
     * @var \Eccube\Application
44
     */
45
    protected $app;
46
47
    /**
48
     * @var Application $consoleApplication
49
     */
50
    private $consoleApplication;
51
52
    private $workingDir;
53
54
    public function __construct(\Eccube\Application $app)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
55
    {
56
        $this->app = $app;
57
        $this->appConfig = $app['config'];
58
    }
59
60
    /**
61
     * Run get info command
62
     *
63
     * @param string $pluginName format foo/bar or foo/bar:1.0.0 or "foo/bar 1.0.0"
64
     * @return array
65
     */
66
    public function execInfo($pluginName)
67
    {
68
        $output = $this->runCommand(array(
69
            'command' => 'info',
70
            'package' => $pluginName,
71
        ));
72
73
        return OutputParser::parseInfo($output);
74
    }
75
76
    /**
77
     * Run execute command
78
     *
79
     * @param string $packageName format "foo/bar foo/bar:1.0.0"
80
     * @return array
81
     */
82 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...
83
    {
84
        $packageName = explode(" ", trim($packageName));
85
        $output = $this->runCommand(array(
86
            'command' => 'require',
87
            'packages' => $packageName,
88
            '--no-interaction' => true,
89
            '--profile' => true,
90
            '--prefer-dist' => true,
91
            '--ignore-platform-reqs' => true,
92
        ));
93
94
        return OutputParser::parseRequire($output);
95
    }
96
97
    /**
98
     * Run remove command
99
     *
100
     * @param string $packageName format "foo/bar foo/bar:1.0.0"
101
     * @return bool
102
     */
103 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...
104
    {
105
        $packageName = explode(' ', trim($packageName));
106
        $this->runCommand(array(
107
            'command' => 'remove',
108
            'packages' => $packageName,
109
            '--ignore-platform-reqs' => true,
110
            '--no-interaction' => true,
111
            '--profile' => true,
112
        ));
113
114
        return true;
115
    }
116
117
    /**
118
     * Get require
119
     *
120
     * @param string $packageName
121
     * @param string $callback
122
     * @param null   $typeFilter
123
     */
124
    public function foreachRequires($packageName, $callback, $typeFilter = null)
125
    {
126
        $info = $this->execInfo($packageName);
127
        if (isset($info['requires'])) {
128
            foreach ($info['requires'] as $name => $version) {
129
                $package = $this->execInfo($name);
130
                if (is_null($typeFilter) || @$package['type'] === $typeFilter) {
131
                    $callback($package);
132
                }
133
            }
134
        }
135
    }
136
137
    /**
138
     * Run get config information
139
     *
140
     * @param string $key
141
     * @param null   $value
142
     * @return array|mixed
143
     */
144
    public function execConfig($key, $value = null)
145
    {
146
        $commands = array(
147
            'command' => 'config',
148
            'setting-key' => $key,
149
            'setting-value' => $value,
150
        );
151
        if ($value) {
152
            $commands['setting-value'] = $value;
153
        }
154
        $output = $this->runCommand($commands);
155
156
        return OutputParser::parseConfig($output);
157
    }
158
159
    /**
160
     * Get config list
161
     *
162
     * @return array
163
     */
164
    public function getConfig()
165
    {
166
        $output = $this->runCommand(array(
167
            'command' => 'config',
168
            '--list' => true,
169
        ));
170
171
        return OutputParser::parseList($output);
172
    }
173
174
    /**
175
     * Set work dir
176
     *
177
     * @param string $workingDir
178
     */
179
    public function setWorkingDir($workingDir)
180
    {
181
        $this->workingDir = $workingDir;
182
    }
183
184
    /**
185
     * Run composer command
186
     *
187
     * @param array $commands
188
     * @return string
189
     */
190
    public function runCommand($commands)
191
    {
192
        $this->init();
193
        $commands['--working-dir'] = $this->workingDir;
194
        $commands['--no-ansi'] = 1;
195
        $input = new ArrayInput($commands);
196
        $output = new BufferedOutput();
197
198
        $exitCode = $this->consoleApplication->run($input, $output);
199
200
        $log = $output->fetch();
201
        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...
202
            log_error($log);
203
            throw new \RuntimeException($log);
204
        }
205
        log_info($log, $commands);
206
207
        return $log;
208
    }
209
210
    /**
211
     * Init composer console application
212
     */
213
    private function init()
214
    {
215
        set_time_limit(0);
216
        @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...
217
        // Config for some environment
218
        putenv('COMPOSER_HOME='.$this->appConfig['plugin_realdir'].'/.composer');
219
        $consoleApplication = new Application();
220
        $consoleApplication->resetComposer();
221
        $consoleApplication->setAutoExit(false);
222
        $this->consoleApplication = $consoleApplication;
223
        $this->workingDir = $this->workingDir ? $this->workingDir : $this->appConfig['root_dir'];
224
    }
225
}
226