|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Spiral Framework. |
|
4
|
|
|
* |
|
5
|
|
|
* @license MIT |
|
6
|
|
|
* @author Anton Titov (Wolfy-J) |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Spiral\Console; |
|
10
|
|
|
|
|
11
|
|
|
use Psr\Container\ContainerExceptionInterface; |
|
12
|
|
|
use Psr\Container\ContainerInterface; |
|
13
|
|
|
use Spiral\Boot\DispatcherInterface; |
|
14
|
|
|
use Spiral\Boot\EnvironmentInterface; |
|
15
|
|
|
use Spiral\Exceptions\ConsoleHandler; |
|
16
|
|
|
use Spiral\Snapshots\SnapshotterInterface; |
|
17
|
|
|
use Symfony\Component\Console\Input\ArgvInput; |
|
18
|
|
|
use Symfony\Component\Console\Output\ConsoleOutput; |
|
19
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Manages Console commands and exception. Lazy loads console service. |
|
23
|
|
|
*/ |
|
24
|
|
|
class ConsoleDispatcher implements DispatcherInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** @var EnvironmentInterface */ |
|
27
|
|
|
private $environment; |
|
28
|
|
|
|
|
29
|
|
|
/** @var ContainerInterface */ |
|
30
|
|
|
private $container; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param EnvironmentInterface $environment |
|
34
|
|
|
* @param ContainerInterface $container |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct(EnvironmentInterface $environment, ContainerInterface $container) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->environment = $environment; |
|
39
|
|
|
$this->container = $container; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @inheritdoc |
|
44
|
|
|
*/ |
|
45
|
|
|
public function canServe(): bool |
|
46
|
|
|
{ |
|
47
|
|
|
// only run in pure CLI more, ignore under RoadRunner |
|
48
|
|
|
return (php_sapi_name() == 'cli' && $this->environment->get('RR') === null); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @inheritdoc |
|
53
|
|
|
*/ |
|
54
|
|
|
public function serve() |
|
55
|
|
|
{ |
|
56
|
|
|
/** @var ConsoleCore $core */ |
|
57
|
|
|
$core = $this->container->get(ConsoleCore::class); |
|
58
|
|
|
$output = new ConsoleOutput(); |
|
59
|
|
|
|
|
60
|
|
|
try { |
|
61
|
|
|
$core->start(new ArgvInput(), $output); |
|
62
|
|
|
} catch (\Throwable $e) { |
|
63
|
|
|
$this->handleException($e, $output); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param \Throwable $e |
|
69
|
|
|
* @param OutputInterface $output |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function handleException(\Throwable $e, OutputInterface $output) |
|
72
|
|
|
{ |
|
73
|
|
|
try { |
|
74
|
|
|
$this->container->get(SnapshotterInterface::class)->register($e); |
|
75
|
|
|
} catch (\Throwable|ContainerExceptionInterface $se) { |
|
76
|
|
|
// no need to notify when unable to register an exception |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
// Explaining exception to the user |
|
80
|
|
|
$handler = new ConsoleHandler(STDERR); |
|
|
|
|
|
|
81
|
|
|
$output->write($handler->renderException($e, $this->mapVerbosity($output))); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param OutputInterface $output |
|
86
|
|
|
* @return int |
|
87
|
|
|
*/ |
|
88
|
|
|
private function mapVerbosity(OutputInterface $output): int |
|
89
|
|
|
{ |
|
90
|
|
|
if ($output->isDebug() || $output->isVeryVerbose()) { |
|
91
|
|
|
return ConsoleHandler::VERBOSITY_DEBUG; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
if ($output->isVerbose()) { |
|
95
|
|
|
return ConsoleHandler::VERBOSITY_VERBOSE; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return ConsoleHandler::VERBOSITY_BASIC; |
|
99
|
|
|
} |
|
100
|
|
|
} |
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: