Skip to content

Commit a76545c

Browse files
committed
accept predefined constructor arguments
1 parent b605c05 commit a76545c

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/ServiceContainer.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Webdevcave\Yadic;
46

57
use Closure;
@@ -49,14 +51,15 @@ public function __construct(CacheInterface $cache = null)
4951
*
5052
* @template T
5153
*
52-
* @param T $id Identifier of the entry to look for.
54+
* @param T $id Identifier of the entry to look for.
55+
* @param array $arguments Predefined arguments.
5356
*
5457
* @throws NotFoundExceptionInterface No entry was found for **this** identifier.
5558
* @throws ContainerExceptionInterface Error while retrieving the entry.
5659
*
5760
* @return T
5861
*/
59-
public function get(string $id): mixed
62+
public function get(string $id, array $arguments = []): mixed
6063
{
6164
if (isset($this->singletons[$id])) {
6265
return $this->singletons[$id];
@@ -69,10 +72,9 @@ public function get(string $id): mixed
6972
try {
7073
$className = $this->aliases[$id] ?? $id;
7174
$reflectionClass = new ReflectionClass($className);
72-
$arguments = [];
7375

7476
if ($constructor = $reflectionClass->getConstructor()) {
75-
$arguments = $this->createArguments($constructor);
77+
$arguments = $this->createArguments($constructor, $arguments);
7678
}
7779

7880
$instance = $reflectionClass->newInstanceArgs($arguments);
@@ -118,7 +120,8 @@ public function has(string $id): bool
118120
private function createArguments(
119121
ReflectionMethod|ReflectionFunction $reflectionMethod,
120122
array $arguments = []
121-
): array {
123+
): array
124+
{
122125
foreach ($reflectionMethod->getParameters() as $reflectionParameter) {
123126
$argumentName = $reflectionParameter->getName();
124127

0 commit comments

Comments
 (0)