-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComposerTool.php
143 lines (131 loc) · 3.95 KB
/
ComposerTool.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
namespace MountHolyoke\Jorge\Tool;
use Composer\Factory;
use Composer\IO\ConsoleIO;
use Composer\IO\NullIO;
use MountHolyoke\Jorge\Helper\ComposerApplication;
use MountHolyoke\Jorge\Tool\Tool;
use Psr\Log\LogLevel;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Provides a Jorge tool that uses the Composer API to execute Composer commands.
*
* @link https://github.com/mtholyoke/jorge
*
* @author Jason Proctor <jproctor@mtholyoke.edu>
* @copyright 2018 Trustees of Mount Holyoke College
*/
class ComposerTool extends Tool {
/** @var MountHolyoke\Jorge\Helper\ComposerApplication $composerApplication The instance of Composer */
protected $composerApplication = NULL;
/**
* {@inheritDoc}
*/
protected function applyVerbosity($argv = []) {
switch ($this->verbosity) {
case OutputInterface::VERBOSITY_QUIET:
$argv['-q'] = TRUE;
break;
case OutputInterface::VERBOSITY_NORMAL:
default:
break;
case OutputInterface::VERBOSITY_VERBOSE:
$argv['-v'] = TRUE;
break;
case OutputInterface::VERBOSITY_VERY_VERBOSE:
$argv['-vv'] = TRUE;
break;
case OutputInterface::VERBOSITY_DEBUG:
$argv['-vvv'] = TRUE;
break;
}
return $argv;
}
/**
* Joins arguments and options as if they were on the command line.
*
* @param array $argv Command, arguments, and options for run()
* @return string The joined string of arguments and options
*/
protected function argvJoin(array $argv = []) {
if (array_key_exists('command', $argv)) {
unset($argv['command']);
}
$joinable = [];
foreach ($argv as $k => $v) {
if (is_bool($v)) {
if ($v) {
$joinable[] = $k;
}
} elseif ($v === NULL) {
$joinable[] = $k;
} else {
$joinable[] = "$k=$v";
}
}
return implode(' ', $joinable);
}
/**
* Establishes the `composer` tool.
*/
protected function configure() {
$this->setName('composer');
}
/**
* Executes the tool command and returns the result array and status.
*
* Composer is a Symfony Console Application, so there’s no need to
* trap its output to send back up the call stack for verbosity control.
*
* @param array $argv Arguments and options for the command
* @param bool|null $prompt Require interaction mid-command
* @return array The command passed to the Composer application and its exit code.
*/
protected function exec($argv = [], $prompt = FALSE) {
if ($argv === NULL || !isset($argv) || !is_array($argv)) {
$argv = [];
}
$input = new ArrayInput($argv);
$output = $this->jorge->getOutput();
if (!array_key_exists('command', $argv)) {
$argv['command'] = '';
}
$this->log(
LogLevel::NOTICE,
'% composer {%cmd} {%argv}',
['%cmd' => $argv['command'], '%argv' => $this->argvJoin($argv)]
);
$status = $this->composerApplication->run($input, $output);
return [
'command' => $input,
'output' => '',
'status' => $status,
];
}
/**
* Creates a Composer object to use for running commands.
*/
protected function initialize() {
if (empty($this->getExecutable())) {
return;
}
if (($rootPath = $this->jorge->getPath()) === NULL) {
return;
}
# Fail silently if the current project doesn’t use Composer.
$this->config = $this->jorge->loadConfigFile('composer.json', NULL);
if (empty($this->config)) {
return;
}
$composerJson = $rootPath . DIRECTORY_SEPARATOR . 'composer.json';
$factory = new Factory();
$composer = $factory->createComposer(new NullIO, $composerJson, FALSE, $rootPath);
$this->composerApplication = new ComposerApplication();
$this->composerApplication->setComposer($composer);
$this->composerApplication->setAutoExit(FALSE);
if (!empty($this->composerApplication)) {
$this->enable();
}
}
}