|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace N98\Magento\Command\Installer\SubCommand; |
|
4
|
|
|
|
|
5
|
|
|
use Composer\IO\ConsoleIO; |
|
6
|
|
|
use Composer\Util\RemoteFilesystem; |
|
7
|
|
|
use N98\Util\OperatingSystem; |
|
8
|
|
|
use N98\Magento\Command\SubCommand\AbstractSubCommand; |
|
9
|
|
|
|
|
10
|
|
|
class InstallComposer extends AbstractSubCommand |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var int |
|
14
|
|
|
*/ |
|
15
|
|
|
const EXEC_STATUS_OK = 0; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @return void |
|
19
|
|
|
* |
|
20
|
|
|
* @throws \Exception |
|
21
|
|
|
*/ |
|
22
|
|
|
public function execute() |
|
23
|
|
|
{ |
|
24
|
|
View Code Duplication |
if (OperatingSystem::isProgramInstalled('composer.phar')) { |
|
|
|
|
|
|
25
|
|
|
$composerBin = 'composer.phar'; |
|
26
|
|
|
} elseif (OperatingSystem::isProgramInstalled('composer')) { |
|
27
|
|
|
$composerBin = 'composer'; |
|
28
|
|
|
} elseif (OperatingSystem::isProgramInstalled('composer.bat')) { |
|
29
|
|
|
$composerBin = 'composer'; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
if (empty($composerBin)) { |
|
33
|
|
|
$composerBin = $this->downloadComposer(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
if (empty($composerBin)) { |
|
37
|
|
|
throw new \Exception('Cannot find or install composer. Please try it manually. https://getcomposer.org/'); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$this->output->writeln('<info>Found executable <comment>' . $composerBin . '</comment></info>'); |
|
41
|
|
|
$this->config['composer_bin'] = $composerBin; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @return string |
|
46
|
|
|
* @throws \Exception |
|
47
|
|
|
*/ |
|
48
|
|
|
protected function downloadComposer() |
|
49
|
|
|
{ |
|
50
|
|
|
$this->output->writeln('<info>Could not find composer. Try to download it.</info>'); |
|
51
|
|
|
$io = new ConsoleIO($this->input, $this->output, $this->getCommand()->getHelperSet()); |
|
|
|
|
|
|
52
|
|
|
$rfs = new RemoteFilesystem($io); |
|
53
|
|
|
$composerInstaller = $rfs->getContents('getcomposer.org', 'https://getcomposer.org/installer', true); |
|
54
|
|
|
|
|
55
|
|
|
$tempComposerInstaller = $this->config['installationFolder'] . '/_composer_installer.php'; |
|
56
|
|
|
file_put_contents($tempComposerInstaller, $composerInstaller); |
|
57
|
|
|
|
|
58
|
|
|
if (OperatingSystem::isWindows()) { |
|
59
|
|
|
$installCommand = 'php ' . $tempComposerInstaller . ' --force'; |
|
60
|
|
|
} else { |
|
61
|
|
|
$installCommand = '/usr/bin/env php ' . $tempComposerInstaller . ' --force'; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$this->output->writeln('<comment>' . $installCommand . '</comment>'); |
|
65
|
|
|
exec($installCommand, $installationOutput, $returnStatus); |
|
66
|
|
|
unlink($tempComposerInstaller); |
|
67
|
|
|
$installationOutput = implode(PHP_EOL, $installationOutput); |
|
68
|
|
|
if ($returnStatus !== self::EXEC_STATUS_OK) { |
|
69
|
|
|
throw new \Exception('Installation failed.' . $installationOutput); |
|
70
|
|
|
} else { |
|
71
|
|
|
$this->output->writeln('<info>Successfully installed composer to Magento root</info>'); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $this->config['installationFolder'] . '/composer.phar'; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
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.