Skip to content

Commit 06b6e26

Browse files
authored
Merge pull request #50 from bigcommerce/revert-48-findable-container
Revert "Add findable container for single-lookup dependency resolution"
2 parents e9ba8f9 + 8511de9 commit 06b6e26

4 files changed

Lines changed: 4 additions & 94 deletions

File tree

src/Adapter/ArrayContainerAdapter.php

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
namespace Bigcommerce\Injector\Adapter;
33

44
use Bigcommerce\Injector\Adapter\Exception\ServiceNotFoundException;
5-
use Bigcommerce\Injector\FindableContainerInterface;
65
use Psr\Container\ContainerExceptionInterface;
6+
use Psr\Container\ContainerInterface;
77
use Psr\Container\NotFoundExceptionInterface;
88

99
/**
1010
* Adapt a simple array container (i.e Pimple) to ContainerInterop Interface
1111
* @package Bigcommerce\Injector\Adapter
1212
*/
13-
class ArrayContainerAdapter implements FindableContainerInterface
13+
class ArrayContainerAdapter implements ContainerInterface
1414
{
1515
/**
1616
* @var array|\ArrayAccess
@@ -56,17 +56,4 @@ public function has($id): bool
5656
{
5757
return isset($this->arrayContainer[$id]);
5858
}
59-
60-
/**
61-
* Return the entry for the given id, or NULL if it isn't in the container. A single lookup, so callers can skip the
62-
* separate ->has() call. Matches ->has()/->get(): a stored null is treated as absent (isset semantics), so a
63-
* present entry is never NULL
64-
*
65-
* @param string $id
66-
* @return mixed
67-
*/
68-
public function find(string $id): mixed
69-
{
70-
return $this->arrayContainer[$id] ?? null;
71-
}
7259
}

src/FindableContainerInterface.php

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/Injector.php

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,8 @@ class Injector implements InjectorInterface
4343
*/
4444
private array $autoCreateCache = [];
4545

46-
/**
47-
* The container as a FindableContainerInterface when it supports find(), else NULL. Lets us resolve a dependency in
48-
* a single lookup instead of ->has() + ->get(). Resolved once in the constructor as it can't change for a given
49-
* container
50-
*/
51-
private readonly ?FindableContainerInterface $findableContainer;
52-
5346
public function __construct(private readonly ContainerInterface $container, private readonly ClassInspectorInterface $classInspector)
5447
{
55-
$this->findableContainer = $container instanceof FindableContainerInterface ? $container : null;
5648
}
5749

5850
/**
@@ -252,12 +244,7 @@ private function buildParameterArrayFromContainer($methodSignature)
252244
}
253245
$type = $parameterData['type'] ?? false;
254246
if ($type) {
255-
$service = $this->findableContainer?->find($type);
256-
if ($service !== null) {
257-
$parameters[$position] = $service;
258-
continue;
259-
}
260-
if ($this->findableContainer === null && $this->container->has($type)) {
247+
if ($this->container->has($type)) {
261248
$parameters[$position] = $this->container->get($type);
262249
continue;
263250
}
@@ -317,12 +304,7 @@ private function resolveParameter($position, $parameterData, &$providedParameter
317304
unset($providedParameters[$type]);
318305
return $result;
319306
}
320-
$service = $this->findableContainer?->find($type);
321-
if ($service !== null) {
322-
// Found the dependency by type in the container
323-
return $service;
324-
}
325-
if ($this->findableContainer === null && $this->container->has($type)) {
307+
if ($this->container->has($type)) {
326308
// Found the dependency by type in the container
327309
return $this->container->get($type);
328310
}

tests/InjectorTest.php

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22
namespace Tests;
33

4-
use Bigcommerce\Injector\Adapter\ArrayContainerAdapter;
54
use Bigcommerce\Injector\Exception\InjectorInvocationException;
65
use Bigcommerce\Injector\Injector;
76
use Bigcommerce\Injector\Reflection\ClassInspector;
@@ -246,42 +245,6 @@ public function testAutoCreate()
246245
$this->assertEquals(25, $instance->getAge());
247246
}
248247

249-
public function testResolvesDependencyFromFindableContainer()
250-
{
251-
$dep = new DummySubDependency();
252-
$this->mockDummyDependencySignature();
253-
254-
$injector = new Injector(
255-
new ArrayContainerAdapter([DummySubDependency::class => $dep]),
256-
$this->inspector->reveal()
257-
);
258-
259-
$instance = $injector->create(DummyDependency::class);
260-
$this->assertSame($dep, $instance->getDependency());
261-
}
262-
263-
public function testFindableContainerAutoCreatesWhenDependencyAbsent()
264-
{
265-
$this->mockDummyDependencySignature();
266-
$this->mockDummySubDependencySignature();
267-
268-
$injector = new Injector(new ArrayContainerAdapter([]), $this->inspector->reveal());
269-
$injector->addAutoCreate(".*DummySubDependency");
270-
271-
$instance = $injector->create(DummyDependency::class);
272-
$this->assertInstanceOf(DummySubDependency::class, $instance->getDependency());
273-
}
274-
275-
public function testFindableContainerThrowsForMissingRequiredDependency()
276-
{
277-
$this->mockDummyDependencySignature();
278-
279-
$injector = new Injector(new ArrayContainerAdapter([]), $this->inspector->reveal());
280-
281-
$this->expectException(InjectorInvocationException::class);
282-
$injector->create(DummyDependency::class);
283-
}
284-
285248
/**
286249
* Injector fails to create a sub-dependency. Should provided a wrapped stack exception message guiding
287250
* developers where to find the issue.

0 commit comments

Comments
 (0)