Skip to content

Commit b2df244

Browse files
ChinaMoliyunwuxin
authored andcommitted
修改 容器中存在提供的类时才进行注入,否则使用默认值
1 parent a288cfb commit b2df244

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

src/Container.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -500,13 +500,10 @@ protected function getObjectParam(string $className, array &$vars, ReflectionPar
500500
$result = $value;
501501
array_shift($vars);
502502
} else {
503-
try {
503+
if ($param->isDefaultValueAvailable()) {
504+
$result = $this->bound($className) ? $this->make($className) : $param->getDefaultValue();
505+
} else {
504506
$result = $this->make($className);
505-
} catch (Throwable $th) {
506-
if (!$param->isDefaultValueAvailable()) {
507-
throw $th;
508-
}
509-
$result = $param->getDefaultValue();
510507
}
511508
}
512509

tests/ContainerTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -340,10 +340,13 @@ public function testInvokeClassNotExists()
340340
public function testInvokeWithDefaultValues()
341341
{
342342
$container = $this->resolveContainer();
343-
$container->bind(Container::class, $container);
344343

345-
$class = $container->make(WithDefaultValues::class);
346-
$this->assertSame($container, $class->container);
344+
$class = $container->invokeClass(WithDefaultValues::class);
345+
$this->assertSame(null, $class->container);
346+
347+
$container->bind(Container::class, $container);
348+
$bound = $container->invokeClass(WithDefaultValues::class);
349+
$this->assertSame($container, $bound->container);
347350

348351
$this->assertSame(null, $container->invokeMethod(WithDefaultValues::class . '::classExistsButCannotBeInjected'));
349352
}

0 commit comments

Comments
 (0)