1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Console; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* This class provides constants for defining console command exit codes. |
9
|
|
|
* |
10
|
|
|
* The exit codes follow the codes defined in the [FreeBSD sysexits(3)](https://man.openbsd.org/sysexits) manual page. |
11
|
|
|
* |
12
|
|
|
* These constants can be used in console controllers for example like this: |
13
|
|
|
* |
14
|
|
|
* ```php |
15
|
|
|
* protected function execute(InputInterface $input, OutputInterface $output) |
16
|
|
|
* { |
17
|
|
|
* if (!$this->isAllowedToPerformAction()) { |
18
|
|
|
* $this->stderr('Error: ' . ExitCode::getReason(ExitCode::NOPERM)); |
19
|
|
|
* return ExitCode::NOPERM; |
20
|
|
|
* } |
21
|
|
|
* |
22
|
|
|
* // do something |
23
|
|
|
* |
24
|
|
|
* return ExitCode::OK; |
25
|
|
|
* } |
26
|
|
|
* ``` |
27
|
|
|
* |
28
|
|
|
* @see https://man.openbsd.org/sysexits |
29
|
|
|
*/ |
30
|
|
|
class ExitCode |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* The command completed successfully. |
34
|
|
|
*/ |
35
|
|
|
public const OK = 0; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The command exited with an error code that says nothing about the error. |
39
|
|
|
*/ |
40
|
|
|
public const UNSPECIFIED_ERROR = 1; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The command was used incorrectly, e.g., with the wrong number of |
44
|
|
|
* arguments, a bad flag, a bad syntax in a parameter, or whatever. |
45
|
|
|
*/ |
46
|
|
|
public const USAGE = 64; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The input data was incorrect in some way. This should only be used for |
50
|
|
|
* user's data and not system files. |
51
|
|
|
*/ |
52
|
|
|
public const DATAERR = 65; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* An input file (not a system file) did not exist or was not readable. |
56
|
|
|
* This could also include errors like ``No message'' to a mailer (if it |
57
|
|
|
* cared to catch it). |
58
|
|
|
*/ |
59
|
|
|
public const NOINPUT = 66; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* The user specified did not exist. This might be used for mail addresses |
63
|
|
|
* or remote logins. |
64
|
|
|
*/ |
65
|
|
|
public const NOUSER = 67; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* The host specified did not exist. This is used in mail addresses or |
69
|
|
|
* network requests. |
70
|
|
|
*/ |
71
|
|
|
public const NOHOST = 68; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* A service is unavailable. This can occur if a support program or file |
75
|
|
|
* does not exist. This can also be used as a catchall message when |
76
|
|
|
* something you wanted to do does not work, but you do not know why. |
77
|
|
|
*/ |
78
|
|
|
public const UNAVAILABLE = 69; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* An internal software error has been detected. This should be limited to |
82
|
|
|
* non-operating system related errors as possible. |
83
|
|
|
*/ |
84
|
|
|
public const SOFTWARE = 70; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* An operating system error has been detected. This is intended to be |
88
|
|
|
* used for such things as ``cannot fork'', ``cannot create pipe'', or the |
89
|
|
|
* like. It includes things like getuid returning a user that does not |
90
|
|
|
* exist in the passwd file. |
91
|
|
|
*/ |
92
|
|
|
public const OSERR = 71; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Some system file (e.g., /etc/passwd, /var/run/utx.active, etc.) does not |
96
|
|
|
* exist, cannot be opened, or has some sort of error (e.g., syntax error). |
97
|
|
|
*/ |
98
|
|
|
public const OSFILE = 72; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* A (user specified) output file cannot be created. |
102
|
|
|
*/ |
103
|
|
|
public const CANTCREAT = 73; |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* An error occurred while doing I/O on some file. |
107
|
|
|
*/ |
108
|
|
|
public const IOERR = 74; |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Temporary failure, indicating something that is not really an error. In |
112
|
|
|
* sendmail, this means that a mailer (e.g.) could not create a connection, |
113
|
|
|
* and the request should be reattempted later. |
114
|
|
|
*/ |
115
|
|
|
public const TEMPFAIL = 75; |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* The remote system returned something that was ``not possible'' during a |
119
|
|
|
* protocol exchange. |
120
|
|
|
*/ |
121
|
|
|
public const PROTOCOL = 76; |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* You did not have sufficient permission to perform the operation. This |
125
|
|
|
* is not intended for file system problems, which should use NOINPUT or |
126
|
|
|
* CANTCREAT, but rather for higher level permissions. |
127
|
|
|
*/ |
128
|
|
|
public const NOPERM = 77; |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Something was found in an unconfigured or misconfigured state. |
132
|
|
|
*/ |
133
|
|
|
public const CONFIG = 78; |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @var array<int, string> A map of reason descriptions for exit codes. |
137
|
|
|
*/ |
138
|
|
|
public static array $reasons = [ |
139
|
|
|
self::OK => 'Success', |
140
|
|
|
self::UNSPECIFIED_ERROR => 'Unspecified error', |
141
|
|
|
self::USAGE => 'Incorrect usage, argument or option error', |
142
|
|
|
self::DATAERR => 'Error in input data', |
143
|
|
|
self::NOINPUT => 'Input file not found or unreadable', |
144
|
|
|
self::NOUSER => 'User not found', |
145
|
|
|
self::NOHOST => 'Host not found', |
146
|
|
|
self::UNAVAILABLE => 'A requied service is unavailable', |
147
|
|
|
self::SOFTWARE => 'Internal error', |
148
|
|
|
self::OSERR => 'Error making system call or using OS service', |
149
|
|
|
self::OSFILE => 'Error accessing system file', |
150
|
|
|
self::CANTCREAT => 'Cannot create output file', |
151
|
|
|
self::IOERR => 'I/O error', |
152
|
|
|
self::TEMPFAIL => 'Temporary failure', |
153
|
|
|
self::PROTOCOL => 'Unexpected remote service behavior', |
154
|
|
|
self::NOPERM => 'Insufficient permissions', |
155
|
|
|
self::CONFIG => 'Configuration error', |
156
|
|
|
]; |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Returns a short reason text for the given exit code. |
160
|
|
|
* |
161
|
|
|
* This method uses {@see $reasons} to determine the reason for an exit code. |
162
|
|
|
* |
163
|
|
|
* @param int $exitCode One of the constants defined in this class. |
164
|
|
|
* |
165
|
|
|
* @return string The reason text, or `"Unknown exit code"` if the code is not listed in {@see $reasons}. |
166
|
|
|
*/ |
167
|
17 |
|
public static function getReason(int $exitCode): string |
168
|
|
|
{ |
169
|
17 |
|
return static::$reasons[$exitCode] ?? 'Unknown exit code'; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|