Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.

Commit 580ff70

Browse files
authored
Merge pull request #4 from reactphp-parallel/add-example
Add Example
2 parents 36892e4 + de77da6 commit 580ff70

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
},
1717
"require-dev": {
1818
"monolog/monolog": "^2.1",
19+
"psr/log": "^1.1",
1920
"wyrihaximus/async-test-utilities": "^2.3",
2021
"yuloh/container": "^1.0"
2122
},

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/logger.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Monolog\Handler\StreamHandler;
6+
use Monolog\Logger;
7+
use Psr\Log\LoggerInterface;
8+
use React\EventLoop\Factory;
9+
use ReactParallel\Factory as ParallelFactory;
10+
use ReactParallel\ObjectProxy\Proxy;
11+
use ReactParallel\Psr11ContainerProxy\ContainerProxy;
12+
use Yuloh\Container\Container;
13+
14+
require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor/autoload.php';
15+
16+
$loop = Factory::create();
17+
$parallelFactory = new ParallelFactory($loop);
18+
$container = new Container();
19+
$container->set(LoggerInterface::class, static fn (): LoggerInterface => new Logger('main', [new StreamHandler(STDOUT)]));
20+
$proxy = new ContainerProxy($container, new Proxy($parallelFactory));
21+
$parallelFactory->call(static function (ContainerProxy $proxy, int $time): int {
22+
$container = new Container();
23+
$container->set('logger', static fn (): LoggerInterface => new Logger('child', [new StreamHandler(STDOUT)]));
24+
$containerProxy = $proxy->create($container);
25+
$containerProxy->get('logger')->critical('Time: ' . $time);
26+
$containerProxy->get(LoggerInterface::class)->critical('Time: ' . $time);
27+
28+
return $time;
29+
}, [$proxy, time()])->then(static function (int $time) use ($parallelFactory): void {
30+
echo 'Time: ', $time;
31+
$parallelFactory->lowLevelPool()->kill();
32+
})->done();
33+
34+
$loop->run();

phpcs.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
<file>src</file>
1010
<file>tests</file>
11+
<file>examples</file>
1112

1213
<rule ref="WyriHaximus-OSS" />
1314
</ruleset>

0 commit comments

Comments
 (0)