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

Commit 36892e4

Browse files
authored
Merge pull request #3 from reactphp-parallel/code
Add Code
2 parents 7332bd8 + f16197e commit 36892e4

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed

src/ContainerProxy.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ReactParallel\Psr11ContainerProxy;
6+
7+
use Psr\Container\ContainerInterface;
8+
use ReactParallel\ObjectProxy\Proxy;
9+
10+
final class ContainerProxy
11+
{
12+
private ContainerInterface $proxy;
13+
14+
public function __construct(ContainerInterface $container, Proxy $proxy)
15+
{
16+
/**
17+
* @psalm-suppress PropertyTypeCoercion
18+
* @phpstan-ignore-next-line
19+
*/
20+
$this->proxy = $proxy->create($container, ContainerInterface::class);
21+
}
22+
23+
public function create(ContainerInterface $container): ContainerInterface
24+
{
25+
return new ThreadContainerProxy($container, $this->proxy);
26+
}
27+
}

src/ThreadContainerProxy.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ReactParallel\Psr11ContainerProxy;
6+
7+
use Psr\Container\ContainerInterface;
8+
use ReactParallel\ObjectProxy\Generated\ProxyList;
9+
10+
use function array_key_exists;
11+
12+
final class ThreadContainerProxy extends ProxyList implements ContainerInterface
13+
{
14+
private ContainerInterface $local;
15+
private ContainerInterface $remote;
16+
17+
public function __construct(ContainerInterface $local, ContainerInterface $remote)
18+
{
19+
$this->local = $local;
20+
$this->remote = $remote;
21+
}
22+
23+
// phpcs:disable
24+
public function has($id)
25+
{
26+
return array_key_exists($id, self::KNOWN_INTERFACE) || $this->local->has($id);
27+
}
28+
29+
// phpcs:disable
30+
public function get($id)
31+
{
32+
if (array_key_exists($id, self::KNOWN_INTERFACE)) {
33+
return $this->remote->get($id);
34+
}
35+
36+
return $this->local->get($id);
37+
}
38+
}

tests/ContainerProxyTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ReactParallel\Tests\Psr11ContainerProxy;
6+
7+
use React\EventLoop\Factory as EventLoopFactory;
8+
use ReactParallel\Factory;
9+
use ReactParallel\ObjectProxy\Proxy;
10+
use ReactParallel\Psr11ContainerProxy\ContainerProxy;
11+
use ReactParallel\Psr11ContainerProxy\ThreadContainerProxy;
12+
use WyriHaximus\AsyncTestUtilities\AsyncTestCase;
13+
use Yuloh\Container\Container;
14+
15+
final class ContainerProxyTest extends AsyncTestCase
16+
{
17+
/**
18+
* @test
19+
*/
20+
public function create(): void
21+
{
22+
$loop = EventLoopFactory::create();
23+
$factory = new Factory($loop);
24+
$proxy = new Proxy($factory);
25+
$containerProxy = new ContainerProxy(new Container(), $proxy);
26+
27+
self::assertInstanceOf(ThreadContainerProxy::class, $containerProxy->create(new Container()));
28+
}
29+
}

tests/ThreadContainerProxyTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ReactParallel\Tests\Psr11ContainerProxy;
6+
7+
use Monolog\Logger;
8+
use Psr\Container\ContainerInterface;
9+
use Psr\Log\LoggerInterface;
10+
use ReactParallel\Psr11ContainerProxy\ThreadContainerProxy;
11+
use stdClass;
12+
use WyriHaximus\AsyncTestUtilities\AsyncTestCase;
13+
14+
final class ThreadContainerProxyTest extends AsyncTestCase
15+
{
16+
/**
17+
* @test
18+
*/
19+
public function logger(): void
20+
{
21+
$std = new stdClass();
22+
$logger = new Logger('monolog');
23+
$local = $this->prophesize(ContainerInterface::class);
24+
$local->has('fake')->shouldBeCalled()->willReturn(true);
25+
$local->get('fake')->shouldBeCalled()->willReturn($std);
26+
$local->has(LoggerInterface::class)->shouldNotBeCalled();
27+
$local->get(LoggerInterface::class)->shouldNotBeCalled();
28+
$remote = $this->prophesize(ContainerInterface::class);
29+
$remote->has('fake')->shouldNotBeCalled();
30+
$remote->get('fake')->shouldNotBeCalled();
31+
$remote->get(LoggerInterface::class)->shouldBeCalled()->willReturn($logger);
32+
33+
$proxy = new ThreadContainerProxy($local->reveal(), $remote->reveal());
34+
35+
self::assertTrue($proxy->has('fake'));
36+
self::assertTrue($proxy->has(LoggerInterface::class));
37+
self::assertSame($std, $proxy->get('fake'));
38+
self::assertSame($logger, $proxy->get(LoggerInterface::class));
39+
}
40+
}

0 commit comments

Comments
 (0)