-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCommandRunner.php
More file actions
102 lines (83 loc) · 2.72 KB
/
Copy pathCommandRunner.php
File metadata and controls
102 lines (83 loc) · 2.72 KB
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
<?php
namespace Task\Plugin\Console;
use Task\Plugin\Stream\ReadableInterface;
use Task\Plugin\Stream\WritableInterface;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\BufferedOutput;
use Task\Plugin\Console\Output\ProxyOutput;
class CommandRunner implements ReadableInterface
{
protected $parameters = [];
public function __construct(Application $app, $commandName)
{
$command = $this->findCommand($app, $commandName);
$command->setApplication($app);
$command->mergeApplicationDefinition();
$app->setAutoExit(false);
$this->command = $command->getName();
$this->definition = $command->getDefinition();
$this->app = $app;
}
/**
* Should throw InvalidArgumentException if command not found.
*/
public function findCommand(Application $app, $commandName)
{
return $app->get($commandName);
}
public function run(OutputInterface $output = null)
{
$input = new ArrayInput(array_merge([
'command' => $this->command
], $this->getParameters()));
return $this->app->run($input, $output);
}
public function __call($method, array $arguments = [])
{
if (strpos($method, 'set') !== 0) {
throw new \InvalidArgumentException("Unknown method $method");
}
$alias = $this->parseMethodName(substr($method, 3));
$value = $arguments[0];
if ($this->definition->hasOption($alias)) {
$this->parameters["--$alias"] = $value;
} elseif ($this->definition->hasArgument($alias)) {
$this->parameters[$alias] = $value;
} else {
throw new \InvalidArgumentException("Unrecognised parameter $alias");
}
return $this;
}
public function parseMethodName($name)
{
$parts = preg_split('/(?<=[a-z])(?![a-z])/', $name, -1, PREG_SPLIT_NO_EMPTY);
return implode('-', array_map('strtolower', $parts));
}
public function read()
{
$output = new BufferedOutput;
$this->run($output);
return $output->fetch();
}
public function pipe(WritableInterface $to)
{
if ($to instanceof OutputInterface) {
$this->run($to);
return $to;
} else {
return $this->pipe((new ProxyOutput)->setTarget($to));
}
}
public function getParameters()
{
return $this->parameters;
}
public function setParameter($param, $value)
{
$this->parameters[$param] = $value;
return $this;
}
}