This repository was archived by the owner on Feb 17, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +134
-0
lines changed
Expand file tree Collapse file tree 4 files changed +134
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments