Skip to content

Commit

Permalink
Rework lazy definition
Browse files Browse the repository at this point in the history
  • Loading branch information
xepozz committed Dec 3, 2022
1 parent f034ae0 commit e03e225
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
14 changes: 8 additions & 6 deletions src/LazyDefinitionDecorator.php → src/LazyDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,24 @@
use Yiisoft\Definitions\Contract\DefinitionInterface;
use Yiisoft\Di\Helpers\DefinitionNormalizer;

final class LazyDefinitionDecorator implements DefinitionInterface
final class LazyDefinition implements DefinitionInterface
{
public function __construct(
private LazyLoadingValueHolderFactory $factory,
private mixed $definition,
private string $objectClass,
) {
}

public function resolve(ContainerInterface $container): mixed
{
return $this->factory->createProxy(
$this->objectClass,
function (&$wrappedObject) use ($container) {
$definition = DefinitionNormalizer::normalize($this->definition, $this->objectClass);
$factory = $container->get(LazyLoadingValueHolderFactory::class);
$definition = $this->definition;
$objectClass = $this->objectClass;

return $factory->createProxy(
$objectClass,
function (&$wrappedObject) use ($container, $objectClass, $definition) {
$definition = DefinitionNormalizer::normalize($definition, $objectClass);
$wrappedObject = $definition->resolve($container);
}
);
Expand Down
16 changes: 16 additions & 0 deletions tests/Unit/ArrayDefinitionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use TypeError;
use Yiisoft\Definitions\ArrayDefinition;
use Yiisoft\Definitions\Exception\InvalidConfigException;
use Yiisoft\Definitions\Reference;
Expand Down Expand Up @@ -523,4 +524,19 @@ public function testMagicMethods(): void
$object->getEvents()
);
}

public function testNonArrayArguments(): void
{
$definition = ArrayDefinition::fromConfig([
ArrayDefinition::CLASS_NAME => Mouse::class,
'setNameAndEngine()' => 'kitty',
]);
$container = new SimpleContainer();

$this->expectException(TypeError::class);
$this->expectExceptionMessage(
'Yiisoft\Definitions\ArrayDefinition::resolveFunctionArguments(): Argument #3 ($arguments) must be of type array, string given'
);
$definition->resolve($container);
}
}
23 changes: 13 additions & 10 deletions tests/Unit/LazyDefinitionDecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use ProxyManager\Factory\LazyLoadingValueHolderFactory;
use ProxyManager\Proxy\LazyLoadingInterface;
use Yiisoft\Definitions\ArrayDefinition;
use Yiisoft\Definitions\LazyDefinitionDecorator;
use Yiisoft\Definitions\LazyDefinition;
use Yiisoft\Definitions\Tests\Support\EngineInterface;
use Yiisoft\Definitions\Tests\Support\NotFinalClass;
use Yiisoft\Definitions\Tests\Support\Phone;
Expand All @@ -19,31 +19,33 @@ final class LazyDefinitionDecoratorTest extends TestCase
{
public function testDecorateFinalClass(): void
{
$container = new SimpleContainer();
$factory = new LazyLoadingValueHolderFactory();
$container = new SimpleContainer([
LazyLoadingValueHolderFactory::class => new LazyLoadingValueHolderFactory(),
]);

$class = Phone::class;

$definition = ArrayDefinition::fromConfig([
ArrayDefinition::CLASS_NAME => $class,
]);
$definition = new LazyDefinitionDecorator($factory, $definition, $class);
$definition = new LazyDefinition($definition, $class);

$this->expectException(InvalidProxiedClassException::class);
$definition->resolve($container);
}

public function testDecorateNotFinalClass(): void
{
$container = new SimpleContainer();
$factory = new LazyLoadingValueHolderFactory();
$container = new SimpleContainer([
LazyLoadingValueHolderFactory::class => new LazyLoadingValueHolderFactory(),
]);

$class = NotFinalClass::class;

$definition = ArrayDefinition::fromConfig([
ArrayDefinition::CLASS_NAME => $class,
]);
$definition = new LazyDefinitionDecorator($factory, $definition, $class);
$definition = new LazyDefinition($definition, $class);

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

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

public function testDecorateInterface(): void
{
$container = new SimpleContainer();
$factory = new LazyLoadingValueHolderFactory();
$container = new SimpleContainer([
LazyLoadingValueHolderFactory::class => new LazyLoadingValueHolderFactory(),
]);

$class = EngineInterface::class;

$definition = ArrayDefinition::fromConfig([
ArrayDefinition::CLASS_NAME => $class,
]);
$definition = new LazyDefinitionDecorator($factory, $definition, $class);
$definition = new LazyDefinition($definition, $class);

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

Expand Down

0 comments on commit e03e225

Please sign in to comment.