|
1 | | -# Utopia CLI |
| 1 | +# Utopia Console |
2 | 2 |
|
3 | | -[](https://travis-ci.com/utopia-php/cli) |
4 | | - |
5 | | -[](https://appwrite.io/discord) |
| 3 | +Small collection of helpers for working with PHP command line applications. The Console class focuses on everyday needs such as logging, prompting users for input, executing external commands, and building long-running daemons. |
6 | 4 |
|
7 | | -Utopia framework CLI library is simple and lite library for extending Utopia PHP Framework to be able to code command line applications. This library is aiming to be as simple and easy to learn and use. This library is maintained by the [Appwrite team](https://appwrite.io). |
| 5 | +## Installation |
8 | 6 |
|
9 | | -Although this library is part of the [Utopia Framework](https://github.com/utopia-php/framework) project it is dependency free and can be used as standalone with any other PHP project or framework. |
| 7 | +Install using Composer: |
10 | 8 |
|
11 | | -## Getting Started |
12 | | - |
13 | | -Install using composer: |
14 | 9 | ```bash |
15 | | -composer require utopia-php/cli |
| 10 | +composer require utopia-php/console |
16 | 11 | ``` |
17 | 12 |
|
18 | | -script.php |
| 13 | +## Usage |
| 14 | + |
19 | 15 | ```php |
20 | 16 | <?php |
21 | | -require_once './vendor/autoload.php'; |
22 | | - |
23 | | -use Utopia\CLI\CLI; |
24 | | -use Utopia\CLI\Console; |
25 | | -use Utopia\CLI\Adapters\Generic; |
26 | | -use Utopia\Http\Validator\Wildcard; |
27 | | - |
28 | | -$cli = new CLI(new Generic()); |
| 17 | +require_once __DIR__.'/vendor/autoload.php'; |
29 | 18 |
|
30 | | -$cli |
31 | | - ->task('command-name') |
32 | | - ->param('email', null, new Wildcard()) |
33 | | - ->action(function ($email) { |
34 | | - Console::success($email); |
35 | | - }); |
36 | | - |
37 | | -$cli->run(); |
38 | | - |
39 | | -``` |
| 19 | +use Utopia\Console; |
40 | 20 |
|
41 | | -And than, run from command line: |
| 21 | +Console::success('Ready to work!'); |
42 | 22 |
|
43 | | -```bash |
44 | | -php script.php command-name --email=me@example.com |
45 | | -``` |
| 23 | +$answer = Console::confirm('Continue? [y/N]'); |
46 | 24 |
|
47 | | -### Hooks |
| 25 | +if ($answer !== 'y') { |
| 26 | + Console::warning('Aborting...'); |
| 27 | + Console::exit(1); |
| 28 | +} |
48 | 29 |
|
49 | | -There are three types of hooks, init hooks, shutdown hooks and error hooks. Init hooks are executed before the task is executed. Shutdown hook is executed after task is executed before application shuts down. Finally error hooks are executed whenever there's an error in the application lifecycle. You can provide multiple hooks for each stage. |
| 30 | +$output = ''; |
| 31 | +$exitCode = Console::execute('php -r "echo \"Hello\";"', '', $output, 3); |
50 | 32 |
|
51 | | -```php |
52 | | -require_once __DIR__ . '/../../vendor/autoload.php'; |
53 | | - |
54 | | -use Utopia\CLI\CLI; |
55 | | -use Utopia\CLI\Console; |
56 | | -use Utopia\Http\Validator\Wildcard; |
57 | | - |
58 | | -CLI::setResource('res1', function() { |
59 | | - return 'resource 1'; |
60 | | -}) |
61 | | - |
62 | | -CLI::init() |
63 | | - inject('res1') |
64 | | - ->action(function($res1) { |
65 | | - Console::info($res1); |
66 | | - }); |
67 | | - |
68 | | -CLI::error() |
69 | | - ->inject('error') |
70 | | - ->action(function($error) { |
71 | | - Console::error('Error occurred ' . $error); |
72 | | - }); |
73 | | - |
74 | | -$cli = new CLI(); |
75 | | - |
76 | | -$cli |
77 | | - ->task('command-name') |
78 | | - ->param('email', null, new Wildcard()) |
79 | | - ->action(function ($email) { |
80 | | - Console::success($email); |
81 | | - }); |
82 | | - |
83 | | -$cli->run(); |
| 33 | +Console::log("Command returned {$exitCode} with: {$output}"); |
84 | 34 | ``` |
85 | 35 |
|
86 | 36 | ### Log Messages |
87 | 37 |
|
88 | 38 | ```php |
89 | | -Console::log('Plain Log'); // stdout |
90 | | -``` |
91 | | - |
92 | | -```php |
93 | | -Console::success('Green log message'); // stdout |
94 | | -``` |
95 | | - |
96 | | -```php |
97 | | -Console::info('Blue log message'); // stdout |
98 | | -``` |
99 | | - |
100 | | -```php |
101 | | -Console::warning('Yellow log message'); // stderr |
102 | | -``` |
103 | | - |
104 | | -```php |
105 | | -Console::error('Red log message'); // stderr |
| 39 | +Console::log('Plain log'); // stdout |
| 40 | +Console::success('Green log'); // stdout |
| 41 | +Console::info('Blue log'); // stdout |
| 42 | +Console::warning('Yellow log'); // stderr |
| 43 | +Console::error('Red log'); // stderr |
106 | 44 | ``` |
107 | 45 |
|
108 | 46 | ### Execute Commands |
109 | 47 |
|
110 | | -Function returns exit code (0 - OK, >0 - error) and writes stdout, stderr to reference variables. The timeout variable allows you to limit the number of seconds the command can run. |
| 48 | +`Console::execute()` returns the exit code and writes the combined stdout/stderr output into the third argument. Pass a timeout (in seconds) to stop long-running processes and an optional progress callback to stream intermediate output. |
111 | 49 |
|
112 | 50 | ```php |
113 | | -$stdout = ''; |
114 | | -$stderr = ''; |
115 | | -$stdin = ''; |
116 | | -$timeout = 3; // seconds |
117 | | -$code = Console::execute('>&1 echo "success"', $stdin, $stdout, $stderr, $timeout); |
118 | | - |
119 | | -echo $code; // 0 |
120 | | -echo $stdout; // 'success' |
121 | | -echo $stderr; // '' |
122 | | -``` |
| 51 | +$output = ''; |
| 52 | +$input = ''; |
| 53 | +$exitCode = Console::execute('>&1 echo "success"', $input, $output, 3); |
123 | 54 |
|
124 | | -```php |
125 | | -$stdout = ''; |
126 | | -$stderr = ''; |
127 | | -$stdin = ''; |
128 | | -$timeout = 3; // seconds |
129 | | -$code = Console::execute('>&2 echo "error"', $stdin, $stdout, $stderr, $timeout); |
130 | | - |
131 | | -echo $code; // 0 |
132 | | -echo $stdout; // '' |
133 | | -echo $stderr; // 'error' |
| 55 | +echo $exitCode; // 0 |
| 56 | +echo $output; // "success\n" |
134 | 57 | ``` |
135 | 58 |
|
136 | 59 | ### Create a Daemon |
137 | 60 |
|
138 | | -You can use the `Console::loop` command to create your PHP daemon. The `loop` method already handles CPU consumption using a configurable sleep function and calls the PHP garbage collector every 5 minutes. |
| 61 | +Use `Console::loop()` to build daemons without tight loops. The helper sleeps between iterations and periodically triggers garbage collection. |
139 | 62 |
|
140 | 63 | ```php |
141 | 64 | <?php |
142 | 65 |
|
143 | | -use Utopia\CLI\Console; |
| 66 | +use Utopia\Console; |
144 | 67 |
|
145 | | -include './vendor/autoload.php'; |
146 | | - |
147 | | -Console::loop(function() { |
| 68 | +Console::loop(function () { |
148 | 69 | echo "Hello World\n"; |
149 | | -}, 1 /* 1 second */); |
| 70 | +}, 1); // 1 second |
150 | 71 | ``` |
151 | 72 |
|
152 | 73 | ## System Requirements |
153 | 74 |
|
154 | | -Utopia Framework requires PHP 7.4 or later. We recommend using the latest PHP version whenever possible. |
155 | | - |
156 | | - |
| 75 | +Utopia Console requires PHP 7.4 or later. We recommend using the latest PHP version whenever possible. |
157 | 76 |
|
158 | | -## Copyright and license |
| 77 | +## License |
159 | 78 |
|
160 | 79 | The MIT License (MIT) [http://www.opensource.org/licenses/mit-license.php](http://www.opensource.org/licenses/mit-license.php) |
0 commit comments