You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/getting-started/app/cli.md
+15-14Lines changed: 15 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,12 +2,6 @@
2
2
3
3
This package allows you to build scripts in a more structured way.
4
4
5
-
## Installation
6
-
7
-
```sh
8
-
composer require innmind/cli:~3.6
9
-
```
10
-
11
5
## Usage
12
6
13
7
```php title="cli.php"
@@ -21,9 +15,10 @@ use Innmind\CLI\{
21
15
Environment,
22
16
};
23
17
use Innmind\OperatingSystem\OperatingSystem;
18
+
use Innmind\Immutable\Attempt;
24
19
25
20
new class extends Main {
26
-
protected function main(Environment $env, OperatingSystem $os): Environment
21
+
protected function main(Environment $env, OperatingSystem $os): Attempt
27
22
{
28
23
return $env->output(Str::of("Hello world\n"));
29
24
}
@@ -36,7 +31,7 @@ You should already be familiar with the `$os` variable by now, if not go the [de
36
31
37
32
The `$env` variable is the abstraction to deal with everything inputed in your script and every output that comes out. It behaves like an immutable object, meaning you **must** always use the new instance returned by its methods.
38
33
39
-
To change the returned exit code you can do `return $env->exit(1)`.
34
+
To change the returned exit code you can do `$env->exit(1)`.
40
35
41
36
If you only have one action in your script you can do everything in the `main` method. But if you want to expose multiple commands you can do:
42
37
@@ -45,14 +40,16 @@ use Innmind\CLI\{
45
40
Commands,
46
41
Console,
47
42
Command,
43
+
Command\Usage,
48
44
};
45
+
use Innmind\Immutable\Attempt;
49
46
50
47
new class extends Main {
51
48
protected function main(Environment $env, OperatingSystem $os): Environment
52
49
{
53
50
$commands = Commands::of(
54
51
new class implements Command {
55
-
public function __invoke(Console $console): Console
52
+
public function __invoke(Console $console): Attempt
56
53
{
57
54
return $console->output(
58
55
Str::of('Hello ')->append(
@@ -61,13 +58,13 @@ new class extends Main {
61
58
);
62
59
}
63
60
64
-
public function usage(): string
61
+
public function usage(): Usage
65
62
{
66
-
return 'greet name';
63
+
return Usage::parse('greet name');
67
64
}
68
65
},
69
66
new class implements Command {
70
-
public function __invoke(Console $console): Console
67
+
public function __invoke(Console $console): Attempt
71
68
{
72
69
return $console->output(
73
70
Str::of($console->arguments()->get('name'))
@@ -76,9 +73,9 @@ new class extends Main {
76
73
);
77
74
}
78
75
79
-
public function usage(): string
76
+
public function usage(): Usage
80
77
{
81
-
return 'shout name';
78
+
return Usage::parse('shout name');
82
79
}
83
80
},
84
81
);
@@ -92,3 +89,7 @@ If you run `php cli.php greet Jane` it will output `Hello Jane` and if you run `
92
89
93
90
??? info
94
91
For simplicity this example use anonymous classes but you can use any class as long as it implements `Command`.
92
+
93
+
## Advanced usage
94
+
95
+
Full documentation available [here](http://innmind.org/CLI/).
Copy file name to clipboardExpand all lines: docs/getting-started/concurrency/async.md
+23-37Lines changed: 23 additions & 37 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,27 +2,19 @@
2
2
3
3
Since Innmind offers to access all I/O operations through the [operating system abstraction](../operating-system/index.md), it can easily execute these operations asynchronously.
4
4
5
-
## Installation
6
-
7
-
```sh
8
-
composer require innmind/mantle:~2.1
9
-
```
10
-
11
5
## Usage
12
6
13
-
Mantle works a bit like a reduce operation. The _reducer_ function allows to launch `Task`s and react to their results. Both the _reducer_ and tasks are run asynchronously.
7
+
Async works a bit like a reduce operation. The _reducer_ function allows to launch tasks and react to their results. Both the _reducer_ and tasks are run asynchronously.
14
8
15
9
=== "Script"
16
10
```php
17
-
use Innmind\Mantle\Forerunner;
11
+
use Innmind\Async\Scheduler;
18
12
use Innmind\Http\Response;
19
13
use Innmind\Immutable\Sequence;
20
14
21
-
$run = Forerunner::of($os);
22
-
$responses = $run(
23
-
Carried::new(),
24
-
new Reducer,
25
-
);
15
+
$responses = Scheduler::of($os)
16
+
->sink(Carried::new())
17
+
->with(new Reducer);
26
18
$responses; // instance of Sequence<?Response>
27
19
```
28
20
@@ -79,10 +71,7 @@ Mantle works a bit like a reduce operation. The _reducer_ function allows to lau
79
71
80
72
=== "Reducer"
81
73
```php
82
-
use Innmind\Mantle\{
83
-
Source\Continuation,
84
-
Task,
85
-
};
74
+
use Innmind\Async\Scope\Continuation;
86
75
use Innmind\OperatingSystem\OperatingSystem;
87
76
use Innmind\Http\Response;
88
77
use Innmind\Immutable\Sequence;
@@ -91,7 +80,7 @@ Mantle works a bit like a reduce operation. The _reducer_ function allows to lau
91
80
{
92
81
/**
93
82
* @param Continuation<Carried> $continuation
94
-
* @param Sequence<?Response> $results
83
+
* @param Sequence<mixed> $results
95
84
*
96
85
* @return Continuation<Carried>
97
86
*/
@@ -104,18 +93,14 @@ Mantle works a bit like a reduce operation. The _reducer_ function allows to lau
@@ -131,19 +116,21 @@ Mantle works a bit like a reduce operation. The _reducer_ function allows to lau
131
116
if ($carried->responses()->size() === 2) {
132
117
return $continuation
133
118
->carryWith($carried)
134
-
->terminate(); #(5)
119
+
->finish(); #(5)
135
120
}
136
121
137
-
return $continuation->carryWith($carried);
122
+
return $continuation
123
+
->carryWith($carried)
124
+
->wakeOnResult();
138
125
}
139
126
}
140
127
```
141
128
142
-
1. This `$os` variable is a new instance built by Mantle and runs asynchronously.
129
+
1. This `$os` variable is a new instance built by Async and runs asynchronously.
143
130
2. This will contain the values returned by the tasks as soon as available.
144
131
3. We flip the flag in order to not launch the tasks each time the reducer is called.
145
-
4. The `$os` variable below is a dedicated new instance for each task.
146
-
5. This tells Mantle to stop calling the reducer and return the carried value.
132
+
4. The `$os` variable is a dedicated new instance for each task.
133
+
5. This tells Async to stop calling the reducer and return the carried value.
147
134
148
135
This `__invoke` method will be called once when starting the runner and then each time a task finishes.
149
136
@@ -186,18 +173,17 @@ Mantle works a bit like a reduce operation. The _reducer_ function allows to lau
186
173
The first big advantage of this design is that your task is completely unaware that it is run asynchronously. It all depends on the `$os` variable injected (1). This means that you can easily experiment a piece of your program in an async context by what code calls it, your program logic itself doesn't have to change!
187
174
{.annotate}
188
175
189
-
1. If it comes from Mantle it's async otherwise it's sync.
176
+
1. If it comes from Async it's async otherwise it's sync.
190
177
191
178
The side effect of this is that you can test your code synchronously even though it's run asynchronously.
192
179
193
180
The other advantage is that since all state is local you can compose async code inside sync code transparently. You can't affect a global state since there is none.
194
181
195
182
## Limitations
196
183
197
-
- CLI signals are currently not supported in this context
198
184
- HTTP calls are currently done via `cURL` and uses micro sleeps instead of watching sockets
199
185
- SQL queries are still run synchronously for now
200
-
- It seems there is a limit of 10k concurrent tasks before performance degradation
186
+
- It seems there is a limit of 100k concurrent tasks before performance degradation
201
187
202
188
Most of these limitations are planned to be fixed in the future.
0 commit comments