-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathBase.php
More file actions
68 lines (56 loc) · 1.21 KB
/
Base.php
File metadata and controls
68 lines (56 loc) · 1.21 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
<?php
namespace WP_CLI\Fetchers;
use WP_CLI;
use WP_CLI\ExitException;
/**
* Fetch a WordPress entity for use in a subcommand.
*
* @template T
*/
abstract class Base {
/**
* The message to display when an item is not found.
*
* @var string
*/
protected $msg;
/**
* @param string|int $arg The raw CLI argument.
* @return T|false The item if found; false otherwise.
*/
abstract public function get( $arg );
/**
* Like get(), but calls WP_CLI::error() instead of returning false.
*
* @param string $arg The raw CLI argument.
* @return T The item if found.
* @throws ExitException If the item is not found.
*
* @phpstan-assert-if-true !false $this->get()
*/
public function get_check( $arg ) {
$item = $this->get( $arg );
if ( ! $item ) {
WP_CLI::error( sprintf( $this->msg, $arg ) );
}
return $item;
}
/**
* Get multiple items.
*
* @param array $args The raw CLI arguments.
* @return T[] The list of found items.
*/
public function get_many( $args ) {
$items = [];
foreach ( $args as $arg ) {
$item = $this->get( $arg );
if ( $item ) {
$items[] = $item;
} else {
WP_CLI::warning( sprintf( $this->msg, $arg ) );
}
}
return $items;
}
}