-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplication.php
More file actions
185 lines (160 loc) · 5.08 KB
/
Copy pathApplication.php
File metadata and controls
185 lines (160 loc) · 5.08 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
/**
* Application class file
*
* @package Mantle
*/
namespace Mantle\Console;
use Closure;
use InvalidArgumentException;
use Mantle\Contracts\Application as Application_Contract;
use Mantle\Contracts\Console\Application as Console_Application_Contract;
use Mantle\Support\Arr;
use Symfony\Component\Console\Application as Console_Application;
use Symfony\Component\Console\Command\Command as Symfony_Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Tester\CommandTester;
use Throwable;
/**
* Console Application
*
* Not to be confused with the Mantle Application/Container, this is an instance
* of the Symfony Console Application.
*/
class Application extends Console_Application implements Console_Application_Contract {
/**
* Array of closures to fire when the application is booted.
*
* @var Closure[]
*/
protected static array $bootstrappers = [];
/**
* Register a console "starting" bootstrapper.
*
* @param Closure $callback Callback to run.
*/
public static function starting( Closure $callback ): void {
static::$bootstrappers[] = $callback;
}
/**
* Forget all of the bootstrappers for the application.
*/
public static function forget_bootstrappers(): void {
static::$bootstrappers = [];
}
/**
* Constructor.
*
* @param Application_Contract $container
*/
public function __construct( protected Application_Contract $container ) {
parent::__construct(
$this->container['config']->get( 'app.name', 'Mantle' ),
$this->container['config']->get( 'app.version', 'UNKNOWN' ),
);
$this->setAutoExit( false );
$this->setCatchExceptions( false );
$this->bootstrap();
}
/**
* Bootstrap the console application.
*/
protected function bootstrap(): void {
// Fire off a starting event for the application to listen to.
$this->container['events']->dispatch( 'console.starting', $this );
foreach ( static::$bootstrappers as $bootstrapper ) {
$bootstrapper( $this );
}
}
/**
* Run the command through the console application.
*
* @todo Add event dispatching for before and after firing the command.
* @param InputInterface|null $input Input interface.
* @param OutputInterface|null $output Output interface.
*/
#[\Override]
public function run( ?InputInterface $input = null, ?OutputInterface $output = null ): int { // phpcs:ignore Generic.CodeAnalysis.UselessOverridingMethod.Found
return parent::run( $input, $output );
}
/**
* Run a console command by name.
*
* @todo Add support for non-Mantle commands (e.g. WP-CLI).
*
* @param string $command Command name.
* @param array<string, string> $parameters Command parameters.
* @param OutputInterface|null $output_buffer Output buffer.
*
* @throws InvalidArgumentException Thrown if the command is not a Mantle command.
*/
public function call( string $command, array $parameters = [], ?OutputInterface $output_buffer = null ): int {
if ( ! $this->has( $command ) ) {
throw new InvalidArgumentException( "Command [{$command}] does not exist." );
}
return $this->run(
new ArrayInput( array_merge( [ 'command' => $command ], $parameters ) ),
$output_buffer ?: new BufferedOutput(),
);
}
/**
* Test a console command by name.
*
* @param string $command Command name.
* @param array<string, string> $parameters Command parameters.
*/
public function test( string $command, array $parameters = [] ): CommandTester {
$command_instance = $this->find( $command );
$tester = new CommandTester( $command_instance );
$tester->execute( array_merge( [ 'command' => $command ], $parameters ) );
return $tester;
}
/**
* Resolve a command through the console application.
*
* @param Symfony_Command|Command|string $command
*/
public function resolve( Symfony_Command|Command|string $command ): static {
if ( is_string( $command ) ) {
return $this->resolve( $this->container->make( $command ) );
}
if ( $command instanceof Command ) {
$command->set_container( $this->container );
}
if ( method_exists( parent::class, 'addCommand' ) ) { // @phpstan-ignore-line function.alreadyNarrowedType
parent::addCommand( $command );
} else {
parent::add( $command );
}
return $this;
}
/**
* Resolve an array of commands through the console application.
*
* @param array<class-string<Command|Symfony_Command>>|class-string<Command|Symfony_Command>|Symfony_Command|Command $commands
*/
public function resolve_commands( array|string|Symfony_Command|Command $commands ): static {
$commands = Arr::wrap( $commands );
foreach ( $commands as $command ) {
$this->resolve( $command );
}
return $this;
}
/**
* Render a throwable for the console.
*
* @param Throwable $e
* @param OutputInterface $output
*/
public function render_throwable( Throwable $e, OutputInterface $output ): void {
$output->writeln(
sprintf(
'<error>Exception: %s</error>',
$e->getMessage(),
)
);
}
}