Skip to content

Commit e03e225

Browse files
committed
Rework lazy definition
1 parent f034ae0 commit e03e225

File tree

3 files changed

+37
-16
lines changed

3 files changed

+37
-16
lines changed

src/LazyDefinitionDecorator.php renamed to src/LazyDefinition.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,24 @@
99
use Yiisoft\Definitions\Contract\DefinitionInterface;
1010
use Yiisoft\Di\Helpers\DefinitionNormalizer;
1111

12-
final class LazyDefinitionDecorator implements DefinitionInterface
12+
final class LazyDefinition implements DefinitionInterface
1313
{
1414
public function __construct(
15-
private LazyLoadingValueHolderFactory $factory,
1615
private mixed $definition,
1716
private string $objectClass,
1817
) {
1918
}
2019

2120
public function resolve(ContainerInterface $container): mixed
2221
{
23-
return $this->factory->createProxy(
24-
$this->objectClass,
25-
function (&$wrappedObject) use ($container) {
26-
$definition = DefinitionNormalizer::normalize($this->definition, $this->objectClass);
22+
$factory = $container->get(LazyLoadingValueHolderFactory::class);
23+
$definition = $this->definition;
24+
$objectClass = $this->objectClass;
2725

26+
return $factory->createProxy(
27+
$objectClass,
28+
function (&$wrappedObject) use ($container, $objectClass, $definition) {
29+
$definition = DefinitionNormalizer::normalize($definition, $objectClass);
2830
$wrappedObject = $definition->resolve($container);
2931
}
3032
);

tests/Unit/ArrayDefinitionTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use InvalidArgumentException;
88
use PHPUnit\Framework\TestCase;
9+
use TypeError;
910
use Yiisoft\Definitions\ArrayDefinition;
1011
use Yiisoft\Definitions\Exception\InvalidConfigException;
1112
use Yiisoft\Definitions\Reference;
@@ -523,4 +524,19 @@ public function testMagicMethods(): void
523524
$object->getEvents()
524525
);
525526
}
527+
528+
public function testNonArrayArguments(): void
529+
{
530+
$definition = ArrayDefinition::fromConfig([
531+
ArrayDefinition::CLASS_NAME => Mouse::class,
532+
'setNameAndEngine()' => 'kitty',
533+
]);
534+
$container = new SimpleContainer();
535+
536+
$this->expectException(TypeError::class);
537+
$this->expectExceptionMessage(
538+
'Yiisoft\Definitions\ArrayDefinition::resolveFunctionArguments(): Argument #3 ($arguments) must be of type array, string given'
539+
);
540+
$definition->resolve($container);
541+
}
526542
}

tests/Unit/LazyDefinitionDecoratorTest.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use ProxyManager\Factory\LazyLoadingValueHolderFactory;
1010
use ProxyManager\Proxy\LazyLoadingInterface;
1111
use Yiisoft\Definitions\ArrayDefinition;
12-
use Yiisoft\Definitions\LazyDefinitionDecorator;
12+
use Yiisoft\Definitions\LazyDefinition;
1313
use Yiisoft\Definitions\Tests\Support\EngineInterface;
1414
use Yiisoft\Definitions\Tests\Support\NotFinalClass;
1515
use Yiisoft\Definitions\Tests\Support\Phone;
@@ -19,31 +19,33 @@ final class LazyDefinitionDecoratorTest extends TestCase
1919
{
2020
public function testDecorateFinalClass(): void
2121
{
22-
$container = new SimpleContainer();
23-
$factory = new LazyLoadingValueHolderFactory();
22+
$container = new SimpleContainer([
23+
LazyLoadingValueHolderFactory::class => new LazyLoadingValueHolderFactory(),
24+
]);
2425

2526
$class = Phone::class;
2627

2728
$definition = ArrayDefinition::fromConfig([
2829
ArrayDefinition::CLASS_NAME => $class,
2930
]);
30-
$definition = new LazyDefinitionDecorator($factory, $definition, $class);
31+
$definition = new LazyDefinition($definition, $class);
3132

3233
$this->expectException(InvalidProxiedClassException::class);
3334
$definition->resolve($container);
3435
}
3536

3637
public function testDecorateNotFinalClass(): void
3738
{
38-
$container = new SimpleContainer();
39-
$factory = new LazyLoadingValueHolderFactory();
39+
$container = new SimpleContainer([
40+
LazyLoadingValueHolderFactory::class => new LazyLoadingValueHolderFactory(),
41+
]);
4042

4143
$class = NotFinalClass::class;
4244

4345
$definition = ArrayDefinition::fromConfig([
4446
ArrayDefinition::CLASS_NAME => $class,
4547
]);
46-
$definition = new LazyDefinitionDecorator($factory, $definition, $class);
48+
$definition = new LazyDefinition($definition, $class);
4749

4850
$phone = $definition->resolve($container);
4951

@@ -52,15 +54,16 @@ public function testDecorateNotFinalClass(): void
5254

5355
public function testDecorateInterface(): void
5456
{
55-
$container = new SimpleContainer();
56-
$factory = new LazyLoadingValueHolderFactory();
57+
$container = new SimpleContainer([
58+
LazyLoadingValueHolderFactory::class => new LazyLoadingValueHolderFactory(),
59+
]);
5760

5861
$class = EngineInterface::class;
5962

6063
$definition = ArrayDefinition::fromConfig([
6164
ArrayDefinition::CLASS_NAME => $class,
6265
]);
63-
$definition = new LazyDefinitionDecorator($factory, $definition, $class);
66+
$definition = new LazyDefinition($definition, $class);
6467

6568
$phone = $definition->resolve($container);
6669

0 commit comments

Comments
 (0)