Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serializable provider provider #291

Merged
merged 9 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/coding-standards.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ jobs:
cs:
uses: ray-di/.github/.github/workflows/coding-standards.yml@v1
with:
php_version: 8.1
php_version: 8.3
2 changes: 1 addition & 1 deletion .github/workflows/demo-php8.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.1
php-version: 8.3
tools: cs2pr
coverage: none

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ jobs:
sa:
uses: ray-di/.github/.github/workflows/static-analysis.yml@v1
with:
php_version: 8.1
php_version: 8.3
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ parameters:
- tests/di/tmp/*
- tests/Di/tmp/*
- tests/di/Fake/*
- tests/di/Ray_Di_*
ignoreErrors:
- '#contains generic class ReflectionAttribute but does not specify its types:#'
- '#generic class ReflectionAttribute but does not specify its types:#'
30 changes: 30 additions & 0 deletions src/di/ProviderProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Ray\Di;

use Ray\Di\Di\Set;

/** @implements ProviderInterface<mixed> */
final class ProviderProvider implements ProviderInterface
{
/** @var InjectorInterface */
private $injector;

/** @var Set<object> */
private $set;

/** @param Set<object> $set */
public function __construct(InjectorInterface $injector, Set $set)
{
$this->injector = $injector;
$this->set = $set;
}

/** @return mixed */
public function get()
{
return $this->injector->getInstance($this->set->interface, $this->set->name);
}
}
koriym marked this conversation as resolved.
Show resolved Hide resolved
26 changes: 1 addition & 25 deletions src/di/ProviderSetProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,30 +48,6 @@ public function get()
throw new SetNotFound((string) $this->ip->getParameter());
}

return new class ($this->injector, $set) implements ProviderInterface
{
/** @var InjectorInterface */
private $injector;

/** @var Set<object> */
private $set;

/**
* @param Set<object> $set
*/
public function __construct(InjectorInterface $injector, Set $set)
{
$this->injector = $injector;
$this->set = $set;
}

/**
* @return mixed
*/
public function get()
{
return $this->injector->getInstance($this->set->interface, $this->set->name);
}
};
return new ProviderProvider($this->injector, $set);
}
}
27 changes: 27 additions & 0 deletions tests/di/ProviderProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Ray\Di;

use PHPUnit\Framework\TestCase;
use Ray\Di\Di\Set;

class ProviderProviderTest extends TestCase
{
public function testInvoke(): void
{
$injector = new Injector(
new class extends AbstractModule {
protected function configure()
{
$this->bind(FakeEngineInterface::class)->toInstance(new FakeEngine());
}
}
);
$set = new Set(FakeEngineInterface::class);
$provider = new ProviderProvider($injector, $set);

Check failure on line 23 in tests/di/ProviderProviderTest.php

View workflow job for this annotation

GitHub Actions / sa / PHPStan

Parameter #2 $set of class Ray\Di\ProviderProvider constructor expects Ray\Di\Di\Set<object>, Ray\Di\Di\Set<Ray\Di\FakeEngineInterface> given.

Check failure on line 23 in tests/di/ProviderProviderTest.php

View workflow job for this annotation

GitHub Actions / sa / PHPStan

Parameter #2 $set of class Ray\Di\ProviderProvider constructor expects Ray\Di\Di\Set<object>, Ray\Di\Di\Set<Ray\Di\FakeEngineInterface> given.
$instance = $provider->get();
$this->assertInstanceOf(FakeEngine::class, $instance);
}
koriym marked this conversation as resolved.
Show resolved Hide resolved
}
2 changes: 1 addition & 1 deletion tests/di/VisitorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class VisitorTest extends TestCase
public function setUp(): void
{
$this->visitor = new NullVisitor();
$this->container = (new ContainerFactory())(new FakeCarModule(), __DIR__);
$this->container = (new ContainerFactory())(new FakeCarModule(), __DIR__ . '/tmp');
$container = $this->container->getContainer();
$dependency = $container['Ray\Di\FakeCarInterface-'];
assert($dependency instanceof Dependency);
Expand Down
Loading