Skip to content

Commit 3155fc6

Browse files
authored
Merge pull request #291 from ray-di/provider-serialisable
Serializable provider provider
2 parents f6f39ef + 2c209a6 commit 3155fc6

File tree

8 files changed

+66
-29
lines changed

8 files changed

+66
-29
lines changed

.github/workflows/coding-standards.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ jobs:
99
cs:
1010
uses: ray-di/.github/.github/workflows/coding-standards.yml@v1
1111
with:
12-
php_version: 8.1
12+
php_version: 8.3

.github/workflows/demo-php8.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
- name: Setup PHP
1717
uses: shivammathur/setup-php@v2
1818
with:
19-
php-version: 8.1
19+
php-version: 8.3
2020
tools: cs2pr
2121
coverage: none
2222

.github/workflows/static-analysis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ jobs:
99
sa:
1010
uses: ray-di/.github/.github/workflows/static-analysis.yml@v1
1111
with:
12-
php_version: 8.1
12+
php_version: 8.3

phpstan.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ parameters:
99
- tests/di/tmp/*
1010
- tests/Di/tmp/*
1111
- tests/di/Fake/*
12+
- tests/di/Ray_Di_*
1213
ignoreErrors:
1314
- '#contains generic class ReflectionAttribute but does not specify its types:#'
1415
- '#generic class ReflectionAttribute but does not specify its types:#'

src/di/ProviderProvider.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Ray\Di;
6+
7+
use Ray\Di\Di\Set;
8+
9+
/**
10+
* @implements ProviderInterface<mixed>
11+
* @template T of object
12+
*/
13+
final class ProviderProvider implements ProviderInterface
14+
{
15+
/** @var InjectorInterface */
16+
private $injector;
17+
18+
/** @var Set<T> */
19+
private $set;
20+
21+
/** @param Set<T> $set */
22+
public function __construct(InjectorInterface $injector, Set $set)
23+
{
24+
$this->injector = $injector;
25+
$this->set = $set;
26+
}
27+
28+
/** @return mixed */
29+
public function get()
30+
{
31+
return $this->injector->getInstance($this->set->interface, $this->set->name);
32+
}
33+
}

src/di/ProviderSetProvider.php

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -48,30 +48,6 @@ public function get()
4848
throw new SetNotFound((string) $this->ip->getParameter());
4949
}
5050

51-
return new class ($this->injector, $set) implements ProviderInterface
52-
{
53-
/** @var InjectorInterface */
54-
private $injector;
55-
56-
/** @var Set<object> */
57-
private $set;
58-
59-
/**
60-
* @param Set<object> $set
61-
*/
62-
public function __construct(InjectorInterface $injector, Set $set)
63-
{
64-
$this->injector = $injector;
65-
$this->set = $set;
66-
}
67-
68-
/**
69-
* @return mixed
70-
*/
71-
public function get()
72-
{
73-
return $this->injector->getInstance($this->set->interface, $this->set->name);
74-
}
75-
};
51+
return new ProviderProvider($this->injector, $set);
7652
}
7753
}

tests/di/ProviderProviderTest.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 Ray\Di;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Ray\Di\Di\Set;
9+
10+
class ProviderProviderTest extends TestCase
11+
{
12+
public function testGet(): void
13+
{
14+
$injector = new Injector(
15+
new class extends AbstractModule {
16+
protected function configure()
17+
{
18+
$this->bind(FakeEngineInterface::class)->toInstance(new FakeEngine());
19+
}
20+
}
21+
);
22+
$set = new Set(FakeEngineInterface::class);
23+
$provider = new ProviderProvider($injector, $set);
24+
$instance = $provider->get();
25+
$this->assertInstanceOf(FakeEngine::class, $instance);
26+
}
27+
}

tests/di/VisitorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class VisitorTest extends TestCase
3030
public function setUp(): void
3131
{
3232
$this->visitor = new NullVisitor();
33-
$this->container = (new ContainerFactory())(new FakeCarModule(), __DIR__);
33+
$this->container = (new ContainerFactory())(new FakeCarModule(), __DIR__ . '/tmp');
3434
$container = $this->container->getContainer();
3535
$dependency = $container['Ray\Di\FakeCarInterface-'];
3636
assert($dependency instanceof Dependency);

0 commit comments

Comments
 (0)