1: <?php declare(strict_types=1);
2:
3: namespace Salient\Contract\Cli;
4:
5: use Salient\Contract\Console\ConsoleInterface as Console;
6: use Salient\Contract\Container\ApplicationInterface;
7: use LogicException;
8:
9: /**
10: * A service container for CLI applications
11: *
12: * @api
13: */
14: interface CliApplicationInterface extends ApplicationInterface
15: {
16: /**
17: * Get the name of the file used to run the application
18: */
19: public function getProgramName(): string;
20:
21: /**
22: * Get the command invoked by run()
23: *
24: * This method should only return a command that is currently running.
25: */
26: public function getRunningCommand(): ?CliCommandInterface;
27:
28: /**
29: * Get the command most recently invoked by run()
30: *
31: * This method should only return a command that ran to completion or failed
32: * with an exception.
33: */
34: public function getLastCommand(): ?CliCommandInterface;
35:
36: /**
37: * Get the return value most recently recorded by run()
38: *
39: * This method should return `0` if a return value has not been recorded.
40: */
41: public function getLastExitStatus(): int;
42:
43: /**
44: * Register a command with the container
45: *
46: * @param string[] $name The name of the command as an array of subcommands.
47: * Valid subcommands start with a letter, followed by any number of letters,
48: * numbers, hyphens and underscores.
49: * @param class-string<CliCommandInterface> $id
50: * @return $this
51: * @throws LogicException if `$name` is invalid or conflicts with a
52: * registered command.
53: */
54: public function command(array $name, string $id);
55:
56: /**
57: * Register one, and only one, command for the lifetime of the container
58: *
59: * Calling this method should have the same effect as calling
60: * {@see CliApplicationInterface::command()} with an empty command name.
61: *
62: * @param class-string<CliCommandInterface> $id
63: * @return $this
64: * @throws LogicException if another command has already been registered.
65: */
66: public function oneCommand(string $id);
67:
68: /**
69: * Process command line arguments passed to the script and record a return
70: * value
71: *
72: * This method should take the first applicable action:
73: *
74: * - If `--help` is the only remaining argument after processing subcommand
75: * arguments, print a help message to `STDOUT`. Return value: `0`
76: *
77: * - If `--version` is the only remaining argument, print the application's
78: * name, version and commit reference to `STDOUT`, followed by the PHP
79: * version. Return value: `0`
80: *
81: * - If subcommand arguments resolve to a registered command, create an
82: * instance of the command and run it. Return value: command exit status
83: *
84: * - If, after processing subcommand arguments, there are no further
85: * arguments but there are further subcommands, print a one-line synopsis
86: * of each registered subcommand. Return value: `0`
87: *
88: * - Report an error and print a one-line synopsis of each registered
89: * subcommand. Return value: `1`
90: *
91: * @return $this
92: */
93: public function run();
94:
95: /**
96: * Exit with the return value most recently recorded by run()
97: *
98: * This method should use exit status `0` if a return value has not been
99: * recorded.
100: *
101: * @return never
102: */
103: public function exit();
104:
105: /**
106: * Process command line arguments passed to the script and exit with the
107: * recorded return value
108: *
109: * See {@see CliApplicationInterface::run()} for details.
110: *
111: * @return never
112: */
113: public function runAndExit();
114:
115: /**
116: * Print the application's name, version and commit reference, followed by
117: * the PHP version
118: *
119: * @param Console::LEVEL_* $level
120: * @return $this
121: */
122: public function reportVersion(int $level = Console::LEVEL_INFO, bool $stdout = false);
123:
124: /**
125: * Get the application's name, version and commit reference, followed by the
126: * PHP version
127: *
128: * This method should return the string that
129: * {@see CliApplicationInterface::reportVersion()} would print.
130: */
131: public function getVersionString(): string;
132: }
133: