1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the webmozart/console package. |
5
|
|
|
* |
6
|
|
|
* (c) Bernhard Schussek <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Webmozart\Console\Adapter; |
13
|
|
|
|
14
|
|
|
use Exception; |
15
|
|
|
use Symfony\Component\Console\Application; |
16
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
17
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
18
|
|
|
use Webmozart\Assert\Assert; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Adapts an `Application` instance of this package to Symfony's |
22
|
|
|
* {@link Application} API. |
23
|
|
|
* |
24
|
|
|
* @since 1.0 |
25
|
|
|
* |
26
|
|
|
* @author Bernhard Schussek <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class ApplicationAdapter extends Application |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var \Webmozart\Console\Api\Application\Application |
32
|
|
|
*/ |
33
|
|
|
private $adaptedApplication; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var CommandAdapter |
37
|
|
|
*/ |
38
|
|
|
private $currentCommand; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Creates the application. |
42
|
|
|
* |
43
|
|
|
* @param \Webmozart\Console\Api\Application\Application $application |
44
|
|
|
*/ |
45
|
15 |
|
public function __construct(\Webmozart\Console\Api\Application\Application $application) |
46
|
|
|
{ |
47
|
15 |
|
$this->adaptedApplication = $application; |
48
|
|
|
|
49
|
15 |
|
$config = $application->getConfig(); |
50
|
|
|
|
51
|
15 |
|
parent::__construct($config->getDisplayName(), $config->getVersion()); |
52
|
|
|
|
53
|
15 |
|
if ($dispatcher = $config->getEventDispatcher()) { |
54
|
12 |
|
$this->setDispatcher($dispatcher); |
55
|
|
|
} |
56
|
|
|
|
57
|
15 |
|
$this->setAutoExit($config->isTerminatedAfterRun()); |
58
|
15 |
|
$this->setCatchExceptions($config->isExceptionCaught()); |
59
|
|
|
|
60
|
15 |
|
foreach ($application->getCommands() as $command) { |
61
|
13 |
|
$this->add(new CommandAdapter($command, $this)); |
62
|
|
|
} |
63
|
15 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return \Webmozart\Console\Api\Application\Application |
67
|
|
|
*/ |
68
|
|
|
public function getAdaptedApplication() |
69
|
|
|
{ |
70
|
|
|
return $this->adaptedApplication; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
|
|
public function doRun(InputInterface $input, OutputInterface $output) |
77
|
|
|
{ |
78
|
|
|
/* @var ArgsInput $input */ |
79
|
|
|
Assert::isInstanceOf($input, 'Webmozart\Console\Adapter\ArgsInput'); |
80
|
|
|
|
81
|
|
|
$rawArgs = $input->getRawArgs(); |
82
|
|
|
$resolvedCommand = $this->adaptedApplication->resolveCommand($rawArgs); |
83
|
|
|
|
84
|
|
|
// Add parsed Args to the adapter |
85
|
|
|
$input = new ArgsInput($rawArgs, $resolvedCommand->getArgs()); |
|
|
|
|
86
|
|
|
|
87
|
|
|
// Don't use $this->get() as get() does not work for sub-commands |
88
|
|
|
$this->currentCommand = new CommandAdapter($resolvedCommand->getCommand(), $this); |
89
|
|
|
|
90
|
|
|
try { |
91
|
|
|
$result = parent::doRun($input, $output); |
92
|
|
|
$this->currentCommand = null; |
93
|
|
|
} catch (Exception $e) { |
94
|
|
|
$this->currentCommand = null; |
95
|
|
|
|
96
|
|
|
throw $e; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $result; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
*/ |
105
|
|
|
protected function getCommandName(InputInterface $input) |
106
|
|
|
{ |
107
|
|
|
// This method must return something, otherwise the base class tries |
108
|
|
|
// to set the "command" argument which doesn't usually exist |
109
|
|
|
return 'command-name'; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritdoc} |
114
|
|
|
*/ |
115
|
|
|
public function find($name) |
116
|
|
|
{ |
117
|
|
|
return $this->currentCommand; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
15 |
|
protected function getDefaultInputDefinition() |
124
|
|
|
{ |
125
|
15 |
|
return new ArgsFormatInputDefinition($this->adaptedApplication->getGlobalArgsFormat()); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* {@inheritdoc} |
130
|
|
|
*/ |
131
|
15 |
|
protected function getDefaultCommands() |
132
|
|
|
{ |
133
|
15 |
|
return array(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* {@inheritdoc} |
138
|
|
|
*/ |
139
|
15 |
|
protected function getDefaultHelperSet() |
140
|
|
|
{ |
141
|
15 |
|
return $this->adaptedApplication->getConfig()->getHelperSet(); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: