Completed
Pull Request — experimental/3.1 (#2588)
by
unknown
87:26
created

ComposerProcessService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 11
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;
24
25
use Doctrine\ORM\EntityManagerInterface;
26
use Eccube\Annotation\Service;
27
use Eccube\Application;
28
29
/**
30
 * Class ComposerProcessService
31
 * @package Eccube\Service
32
 * @Service
33
 */
34
class ComposerProcessService
35
{
36
    /**
37
     * @var Application
38
     */
39
    protected $app;
40
41
    private $composerFile;
42
    private $composerSetup;
43
    private static $vendorName = 'ec-cube';
44
45
    /**
46
     * ComposerProcessService constructor.
47
     * @param Application $app
48
     */
49
    public function __construct(Application $app)
50
    {
51
        @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...
52
        // Config for some environment
53
        putenv('COMPOSER_HOME='.$app['config']['plugin_realdir'].'/.composer');
54
        $this->composerFile = $app['config']['root_dir'].'/composer.phar';
55
        $this->composerSetup = $app['config']['root_dir'].'/composer-setup.php';
56
        $this->app = $app;
57
58
        $this->setupComposer();
59
    }
60
61
    /**
62
     * This function to install a plugin by composer require
63
     *
64
     * @param string $packageName
65
     * @return bool
66
     */
67 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...
68
    {
69
        set_time_limit(0);
70
        // Build command
71
        $packageName = self::$vendorName.'/'.$packageName;
72
        $command = $this->getPHP().' '.$this->composerFile.' require '.$packageName;
73
        $command .= ' --prefer-dist --no-progress --no-suggest --no-scripts --ignore-platform-reqs --profile --no-ansi --no-interaction -d ';
74
        $command .= $this->app['config']['root_dir'].' 2>&1';
75
        $this->app->log($command);
76
77
        /**
78
         * Mysql lock in transaction
79
         * @link https://dev.mysql.com/doc/refman/5.7/en/lock-tables.html
80
         * @var EntityManagerInterface $em
81
         */
82
        $em = $this->app['orm.em'];
83
        if ($em->getConnection()->isTransactionActive()) {
84
            $em->getConnection()->commit();
85
            $em->getConnection()->beginTransaction();
86
        }
87
88
        // Execute command
89
        $output = array();
90
        exec($command, $output);
91
        $this->app->log(PHP_EOL . implode(PHP_EOL, $output) . PHP_EOL);
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
92
93
        return true;
94
    }
95
96
    /**
97
     * This function to remove a plugin by composer remove
98
     * Note: Remove with dependency, if not, please add " --no-update-with-dependencies"
99
     *
100
     * @param string $packageName
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
        set_time_limit(0);
106
        // Build command
107
        $packageName = self::$vendorName.'/'.$packageName;
108
        $command = $this->getPHP().' '.$this->composerFile.' remove '.$packageName;
109
        $command .= ' --no-progress --no-scripts --ignore-platform-reqs --profile --no-ansi --no-interaction -d ';
110
        $command .= $this->app['config']['root_dir'].' 2>&1';
111
        $this->app->log($command);
112
113
        /**
114
         * Mysql lock in transaction
115
         * @link https://dev.mysql.com/doc/refman/5.7/en/lock-tables.html
116
         * @var EntityManagerInterface $em
117
         */
118
        $em = $this->app['orm.em'];
119
        if ($em->getConnection()->isTransactionActive()) {
120
            $em->getConnection()->commit();
121
            $em->getConnection()->beginTransaction();
122
        }
123
124
        // Execute command
125
        $output = array();
126
        exec($command, $output);
127
        $this->app->log(PHP_EOL.implode(PHP_EOL, $output).PHP_EOL);
128
129
        return true;
130
    }
131
132
    /**
133
     * Get environment php command
134
     *
135
     * @return string
136
     */
137
    private function getPHP()
138
    {
139
        return 'php';
140
    }
141
142
    /**
143
     * Check composer file and setup it
144
     */
145
    private function setupComposer()
146
    {
147
        if (!file_exists($this->composerFile)) {
148
            if (!file_exists($this->composerSetup)) {
149
                $result = copy('https://getcomposer.org/installer', $this->composerSetup);
150
                $this->app->log($this->composerSetup.' : '.$result);
151
            }
152
            $command = $this->getPHP().' '.$this->composerSetup;
153
            $output = array();
154
            exec($command, $output);
155
            $this->app->log(PHP_EOL.implode(PHP_EOL, $output).PHP_EOL);
156
157
            unlink($this->composerSetup);
158
        }
159
    }
160
}
161