Failed Conditions
Push — experimental/3.1 ( d52b28...afea38 )
by chihiro
28s
created

ComposerProcessService::runCommand()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
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 Eccube\Annotation\Inject;
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
     * @Inject("config")
37
     * @var array
38
     */
39
    protected $appConfig;
40
41
    private $workingDir;
42
    private $composerFile;
43
    private $composerSetup;
44
45
    /**
46
     * This function to install a plugin by composer require
47
     *
48
     * @param string $packageName format foo/bar or foo/bar:1.0.0 or "foo/bar 1.0.0"
49
     * @return bool
50
     */
51 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...
52
    {
53
        set_time_limit(0);
54
        $this->init();
55
        // Build command
56
        $command = $this->getPHP().' '.$this->composerFile.' require '.$packageName;
57
        $command .= ' --prefer-dist --no-progress --no-suggest --no-scripts --ignore-platform-reqs --profile --no-ansi --no-interaction -d ';
58
        $command .= $this->workingDir.' 2>&1';
59
        log_info($command);
60
        $this->runCommand($command);
61
62
        return true;
63
    }
64
65
    /**
66
     * This function to remove a plugin by composer remove
67
     * Note: Remove with dependency, if not, please add " --no-update-with-dependencies"
68
     *
69
     * @param string $packageName format foo/bar or foo/bar:1.0.0 or "foo/bar 1.0.0"
70
     * @return bool
71
     */
72 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...
73
    {
74
        set_time_limit(0);
75
        $this->init();
76
        // Build command
77
        $command = $this->getPHP().' '.$this->composerFile.' remove '.$packageName;
78
        $command .= ' --no-progress --no-scripts --ignore-platform-reqs --profile --no-ansi --no-interaction -d ';
79
        $command .= $this->workingDir.' 2>&1';
80
        log_info($command);
81
82
        // Execute command
83
        $this->runCommand($command);
84
85
        return true;
86
    }
87
88
    /**
89
     * Run command
90
     *
91
     * @param string $command
92
     * @return void
93
     */
94
    public function runCommand($command)
95
    {
96
        // Execute command
97
        $output = array();
98
        exec($command, $output);
99
        log_info(PHP_EOL.implode(PHP_EOL, $output).PHP_EOL);
100
    }
101
102
    /**
103
     * Set working dir
104
     * @param string $workingDir
105
     */
106
    public function setWorkingDir($workingDir)
107
    {
108
        $this->workingDir = $workingDir;
109
    }
110
111
    /**
112
     * Get environment php command
113
     *
114
     * @return string
115
     */
116
    private function getPHP()
117
    {
118
        return 'php';
119
    }
120
121
    /**
122
     * Set init
123
     */
124
    private function init()
125
    {
126
        @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...
127
        // Config for some environment
128
        putenv('COMPOSER_HOME='.$this->appConfig['plugin_realdir'].'/.composer');
129
        $this->workingDir = $this->workingDir ? $this->workingDir : $this->appConfig['root_dir'];
130
        $this->setupComposer();
131
    }
132
133
    /**
134
     * Check composer file and setup it
135
     */
136
    private function setupComposer()
137
    {
138
        $this->composerFile = $this->workingDir.'/composer.phar';
139
        $this->composerSetup = $this->workingDir.'/composer-setup.php';
140
        if (!file_exists($this->composerFile)) {
141
            if (!file_exists($this->composerSetup)) {
142
                $result = copy('https://getcomposer.org/installer', $this->composerSetup);
143
                log_info($this->composerSetup.' : '.$result);
144
            }
145
            $command = $this->getPHP().' '.$this->composerSetup;
146
            $this->runCommand($command);
147
148
            unlink($this->composerSetup);
149
        }
150
    }
151
}
152