Skip to content

Commit f77104e

Browse files
committed
chore: extract console
1 parent e41c8d0 commit f77104e

File tree

13 files changed

+84
-1302
lines changed

13 files changed

+84
-1302
lines changed

README.md

Lines changed: 35 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,160 +1,79 @@
1-
# Utopia CLI
1+
# Utopia Console
22

3-
[![Build Status](https://travis-ci.org/utopia-php/cli.svg?branch=master)](https://travis-ci.com/utopia-php/cli)
4-
![Total Downloads](https://img.shields.io/packagist/dt/utopia-php/cli.svg)
5-
[![Discord](https://img.shields.io/discord/564160730845151244)](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.
64

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
86

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:
108

11-
## Getting Started
12-
13-
Install using composer:
149
```bash
15-
composer require utopia-php/cli
10+
composer require utopia-php/console
1611
```
1712

18-
script.php
13+
## Usage
14+
1915
```php
2016
<?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';
2918

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;
4020

41-
And than, run from command line:
21+
Console::success('Ready to work!');
4222

43-
```bash
44-
php script.php command-name --email=me@example.com
45-
```
23+
$answer = Console::confirm('Continue? [y/N]');
4624

47-
### Hooks
25+
if ($answer !== 'y') {
26+
Console::warning('Aborting...');
27+
Console::exit(1);
28+
}
4829

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);
5032

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}");
8434
```
8535

8636
### Log Messages
8737

8838
```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
10644
```
10745

10846
### Execute Commands
10947

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.
11149

11250
```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);
12354

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"
13457
```
13558

13659
### Create a Daemon
13760

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.
13962

14063
```php
14164
<?php
14265

143-
use Utopia\CLI\Console;
66+
use Utopia\Console;
14467

145-
include './vendor/autoload.php';
146-
147-
Console::loop(function() {
68+
Console::loop(function () {
14869
echo "Hello World\n";
149-
}, 1 /* 1 second */);
70+
}, 1); // 1 second
15071
```
15172

15273
## System Requirements
15374

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.
15776

158-
## Copyright and license
77+
## License
15978

16079
The MIT License (MIT) [http://www.opensource.org/licenses/mit-license.php](http://www.opensource.org/licenses/mit-license.php)

composer.json

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
2-
"name": "utopia-php/cli",
3-
"description": "A simple CLI library to manage command line applications",
2+
"name": "utopia-php/console",
3+
"description": "Console helpers for logging, prompting, and executing commands",
44
"type": "library",
5-
"keywords": ["php","framework", "upf", "utopia", "cli", "command line"],
5+
"keywords": ["php", "utopia", "console", "cli", "terminal"],
66
"license": "MIT",
77
"scripts": {
88
"test": "vendor/bin/phpunit --configuration phpunit.xml < tests/input.txt",
@@ -11,12 +11,10 @@
1111
"format": "vendor/bin/pint"
1212
},
1313
"autoload": {
14-
"psr-4": {"Utopia\\CLI\\": "src/CLI"}
14+
"psr-4": {"Utopia\\": "src/"}
1515
},
1616
"require": {
17-
"php": ">=7.4",
18-
"utopia-php/framework": "1.0.*",
19-
"utopia-php/di": "0.1.*"
17+
"php": ">=7.4"
2018
},
2119
"require-dev": {
2220
"phpunit/phpunit": "^9.3",

0 commit comments

Comments
 (0)