Skip to content

Commit 68a52e0

Browse files
authored
Merge pull request #4 from Innmind/new-majors-wave
Documentation for the new major versions
2 parents f070dcb + 2b6bbb4 commit 68a52e0

40 files changed

Lines changed: 396 additions & 416 deletions

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Innmind documentation
22

3-
You can find the built version of this documentation at https://innmind.github.io/documentation/.
3+
You can find the built version of this documentation at <https://innmind.org/>.
44

5-
To view it on your machine, pull the repository and run `make serve` that will open the page http://0.0.0.0:8000/
5+
To view it on your machine, pull the repository and run `make serve` that will open the page <http://0.0.0.0:8000/>.

docs/getting-started/app/cli.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22

33
This package allows you to build scripts in a more structured way.
44

5-
## Installation
6-
7-
```sh
8-
composer require innmind/cli:~3.6
9-
```
10-
115
## Usage
126

137
```php title="cli.php"
@@ -21,9 +15,10 @@ use Innmind\CLI\{
2115
Environment,
2216
};
2317
use Innmind\OperatingSystem\OperatingSystem;
18+
use Innmind\Immutable\Attempt;
2419

2520
new class extends Main {
26-
protected function main(Environment $env, OperatingSystem $os): Environment
21+
protected function main(Environment $env, OperatingSystem $os): Attempt
2722
{
2823
return $env->output(Str::of("Hello world\n"));
2924
}
@@ -36,7 +31,7 @@ You should already be familiar with the `$os` variable by now, if not go the [de
3631

3732
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.
3833

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)`.
4035

4136
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:
4237

@@ -45,14 +40,16 @@ use Innmind\CLI\{
4540
Commands,
4641
Console,
4742
Command,
43+
Command\Usage,
4844
};
45+
use Innmind\Immutable\Attempt;
4946

5047
new class extends Main {
5148
protected function main(Environment $env, OperatingSystem $os): Environment
5249
{
5350
$commands = Commands::of(
5451
new class implements Command {
55-
public function __invoke(Console $console): Console
52+
public function __invoke(Console $console): Attempt
5653
{
5754
return $console->output(
5855
Str::of('Hello ')->append(
@@ -61,13 +58,13 @@ new class extends Main {
6158
);
6259
}
6360

64-
public function usage(): string
61+
public function usage(): Usage
6562
{
66-
return 'greet name';
63+
return Usage::parse('greet name');
6764
}
6865
},
6966
new class implements Command {
70-
public function __invoke(Console $console): Console
67+
public function __invoke(Console $console): Attempt
7168
{
7269
return $console->output(
7370
Str::of($console->arguments()->get('name'))
@@ -76,9 +73,9 @@ new class extends Main {
7673
);
7774
}
7875

79-
public function usage(): string
76+
public function usage(): Usage
8077
{
81-
return 'shout name';
78+
return Usage::parse('shout name');
8279
}
8380
},
8481
);
@@ -92,3 +89,7 @@ If you run `php cli.php greet Jane` it will output `Hello Jane` and if you run `
9289

9390
??? info
9491
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/).

docs/getting-started/app/http.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22

33
This package allows to build simple HTTP applications by representing requests and responses via objects.
44

5-
## Installation
6-
7-
```sh
8-
composer require innmind/http-server:~4.1
9-
```
10-
115
## Usage
126

137
```php title="index.php"
@@ -53,6 +47,7 @@ use Innmind\Http\{
5347
Headers,
5448
Header\ContentType,
5549
};
50+
use Innmind\MediaType\MediaType;
5651
use Innmind\Url\Path;
5752
use Innmind\Immutable\Predicate\Instance;
5853

@@ -70,12 +65,13 @@ new class extends Main {
7065
Response\StatusCode::ok,
7166
$request->protocolVersion(),
7267
Headers::of(
73-
ContentType::of('image', 'png'),
68+
ContentType::of(new MediaType('image', 'png')),
7469
),
7570
$this
7671
->os
7772
->filesystem()
7873
->mount(Path::of('images/'))
74+
->unwrap()
7975
->get(Name::of('some-image.png'))
8076
->keep(Instance::of(File::class))
8177
->match(

docs/getting-started/concurrency/async.md

Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,19 @@
22

33
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.
44

5-
## Installation
6-
7-
```sh
8-
composer require innmind/mantle:~2.1
9-
```
10-
115
## Usage
126

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

159
=== "Script"
1610
```php
17-
use Innmind\Mantle\Forerunner;
11+
use Innmind\Async\Scheduler;
1812
use Innmind\Http\Response;
1913
use Innmind\Immutable\Sequence;
2014

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);
2618
$responses; // instance of Sequence<?Response>
2719
```
2820

@@ -79,10 +71,7 @@ Mantle works a bit like a reduce operation. The _reducer_ function allows to lau
7971

8072
=== "Reducer"
8173
```php
82-
use Innmind\Mantle\{
83-
Source\Continuation,
84-
Task,
85-
};
74+
use Innmind\Async\Scope\Continuation;
8675
use Innmind\OperatingSystem\OperatingSystem;
8776
use Innmind\Http\Response;
8877
use Innmind\Immutable\Sequence;
@@ -91,7 +80,7 @@ Mantle works a bit like a reduce operation. The _reducer_ function allows to lau
9180
{
9281
/**
9382
* @param Continuation<Carried> $continuation
94-
* @param Sequence<?Response> $results
83+
* @param Sequence<mixed> $results
9584
*
9685
* @return Continuation<Carried>
9786
*/
@@ -104,18 +93,14 @@ Mantle works a bit like a reduce operation. The _reducer_ function allows to lau
10493
if ($carried->needsToLaunchTasks()) {
10594
return $continuation
10695
->carryWith($carried->tasksLaunched()) #(3)
107-
->launch(Sequence::of(
108-
Task::of( #(4)
109-
static fn(OperatingSystem $os) => MyTask::of(
110-
$os,
111-
'https://github.com/'
112-
),
96+
->schedule(Sequence::of(
97+
static fn(OperatingSystem $os) => MyTask::of( #(4)
98+
$os,
99+
'https://github.com/'
113100
),
114-
Task::of(
115-
static fn(OperatingSystem $os) => MyTask::of(
116-
$os,
117-
'https://gitlab.com/'
118-
),
101+
static fn(OperatingSystem $os) => MyTask::of(
102+
$os,
103+
'https://gitlab.com/'
119104
),
120105
));
121106
}
@@ -131,19 +116,21 @@ Mantle works a bit like a reduce operation. The _reducer_ function allows to lau
131116
if ($carried->responses()->size() === 2) {
132117
return $continuation
133118
->carryWith($carried)
134-
->terminate(); #(5)
119+
->finish(); #(5)
135120
}
136121

137-
return $continuation->carryWith($carried);
122+
return $continuation
123+
->carryWith($carried)
124+
->wakeOnResult();
138125
}
139126
}
140127
```
141128

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.
143130
2. This will contain the values returned by the tasks as soon as available.
144131
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.
147134

148135
This `__invoke` method will be called once when starting the runner and then each time a task finishes.
149136

@@ -186,18 +173,17 @@ Mantle works a bit like a reduce operation. The _reducer_ function allows to lau
186173
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!
187174
{.annotate}
188175

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

191178
The side effect of this is that you can test your code synchronously even though it's run asynchronously.
192179

193180
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.
194181

195182
## Limitations
196183

197-
- CLI signals are currently not supported in this context
198184
- HTTP calls are currently done via `cURL` and uses micro sleeps instead of watching sockets
199185
- 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
201187

202188
Most of these limitations are planned to be fixed in the future.
203189

docs/getting-started/concurrency/http.md

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,16 @@ You can configure the max concurrency at the start of your program and leave you
8484

8585
=== "Operating System"
8686
```php
87-
use Innmind\OperatingSystem\{
88-
Factory,
89-
Config,
90-
};
91-
92-
$os = Factory::build(
93-
Config::of()->limitHttpConcurrencyTo(20),
87+
use Innmind\OperatingSystem\Config;
88+
use Innmind\HttpTransport\Curl;
89+
90+
$os = $os->map(
91+
static fn($config) => $config->useHttpTransport(
92+
Curl::of(
93+
$os->clock(),
94+
$config->io(),
95+
)->maxConcurrency(20),
96+
),
9497
);
9598

9699
// rest of your script
@@ -103,8 +106,17 @@ You can configure the max concurrency at the start of your program and leave you
103106
Application,
104107
};
105108
use Innmind\OperatingSystem\Config;
109+
use Innmind\HttpTransport\Curl;
110+
111+
$config = Config::of();
112+
$config = $config->useHttpTransport(
113+
Curl::of(
114+
$config->clock(),
115+
$config->io(),
116+
)->maxConcurrency(20),
117+
);
106118

107-
new class(Config::of()->limitHttpConcurrencyTo(20)) extends Cli {
119+
new class($config) extends Cli {
108120
protected function configure(Application $app): Application
109121
{
110122
// configure your app here
@@ -125,8 +137,17 @@ You can configure the max concurrency at the start of your program and leave you
125137
OperatingSystem,
126138
Config,
127139
};
140+
use Innmind\HttpTransport\Curl;
141+
142+
$config = Config::of();
143+
$config = $config->useHttpTransport(
144+
Curl::of(
145+
$config->clock(),
146+
$config->io(),
147+
)->maxConcurrency(20),
148+
);
128149

129-
new class(Config::of()->limitHttpConcurrencyTo(20)) extends Main {
150+
new class($config) extends Main {
130151
protected function main(Environment $env, OperatingSystem $os): Environment
131152
{
132153
// your code here
@@ -145,8 +166,17 @@ You can configure the max concurrency at the start of your program and leave you
145166
Response,
146167
};
147168
use Innmind\OperatingSystem\Config;
169+
use Innmind\HttpTransport\Curl;
170+
171+
$config = Config::of();
172+
$config = $config->useHttpTransport(
173+
Curl::of(
174+
$config->clock(),
175+
$config->io(),
176+
)->maxConcurrency(20),
177+
);
148178

149-
new class(Config::of()->limitHttpConcurrencyTo(20)) extends Main {
179+
new class($config) extends Main {
150180
protected function main(ServerRequest $request): Response
151181
{
152182
// your code here

0 commit comments

Comments
 (0)