| 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 Eccube\Annotation\Inject; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  |  |      * @Inject("config") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     protected $appConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     private $composerFile; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     private $composerSetup; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     private static $vendorName = 'ec-cube'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * This function to install a plugin by composer require | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @param string $packageName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 52 |  | View Code Duplication |     public function execRequire($packageName) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         set_time_limit(0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $this->setupComposer(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         // Build command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $packageName = self::$vendorName.'/'.$packageName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $command = $this->getPHP().' '.$this->composerFile.' require '.$packageName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $command .= ' --prefer-dist --no-progress --no-suggest --no-scripts --ignore-platform-reqs --profile --no-ansi --no-interaction -d '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $command .= $this->appConfig['root_dir'].' 2>&1'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         log_info($command); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         // Execute command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $output = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         exec($command, $output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         log_info(PHP_EOL.implode(PHP_EOL, $output).PHP_EOL); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * This function to remove a plugin by composer remove | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * Note: Remove with dependency, if not, please add " --no-update-with-dependencies" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @param string $packageName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @return bool | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 77 |  |  |      */ | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 78 |  | View Code Duplication |     public function execRemove($packageName) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |         set_time_limit(0); | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |         $this->setupComposer(); | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |         // Build command | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |         $packageName = self::$vendorName.'/'.$packageName; | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |         $command = $this->getPHP().' '.$this->composerFile.' remove '.$packageName; | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |         $command .= ' --no-progress --no-scripts --ignore-platform-reqs --profile --no-ansi --no-interaction -d '; | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |         $command .= $this->appConfig['root_dir'].' 2>&1'; | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |         log_info($command); | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |         // Execute command | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |         $output = array(); | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |         exec($command, $output); | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |         log_info(PHP_EOL.implode(PHP_EOL, $output).PHP_EOL); | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |         return true; | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * Get environment php command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     private function getPHP() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         return 'php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * Check composer file and setup it | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     private function setupComposer() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         @ini_set('memory_limit', '1536M'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         // Config for some environment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         putenv('COMPOSER_HOME='.$this->appConfig['plugin_realdir'].'/.composer'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         $this->composerFile = $this->appConfig['root_dir'].'/composer.phar'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         $this->composerSetup = $this->appConfig['root_dir'].'/composer-setup.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         if (!file_exists($this->composerFile)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             if (!file_exists($this->composerSetup)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                 $result = copy('https://getcomposer.org/installer', $this->composerSetup); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                 log_info($this->composerSetup.' : '.$result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             $command = $this->getPHP().' '.$this->composerSetup; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             $output = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             exec($command, $output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             log_info(PHP_EOL.implode(PHP_EOL, $output).PHP_EOL); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             unlink($this->composerSetup); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 131 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 132 |  |  |  | 
            
                        
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.