-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommand.php
More file actions
91 lines (77 loc) · 2.28 KB
/
Command.php
File metadata and controls
91 lines (77 loc) · 2.28 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
<?php declare(strict_types=1);
namespace StellarWP\Foundation\WPCli;
use StellarWP\Foundation\Container\Contracts\Container;
use WP_CLI;
use WP_CLI_Command;
/**
* Base class for WP-CLI commands that use the Foundation container.
*
* Extend this class in an application package when a command needs container
* access and should register itself with WP-CLI using a consistent synopsis.
*/
abstract class Command extends WP_CLI_Command
{
protected const string POSITIONAL = 'positional';
protected const string ASSOCIATIVE = 'assoc';
protected const string FLAG = 'flag';
protected const int SUCCESS = 0;
protected const int ERROR = 1;
public function __construct(
protected Container $container,
private readonly string $commandPrefix
) {
parent::__construct();
}
/**
* @param list<mixed> $args
* @param array<string,mixed> $assocArgs
*
* @return int 0 is success; any other value is an error.
*/
abstract public function runCommand(array $args = [], array $assocArgs = []): int;
/**
* The command name under the configured prefix, e.g. "sync".
*/
abstract protected function subcommand(): string;
/**
* The command description as it appears in "wp help".
*/
abstract protected function description(): string;
/**
* The array of command arguments/options the command accepts.
*
* @return array{}|list<array{type: string, name: string, description: string, default?: mixed, optional?: bool, repeating?: bool, options?: list<mixed>}>
*/
abstract protected function arguments(): array;
/**
* Register the command with WP-CLI.
*/
public function register(): void {
WP_CLI::add_command($this->command(), [$this, 'runCommand'], [
'shortdesc' => $this->description(),
'synopsis' => $this->arguments(),
]);
}
protected function command(): string {
return trim($this->commandPrefix . ' ' . $this->subcommand());
}
/**
* Ask a question and retrieve a normalized answer from STDIN.
*/
protected function ask(string $question): string {
fwrite($this->output(), $question . ' ');
return strtolower(trim((string) fgets($this->input())));
}
/**
* @return resource
*/
protected function input(): mixed {
return STDIN;
}
/**
* @return resource
*/
protected function output(): mixed {
return STDOUT;
}
}