1: <?php declare(strict_types=1);
2:
3: namespace Salient\Contract\Console;
4:
5: use Psr\Log\LoggerInterface;
6: use Salient\Contract\Console\Target\StreamTargetInterface;
7: use Salient\Contract\Console\Target\TargetInterface;
8: use Salient\Contract\Core\Instantiable;
9: use Salient\Contract\HasMessageLevel;
10: use Salient\Contract\HasMessageLevels;
11: use Throwable;
12:
13: /**
14: * @api
15: */
16: interface ConsoleInterface extends
17: Instantiable,
18: HasMessageLevel,
19: HasMessageLevels,
20: HasMessageType,
21: HasMessageTypes,
22: HasTargetFlag
23: {
24: /**
25: * Get a PSR-3 logger backed by the console
26: */
27: public function logger(): LoggerInterface;
28:
29: /**
30: * Register a target to receive output with each of the given message levels
31: * from the console
32: *
33: * @param array<ConsoleInterface::LEVEL_*> $levels
34: * @return $this
35: */
36: public function registerTarget(
37: TargetInterface $target,
38: array $levels = ConsoleInterface::LEVELS_ALL
39: );
40:
41: /**
42: * Deregister a target if it is currently registered with the console
43: *
44: * @return $this
45: */
46: public function deregisterTarget(TargetInterface $target);
47:
48: /**
49: * Register STDERR for console output if running on the command line
50: *
51: * Messages with level `LEVEL_DEBUG` are written to `STDERR` if:
52: *
53: * - `$debug` is `true`, or
54: * - `$debug` is `null` and debug mode is enabled in the environment
55: *
56: * @return $this
57: */
58: public function registerStderrTarget(?bool $debug = null);
59:
60: /**
61: * Register STDOUT and STDERR for console output if running on the command
62: * line
63: *
64: * If the level of a message is `LEVEL_WARNING` or lower, it is written to
65: * `STDERR`, otherwise it is written to `STDOUT`.
66: *
67: * Messages with level `LEVEL_DEBUG` are written to `STDOUT` if:
68: *
69: * - `$debug` is `true`, or
70: * - `$debug` is `null` and debug mode is enabled in the environment
71: *
72: * @return $this
73: */
74: public function registerStdioTargets(?bool $debug = null);
75:
76: /**
77: * Set or unset the prefix applied to each line of output by registered
78: * targets that implement HasPrefix after optionally filtering them by flag
79: *
80: * @param int-mask-of<ConsoleInterface::TARGET_*> $targetFlags
81: * @return $this
82: */
83: public function setPrefix(?string $prefix, int $targetFlags = 0);
84:
85: /**
86: * Get registered targets, optionally filtering them by message level and
87: * flag
88: *
89: * @param ConsoleInterface::LEVEL_*|null $level
90: * @param int-mask-of<ConsoleInterface::TARGET_*> $targetFlags
91: * @return TargetInterface[]
92: */
93: public function getTargets(?int $level = null, int $targetFlags = 0): array;
94:
95: /**
96: * Get a target for STDOUT, creating an unregistered one if necessary
97: */
98: public function getStdoutTarget(): StreamTargetInterface;
99:
100: /**
101: * Get a target for STDERR, creating an unregistered one if necessary
102: */
103: public function getStderrTarget(): StreamTargetInterface;
104:
105: /**
106: * Get a TTY target, creating an unregistered one if necessary
107: *
108: * Returns a target for `STDERR` if neither or both of `STDOUT` and `STDERR`
109: * refer to an interactive terminal.
110: */
111: public function getTtyTarget(): StreamTargetInterface;
112:
113: /**
114: * Escape inline formatting tags in a string so it can be safely used in a
115: * console message
116: */
117: public function escape(string $string, bool $escapeNewlines = false): string;
118:
119: /**
120: * Remove escapes from inline formatting tags in a string
121: */
122: public function removeEscapes(string $string): string;
123:
124: /**
125: * Remove inline formatting tags from a string
126: */
127: public function removeTags(string $string): string;
128:
129: /**
130: * Print "! $msg1 $msg2" or similar with level LEVEL_ERROR
131: *
132: * @param string $msg1 May use inline formatting tags (see {@see escape()}).
133: * @param string|null $msg2 Inline formatting tags have no special meaning.
134: * @return $this
135: */
136: public function error(
137: string $msg1,
138: ?string $msg2 = null,
139: ?Throwable $ex = null,
140: bool $count = true
141: );
142:
143: /**
144: * Print "! $msg1 $msg2" or similar with level LEVEL_ERROR once per run
145: *
146: * @param string $msg1 May use inline formatting tags (see {@see escape()}).
147: * @param string|null $msg2 Inline formatting tags have no special meaning.
148: * @return $this
149: */
150: public function errorOnce(
151: string $msg1,
152: ?string $msg2 = null,
153: ?Throwable $ex = null,
154: bool $count = true
155: );
156:
157: /**
158: * Print "^ $msg1 $msg2" or similar with level LEVEL_WARNING
159: *
160: * @param string $msg1 May use inline formatting tags (see {@see escape()}).
161: * @param string|null $msg2 Inline formatting tags have no special meaning.
162: * @return $this
163: */
164: public function warn(
165: string $msg1,
166: ?string $msg2 = null,
167: ?Throwable $ex = null,
168: bool $count = true
169: );
170:
171: /**
172: * Print "^ $msg1 $msg2" or similar with level LEVEL_WARNING once per run
173: *
174: * @param string $msg1 May use inline formatting tags (see {@see escape()}).
175: * @param string|null $msg2 Inline formatting tags have no special meaning.
176: * @return $this
177: */
178: public function warnOnce(
179: string $msg1,
180: ?string $msg2 = null,
181: ?Throwable $ex = null,
182: bool $count = true
183: );
184:
185: /**
186: * Increase message indentation level and print "» $msg1 $msg2" or similar
187: * with level LEVEL_NOTICE
188: *
189: * If `$endMsg1` or `$endMsg2` are not `null`, `"« $endMsg1 $endMsg2"` or
190: * similar is printed with level `LEVEL_NOTICE` when {@see groupEnd()} is
191: * called to close the group.
192: *
193: * @param string $msg1 May use inline formatting tags (see {@see escape()}).
194: * @param string|null $msg2 Inline formatting tags have no special meaning.
195: * @param string|null $endMsg1 May use inline formatting tags.
196: * @param string|null $endMsg2 Inline formatting tags have no special
197: * meaning.
198: * @return $this
199: */
200: public function group(
201: string $msg1,
202: ?string $msg2 = null,
203: ?string $endMsg1 = null,
204: ?string $endMsg2 = null
205: );
206:
207: /**
208: * Close the group of messages most recently opened with group()
209: *
210: * @return $this
211: */
212: public function groupEnd();
213:
214: /**
215: * Print "> $msg1 $msg2" or similar with level LEVEL_NOTICE
216: *
217: * @param string $msg1 May use inline formatting tags (see {@see escape()}).
218: * @param string|null $msg2 Inline formatting tags have no special meaning.
219: * @return $this
220: */
221: public function info(string $msg1, ?string $msg2 = null);
222:
223: /**
224: * Print "> $msg1 $msg2" or similar with level LEVEL_NOTICE once per run
225: *
226: * @param string $msg1 May use inline formatting tags (see {@see escape()}).
227: * @param string|null $msg2 Inline formatting tags have no special meaning.
228: * @return $this
229: */
230: public function infoOnce(string $msg1, ?string $msg2 = null);
231:
232: /**
233: * Print "- $msg1 $msg2" or similar with level LEVEL_INFO
234: *
235: * @param string $msg1 May use inline formatting tags (see {@see escape()}).
236: * @param string|null $msg2 Inline formatting tags have no special meaning.
237: * @return $this
238: */
239: public function log(string $msg1, ?string $msg2 = null);
240:
241: /**
242: * Print "- $msg1 $msg2" or similar with level LEVEL_INFO once per run
243: *
244: * @param string $msg1 May use inline formatting tags (see {@see escape()}).
245: * @param string|null $msg2 Inline formatting tags have no special meaning.
246: * @return $this
247: */
248: public function logOnce(string $msg1, ?string $msg2 = null);
249:
250: /**
251: * Print "⠋ $msg1 $msg2" or similar with level LEVEL_INFO to TTY targets
252: * without moving to the next line
253: *
254: * May be called repeatedly to display progress updates without disrupting
255: * other console messages or bloating output logs.
256: *
257: * @param string $msg1 May use inline formatting tags (see {@see escape()}).
258: * @param string|null $msg2 Inline formatting tags have no special meaning.
259: * @return $this
260: */
261: public function logProgress(string $msg1, ?string $msg2 = null);
262:
263: /**
264: * Clear the message most recently printed with logProgress()
265: *
266: * @return $this
267: */
268: public function clearProgress();
269:
270: /**
271: * Print ": <caller> $msg1 $msg2" or similar with level LEVEL_DEBUG
272: *
273: * @param string $msg1 May use inline formatting tags (see {@see escape()}).
274: * @param string|null $msg2 Inline formatting tags have no special meaning.
275: * @param int $depth To print your caller's name instead of your own, set
276: * `$depth` to 1.
277: * @return $this
278: */
279: public function debug(
280: string $msg1,
281: ?string $msg2 = null,
282: ?Throwable $ex = null,
283: int $depth = 0
284: );
285:
286: /**
287: * Print ": <caller> $msg1 $msg2" or similar with level LEVEL_DEBUG once per
288: * run
289: *
290: * @param string $msg1 May use inline formatting tags (see {@see escape()}).
291: * @param string|null $msg2 Inline formatting tags have no special meaning.
292: * @param int $depth To print your caller's name instead of your own, set
293: * `$depth` to 1.
294: * @return $this
295: */
296: public function debugOnce(
297: string $msg1,
298: ?string $msg2 = null,
299: ?Throwable $ex = null,
300: int $depth = 0
301: );
302:
303: /**
304: * Print "$msg1 $msg2" or similar with level- and type-based formatting
305: *
306: * @param string $msg1 May use inline formatting tags (see {@see escape()}).
307: * @param string|null $msg2 Inline formatting tags have no special meaning.
308: * @param ConsoleInterface::LEVEL_* $level
309: * @param ConsoleInterface::TYPE_* $type
310: * @return $this
311: */
312: public function message(
313: string $msg1,
314: ?string $msg2 = null,
315: int $level = ConsoleInterface::LEVEL_INFO,
316: int $type = ConsoleInterface::TYPE_UNDECORATED,
317: ?Throwable $ex = null,
318: bool $count = true
319: );
320:
321: /**
322: * Print "$msg1 $msg2" or similar with level- and type-based formatting once
323: * per run
324: *
325: * @param string $msg1 May use inline formatting tags (see {@see escape()}).
326: * @param string|null $msg2 Inline formatting tags have no special meaning.
327: * @param ConsoleInterface::LEVEL_* $level
328: * @param ConsoleInterface::TYPE_* $type
329: * @return $this
330: */
331: public function messageOnce(
332: string $msg1,
333: ?string $msg2 = null,
334: int $level = ConsoleInterface::LEVEL_INFO,
335: int $type = ConsoleInterface::TYPE_UNDECORATED,
336: ?Throwable $ex = null,
337: bool $count = true
338: );
339:
340: /**
341: * Print an exception and its stack trace with the given message levels
342: *
343: * @param ConsoleInterface::LEVEL_* $level
344: * @param ConsoleInterface::LEVEL_*|null $traceLevel If `null`, the
345: * exception's stack trace is not printed.
346: * @return $this
347: */
348: public function exception(
349: Throwable $exception,
350: int $level = ConsoleInterface::LEVEL_ERROR,
351: ?int $traceLevel = ConsoleInterface::LEVEL_DEBUG,
352: bool $count = true
353: );
354:
355: /**
356: * Print a "command finished" message with a summary of errors, warnings and
357: * resource usage
358: *
359: * @param string $finishedText May use inline formatting tags (see
360: * {@see escape()}).
361: * @param string $successText May use inline formatting tags.
362: * @return $this
363: */
364: public function summary(
365: string $finishedText = 'Command finished',
366: string $successText = 'without errors',
367: bool $withResourceUsage = false,
368: bool $withoutErrorsAndWarnings = false,
369: bool $withGenericType = false
370: );
371:
372: /**
373: * Print "$msg" to registered targets
374: *
375: * @param string $msg May use inline formatting tags (see {@see escape()}).
376: * @param ConsoleInterface::LEVEL_* $level
377: * @return $this
378: */
379: public function print(
380: string $msg,
381: int $level = ConsoleInterface::LEVEL_INFO
382: );
383:
384: /**
385: * Print "$msg" to registered targets that write to STDOUT or STDERR
386: *
387: * @param string $msg May use inline formatting tags (see {@see escape()}).
388: * @param ConsoleInterface::LEVEL_* $level
389: * @return $this
390: */
391: public function printStdio(
392: string $msg,
393: int $level = ConsoleInterface::LEVEL_INFO
394: );
395:
396: /**
397: * Print "$msg" to registered targets that write to a TTY
398: *
399: * @param string $msg May use inline formatting tags (see {@see escape()}).
400: * @param ConsoleInterface::LEVEL_* $level
401: * @return $this
402: */
403: public function printTty(
404: string $msg,
405: int $level = ConsoleInterface::LEVEL_INFO
406: );
407:
408: /**
409: * Print "$msg" to STDOUT, creating an unregistered target if necessary
410: *
411: * @param string $msg May use inline formatting tags (see {@see escape()}).
412: * @param ConsoleInterface::LEVEL_* $level
413: * @return $this
414: */
415: public function printStdout(
416: string $msg,
417: int $level = ConsoleInterface::LEVEL_INFO
418: );
419:
420: /**
421: * Record a message with the given level without printing anything
422: *
423: * @param ConsoleInterface::LEVEL_* $level
424: * @return $this
425: */
426: public function count(int $level);
427:
428: /**
429: * Get the number of error messages recorded so far
430: */
431: public function errors(): int;
432:
433: /**
434: * Get the number of warning messages recorded so far
435: */
436: public function warnings(): int;
437: }
438: