generated from yiisoft/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 5
Lazy definition #58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
xepozz
wants to merge
37
commits into
master
Choose a base branch
from
lazy-definition
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Lazy definition #58
Changes from 18 commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
8679688
Add lazy definition
xepozz a783c5d
Apply fixes from StyleCI
StyleCIBot 5ccf19c
Add missed files
xepozz 908b4d6
Merge remote-tracking branch 'origin/lazy-definition' into lazy-defin…
xepozz 5d6b89f
Fix decorator
xepozz f5225c2
[ci-review] Apply changes from Rector action.
8dbcf88
Add suggest section
xepozz 9bd5fb3
Merge remote-tracking branch 'origin/lazy-definition' into lazy-defin…
xepozz 690a335
Merge branch 'master' into lazy-definition
xepozz f034ae0
Use `friendsofphp/proxy-manager-lts` package
xepozz e03e225
Rework lazy definition
xepozz 6a8f40e
Apply fixes from StyleCI
StyleCIBot f7aba27
Merge branch 'master' into lazy-definition
xepozz de1c7e8
Apply Rector changes (CI)
xepozz bd65bf9
Add php 8.2 in CI
xepozz fba80b0
Add php 8.2 in CI
xepozz a4e6abc
Merge remote-tracking branch 'origin/lazy-definition' into lazy-defin…
xepozz 4ced43c
Fix tests and psalm errors
xepozz 2ca1b5e
Apply suggestions from code review
xepozz e1d4093
Apply PR suggestions
xepozz baee300
Add composer-require-checker.json
xepozz 3f99488
Fix null class property
xepozz bc09d53
Apply fixes from StyleCI
StyleCIBot cbc4373
Add a hack for already normalized configs
xepozz fd066f1
Merge branch 'lazy-definition' of github.com:yiisoft/definitions into…
xepozz 6060238
Apply fixes from StyleCI
StyleCIBot 6d4696e
Fix psalm
xepozz bb401d9
Merge branch 'lazy-definition' of github.com:yiisoft/definitions into…
xepozz 41eef65
Remove PHP 8.2 from psalm CI
xepozz 7993b9b
Merge branch 'master' into lazy-definition
xepozz 29f4c54
Fix psalm error
xepozz 4128323
Merge branch 'master' into lazy-definition
xepozz e6c3e9b
Merge branch 'master' into lazy-definition
xepozz 23e3ff7
improve
vjik 69be227
Merge pull request #88 from yiisoft/lazy-suggestion
xepozz 5434734
Merge branch 'master' into lazy-definition
xepozz cf7935f
Apply fixes from StyleCI
StyleCIBot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,4 +28,4 @@ jobs: | |
os: >- | ||
['ubuntu-latest', 'windows-latest'] | ||
php: >- | ||
['8.0', '8.1'] | ||
['8.0', '8.1', '8.2'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,4 +28,4 @@ jobs: | |
os: >- | ||
['ubuntu-latest'] | ||
php: >- | ||
['8.0', '8.1'] | ||
['8.0', '8.1', '8.2'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Definitions; | ||
|
||
use ProxyManager\Factory\LazyLoadingValueHolderFactory; | ||
use Psr\Container\ContainerInterface; | ||
use Yiisoft\Definitions\Contract\DefinitionInterface; | ||
use Yiisoft\Definitions\Helpers\Normalizer; | ||
|
||
final class LazyDefinition implements DefinitionInterface | ||
{ | ||
public function __construct( | ||
private mixed $definition, | ||
/** | ||
* @var class-string | ||
xepozz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
private string $objectClass, | ||
xepozz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) { | ||
} | ||
|
||
/** | ||
* @psalm-suppress MixedArgumentTypeCoercion | ||
*/ | ||
public function resolve(ContainerInterface $container): mixed | ||
xepozz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
/** @var LazyLoadingValueHolderFactory $factory */ | ||
$factory = $container->get(LazyLoadingValueHolderFactory::class); | ||
/** | ||
* @var mixed $definition | ||
*/ | ||
$definition = $this->definition; | ||
$objectClass = $this->objectClass; | ||
vjik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** @psalm-suppress InvalidArgument */ | ||
return $factory->createProxy( | ||
$objectClass, | ||
function (mixed &$wrappedObject) use ($container, $objectClass, $definition) { | ||
xepozz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$definition = Normalizer::normalize($definition, $objectClass); | ||
/** | ||
* @var mixed $wrappedObject | ||
*/ | ||
$wrappedObject = $definition->resolve($container); | ||
} | ||
); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Definitions\Tests\Support; | ||
|
||
class NotFinalClass | ||
{ | ||
private array $arguments; | ||
|
||
public function __construct(...$arguments) | ||
{ | ||
$this->arguments = $arguments; | ||
} | ||
|
||
public function getArguments(): array | ||
{ | ||
return $this->arguments; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Definitions\Tests\Unit; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use ProxyManager\Exception\InvalidProxiedClassException; | ||
use ProxyManager\Factory\LazyLoadingValueHolderFactory; | ||
use ProxyManager\Proxy\LazyLoadingInterface; | ||
use Yiisoft\Definitions\ArrayDefinition; | ||
use Yiisoft\Definitions\LazyDefinition; | ||
use Yiisoft\Definitions\Tests\Support\EngineInterface; | ||
use Yiisoft\Definitions\Tests\Support\NotFinalClass; | ||
use Yiisoft\Definitions\Tests\Support\Phone; | ||
use Yiisoft\Test\Support\Container\SimpleContainer; | ||
|
||
final class LazyDefinitionDecoratorTest extends TestCase | ||
xepozz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
public function testDecorateFinalClass(): void | ||
{ | ||
$container = new SimpleContainer([ | ||
LazyLoadingValueHolderFactory::class => new LazyLoadingValueHolderFactory(), | ||
]); | ||
|
||
$class = Phone::class; | ||
|
||
$definition = ArrayDefinition::fromConfig([ | ||
ArrayDefinition::CLASS_NAME => $class, | ||
]); | ||
$definition = new LazyDefinition($definition, $class); | ||
xepozz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
$this->expectException(InvalidProxiedClassException::class); | ||
$definition->resolve($container); | ||
} | ||
|
||
public function testDecorateNotFinalClass(): void | ||
{ | ||
$container = new SimpleContainer([ | ||
LazyLoadingValueHolderFactory::class => new LazyLoadingValueHolderFactory(), | ||
]); | ||
|
||
$class = NotFinalClass::class; | ||
|
||
$definition = ArrayDefinition::fromConfig([ | ||
ArrayDefinition::CLASS_NAME => $class, | ||
]); | ||
$definition = new LazyDefinition($definition, $class); | ||
xepozz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
$phone = $definition->resolve($container); | ||
xepozz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
self::assertInstanceOf(LazyLoadingInterface::class, $phone); | ||
} | ||
|
||
public function testDecorateInterface(): void | ||
{ | ||
$container = new SimpleContainer([ | ||
LazyLoadingValueHolderFactory::class => new LazyLoadingValueHolderFactory(), | ||
]); | ||
|
||
$class = EngineInterface::class; | ||
|
||
$definition = ArrayDefinition::fromConfig([ | ||
ArrayDefinition::CLASS_NAME => $class, | ||
]); | ||
$definition = new LazyDefinition($definition, $class); | ||
xepozz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
$phone = $definition->resolve($container); | ||
xepozz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
self::assertInstanceOf(LazyLoadingInterface::class, $phone); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.