Skip to content

Commit 05cd936

Browse files
authored
Merge pull request #16 from utopia-php/fix-cli-readme
fixes for cli
2 parents 69e68f8 + f70cf33 commit 05cd936

3 files changed

Lines changed: 21 additions & 14 deletions

File tree

README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,17 @@ composer require utopia-php/cli
1818
script.php
1919
```php
2020
<?php
21-
2221
require_once './vendor/autoload.php';
2322

2423
use Utopia\CLI\CLI;
2524
use Utopia\CLI\Console;
26-
use Utopia\Validator\Email;
25+
use Utopia\Validator\Wildcard;
2726

2827
$cli = new CLI();
2928

3029
$cli
3130
->task('command-name')
32-
->param('email', null, new Email())
31+
->param('email', null, new Wildcard())
3332
->action(function ($email) {
3433
Console::success($email);
3534
});
@@ -70,8 +69,6 @@ Console::error('Red log message'); // stderr
7069

7170
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.
7271

73-
74-
7572
```php
7673
$stdout = '';
7774
$stderr = '';
@@ -109,7 +106,7 @@ include './vendor/autoload.php';
109106

110107
Console::loop(function() {
111108
echo "Hello World\n";
112-
}, 200000 /* 200ms */);
109+
}, 1 /* 1 second */);
113110
```
114111

115112
## System Requirements

src/CLI/Console.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,15 +194,15 @@ static public function isInteractive(): bool
194194

195195
/**
196196
* @param callable $callback
197-
* @param int $sleep in seconds
197+
* @param float $sleep // in seconds!
198198
* @param callable $onError
199199
*/
200-
static public function loop(callable $callback, $sleep = 1 /* 1 second */, callable $onError = null): void
200+
static public function loop(callable $callback, $sleep = 1 /* seconds */, callable $onError = null): void
201201
{
202202
gc_enable();
203203

204204
$time = 0;
205-
205+
206206
while (!connection_aborted() || PHP_SAPI == "cli") {
207207
try {
208208
$callback();
@@ -214,12 +214,21 @@ static public function loop(callable $callback, $sleep = 1 /* 1 second */, calla
214214
}
215215
}
216216

217-
sleep($sleep);
217+
$intSeconds = intval($sleep);
218+
$microSeconds = ($sleep - $intSeconds) * 1000000;
219+
220+
if($intSeconds > 0) {
221+
sleep($intSeconds);
222+
}
223+
224+
if($microSeconds > 0) {
225+
usleep($microSeconds);
226+
}
218227

219228
$time = $time + $sleep;
220229

221230
if (PHP_SAPI == "cli") {
222-
if($time >= (1000000 * 300)) { // Every 5 minutes
231+
if($time >= 60 * 5) { // Every 5 minutes
223232
$time = 0;
224233
gc_collect_cycles(); //Forces collection of any existing garbage cycles
225234
}

src/CLI/Task.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?php
2-
32
namespace Utopia\CLI;
43

4+
use Utopia\Validator;
5+
56
class Task
67
{
78
/**
@@ -80,13 +81,13 @@ public function action(callable $action): self
8081
*
8182
* @param string $key
8283
* @param mixed $default
83-
* @param string $validator
84+
* @param Validator $validator
8485
* @param string $description
8586
* @param bool $optional
8687
*
8788
* @return $this
8889
*/
89-
public function param(string $key, $default, $validator, string $description = '', bool $optional = false): self
90+
public function param(string $key, $default, Validator $validator, string $description = '', bool $optional = false): self
9091
{
9192
$this->params[$key] = array(
9293
'default' => $default,

0 commit comments

Comments
 (0)