Skip to content

Commit

Permalink
Merge pull request #5 from inpsyde/4_remove_object_restriction_on_ser…
Browse files Browse the repository at this point in the history
…vices

4 remove object restriction on services
  • Loading branch information
Chrico authored May 21, 2021
2 parents 279791e + 7fd0b5f commit e7de7c1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 20 deletions.
17 changes: 8 additions & 9 deletions src/Container/ReadOnlyContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class ReadOnlyContainer implements ContainerInterface
{
/**
* @var array<string, callable(\Psr\Container\ContainerInterface $container):object>
* @var array<string, callable(\Psr\Container\ContainerInterface $container):mixed>
*/
private $services;

Expand All @@ -20,14 +20,14 @@ class ReadOnlyContainer implements ContainerInterface
private $factoryIds = [];

/**
* @var array<string, array<callable(object, ContainerInterface $container):object>>
* @var array<string, array<callable(mixed, ContainerInterface $container):mixed>>
*/
private $extensions;

/**
* Resolved factories.
*
* @var array<string, object>
* @var array<string, mixed>
*/
private $resolvedServices = [];

Expand All @@ -39,9 +39,9 @@ class ReadOnlyContainer implements ContainerInterface
/**
* ReadOnlyContainer constructor.
*
* @param array<string, callable(ContainerInterface $container):object> $services
* @param array<string, callable(ContainerInterface $container):mixed> $services
* @param array<string, bool> $factoryIds
* @param array<string, array<callable(object, ContainerInterface $container):object>> $extensions
* @param array<string, array<callable(mixed, ContainerInterface $container):mixed>> $extensions
* @param ContainerInterface[] $containers
*/
public function __construct(
Expand Down Expand Up @@ -88,7 +88,6 @@ public function get($id)

foreach ($this->containers as $container) {
if ($container->has($id)) {
/** @var object $service */
$service = $container->get($id);

return $this->resolveExtensions($id, $service);
Expand Down Expand Up @@ -129,11 +128,11 @@ public function has($id)

/**
* @param string $id
* @param object $service
* @param mixed $service
*
* @return object
* @return mixed
*/
private function resolveExtensions(string $id, object $service): object
private function resolveExtensions(string $id, $service)
{
if (!isset($this->extensions[$id])) {
return $service;
Expand Down
46 changes: 35 additions & 11 deletions tests/unit/Container/ReadOnlyContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,48 @@ public function testGetUnknown(): void

/**
* @test
*
* @dataProvider provideServices
*/
public function testHasGetService(): void
public function testHasGetService($expected, callable $service): void
{
$expectedServiceKey = 'service';
$expectedValue = new \stdClass();
$services = [
'service' => static function () use ($expectedValue) {
return $expectedValue;
},
];
$expectedId = 'service';
$services = [$expectedId => $service];
$testee = $this->createContainer($services);

// check in Services
static::assertTrue($testee->has($expectedServiceKey));
static::assertTrue($testee->has($expectedId));
// resolve Service
static::assertSame($expectedValue, $testee->get($expectedServiceKey));
static::assertSame($expected, $testee->get($expectedId));
// check in Factories
static::assertTrue($testee->has($expectedServiceKey));
static::assertTrue($testee->has($expectedId));
}

public function provideServices(): \Generator
{
$service = new \stdClass();
yield 'object service' => [
$service,
function () use ($service) {
return $service;
},
];

$service = 'foo';
yield 'string service' => [
$service,
function () use ($service) {
return $service;
},
];

$service = ['foo', 'bar'];
yield 'array service' => [
$service,
function () use ($service) {
return $service;
},
];
}

/**
Expand Down

0 comments on commit e7de7c1

Please sign in to comment.