1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Weew\ConsoleApp; |
4
|
|
|
|
5
|
|
|
use Weew\App\App; |
6
|
|
|
use Weew\Console\ContainerAware\Console; |
7
|
|
|
use Weew\Console\IConsole; |
8
|
|
|
use Weew\ConsoleApp\Commands\ConfigDumpCommand; |
9
|
|
|
use Weew\ConsoleApp\Commands\GlobalEnvironmentCommand; |
10
|
|
|
use Weew\ConsoleArguments\IOption; |
11
|
|
|
use Weew\ConsoleArguments\Option; |
12
|
|
|
use Weew\ConsoleArguments\OptionType; |
13
|
|
|
|
14
|
|
|
class ConsoleApp extends App implements IConsoleApp { |
15
|
|
|
/** |
16
|
|
|
* @var IConsole |
17
|
|
|
*/ |
18
|
|
|
protected $console; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* ConsoleApp constructor. |
22
|
|
|
* |
23
|
|
|
* @param string $environment |
24
|
|
|
* @param bool $debug |
25
|
|
|
*/ |
26
|
|
|
public function __construct($environment = null, $debug = null) { |
27
|
|
|
parent::__construct($environment, $debug); |
28
|
|
|
|
29
|
|
|
$this->console = $this->createConsole(); |
30
|
|
|
$this->container->set([ConsoleApp::class, IConsoleApp::class], $this); |
|
|
|
|
31
|
|
|
$this->container->set([Console::class, IConsole::class], $this->console); |
|
|
|
|
32
|
|
|
$this->addDefaultCommands(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return IConsole |
37
|
|
|
*/ |
38
|
|
|
public function getConsole() { |
39
|
|
|
return $this->console; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param array $argv |
44
|
|
|
*/ |
45
|
|
View Code Duplication |
public function handleArgv(array $argv = null) { |
|
|
|
|
46
|
|
|
$option = $this->createEnvOption(); |
47
|
|
|
$option->parseArgv($argv); |
48
|
|
|
$this->detectEnvFromOption($option); |
49
|
|
|
|
50
|
|
|
$this->start(); |
51
|
|
|
$this->console->parseArgv($argv); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param array $args |
56
|
|
|
*/ |
57
|
|
View Code Duplication |
public function handleArgs(array $args) { |
|
|
|
|
58
|
|
|
$option = $this->createEnvOption(); |
59
|
|
|
$option->parseArgs($args); |
60
|
|
|
$this->detectEnvFromOption($option); |
61
|
|
|
|
62
|
|
|
$this->start(); |
63
|
|
|
$this->console->parseArgs($args); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param $string |
68
|
|
|
*/ |
69
|
|
View Code Duplication |
public function handleArgsString($string) { |
|
|
|
|
70
|
|
|
$option = $this->createEnvOption(); |
71
|
|
|
$option->parseString($string); |
72
|
|
|
$this->detectEnvFromOption($option); |
73
|
|
|
|
74
|
|
|
$this->start(); |
75
|
|
|
$this->console->parseString($string); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return IConsole |
80
|
|
|
*/ |
81
|
|
|
protected function createConsole() { |
82
|
|
|
return new Console($this->getContainer()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Register default commands. |
87
|
|
|
*/ |
88
|
|
|
protected function addDefaultCommands() { |
89
|
|
|
$this->getConsole()->addCommands([ |
90
|
|
|
GlobalEnvironmentCommand::class, |
91
|
|
|
]); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return Option |
96
|
|
|
*/ |
97
|
|
|
protected function createEnvOption() { |
98
|
|
|
return new Option(OptionType::SINGLE_OPTIONAL, '--env'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param IOption $option |
103
|
|
|
*/ |
104
|
|
|
protected function detectEnvFromOption(IOption $option) { |
105
|
|
|
if ($this->getDebug() && $option->hasValue() && ! $this->started) { |
106
|
|
|
$this->setEnvironment($option->getValue()); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
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: