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