Skip to content
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

Lazy services #232

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
a0fdad8
Decorate definition when it contains lazy
xepozz Aug 1, 2021
61f2bff
Add tests
xepozz Aug 1, 2021
1b4cfae
Normalize definition out of the building process
xepozz Aug 1, 2021
abb2e83
Rework test cases
xepozz Aug 1, 2021
15f8a66
Add class resolver
xepozz Aug 1, 2021
dbed5b3
Apply fixes from StyleCI
samdark Aug 1, 2021
4bdd4bc
Merge branch 'master' into lazy-services
xepozz Aug 7, 2021
592c239
Fix tests
xepozz Aug 7, 2021
e4bd46f
Apply fixes from StyleCI
samdark Aug 7, 2021
2f8c8b5
Merge branch 'master' into lazy-services
xepozz Aug 10, 2021
8212a39
Merge branch 'master' into lazy-services
xepozz Nov 20, 2022
5136d8d
[ci-review] Apply changes from Rector action.
Nov 20, 2022
47dc088
Fix resolving lazy service
xepozz Nov 20, 2022
40ad592
Merge remote-tracking branch 'origin/lazy-services' into lazy-services
xepozz Nov 20, 2022
125231d
Merge branch 'master' into lazy-services
xepozz Dec 2, 2022
51789d0
Merge branch 'master' into lazy-services
xepozz Dec 3, 2022
1d59fbd
Use `friendsofphp/proxy-manager-lts` package
xepozz Dec 3, 2022
95cdd9e
Fix psalm
xepozz Dec 3, 2022
0b0b784
Rework lazy definition
xepozz Dec 3, 2022
20db77b
Merge branch 'master' into lazy-services
xepozz Jul 29, 2023
d1b0c55
Apply Rector changes (CI)
xepozz Jul 29, 2023
feff3b7
Apply fixes from StyleCI
StyleCIBot Jul 29, 2023
5bf1e81
Fix annotation
xepozz Aug 1, 2023
1d04fbc
Add new test case
xepozz Aug 1, 2023
bc7f70e
Fix psalm
xepozz Aug 1, 2023
fedff5e
Merge branch 'master' into lazy-services
xepozz Mar 3, 2024
51fefa2
Suggestion for lazy service (#355)
vjik Apr 16, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix resolving lazy service
  • Loading branch information
xepozz committed Nov 20, 2022
commit 47dc08893c8f2544f5695401c19c9d5f288a4743
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
"php": "^8.0",
"ext-mbstring": "*",
"psr/container": "^1.1|^2.0",
"yiisoft/definitions": "^3.0"
"yiisoft/definitions": "dev-lazy-definition"
},
"require-dev": {
"league/container": "^4.2",
"maglnet/composer-require-checker": "^4.2",
"ocramius/proxy-manager": "^2.13",
"ocramius/proxy-manager": "^2.14.1",
"phpbench/phpbench": "^1.2.0",
"phpunit/phpunit": "^9.5",
"rector/rector": "^0.14.3",
Expand Down
31 changes: 14 additions & 17 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
use Closure;
use ProxyManager\Factory\LazyLoadingValueHolderFactory;
use Psr\Container\ContainerInterface;
use RuntimeException;
use Yiisoft\Definitions\ArrayDefinition;
use Yiisoft\Definitions\Contract\DefinitionInterface;
use Yiisoft\Definitions\Exception\CircularReferenceException;
use Yiisoft\Definitions\Exception\InvalidConfigException;
use Yiisoft\Definitions\Exception\NotInstantiableException;
use Yiisoft\Definitions\Helpers\DefinitionValidator;
use Yiisoft\Definitions\DefinitionStorage;
use Yiisoft\Definitions\LazyDefinitionDecorator;
use Yiisoft\Di\Helpers\DefinitionNormalizer;
use Yiisoft\Di\Helpers\DefinitionParser;
use Yiisoft\Di\Helpers\TagHelper;
Expand Down Expand Up @@ -614,27 +617,21 @@ private function buildProvider(mixed $provider): ServiceProviderInterface
return $providerInstance;
}

/**
* @param mixed $variable
*/
private function getVariableType($variable): string
{
if (is_object($variable)) {
return get_class($variable);
}

return gettype($variable);
}

private function decorateLazy(string $id, DefinitionInterface $definition): DefinitionInterface
private function decorateLazy(string $id, $definition): DefinitionInterface
{
$factory = $this->getLazyLoadingValueHolderFactory();
if (class_exists($id) || interface_exists($id)) {
$class = $id;
} elseif ($definition instanceof ArrayDefinition) {
$class = $definition->getClass();
} elseif (is_array($definition)) {
$class = $definition['class'];
} else {
throw new \RuntimeException('Could not determinate object class');
throw new RuntimeException(
sprintf(
'Could not handle definition type "%s" with definition class "%s"',
get_debug_type($definition),
$id,
)
);
}

return new LazyDefinitionDecorator($factory, $definition, $class);
Expand All @@ -643,7 +640,7 @@ private function decorateLazy(string $id, DefinitionInterface $definition): Defi
private function getLazyLoadingValueHolderFactory(): LazyLoadingValueHolderFactory
{
if (!class_exists(LazyLoadingValueHolderFactory::class)) {
throw new \RuntimeException('You should install `ocramius/proxy-manager` if you want to use lazy services.');
throw new RuntimeException('You should install `ocramius/proxy-manager` if you want to use lazy services.');
}
return $this->lazyFactory ??= new LazyLoadingValueHolderFactory();
xepozz marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down
28 changes: 17 additions & 11 deletions tests/Unit/LazyServiceContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
namespace Yiisoft\Di\Tests\Unit;

use PHPUnit\Framework\TestCase;
use ProxyManager\Factory\LazyLoadingValueHolderFactory;
use ProxyManager\Proxy\LazyLoadingInterface;
use Yiisoft\Di\Container;
use Yiisoft\Di\ContainerConfig;
use Yiisoft\Di\Tests\Support\EngineMarkOne;

class LazyServiceContainerTest extends TestCase
{
protected function setUp(): void
{
if (!class_exists(\ProxyManager\Factory\LazyLoadingValueHolderFactory::class)) {
if (!class_exists(LazyLoadingValueHolderFactory::class)) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's always false in tests

$this->markTestSkipped('You should install `ocramius/proxy-manager` if you want to use lazy services.');
}
}
Expand All @@ -23,23 +25,25 @@ public function testIsTheSameObject(): void
$class = EngineMarkOne::class;
$number = 55;

$container = new Container([
EngineMarkOne::class => [
'class' => $class,
'setNumber()' => [$number],
'lazy' => true,
],
]);
$config = ContainerConfig::create()
->withDefinitions([
EngineMarkOne::class => [
'class' => $class,
'setNumber()' => [$number],
'lazy' => true,
],
]);
$container = new Container($config);

/* @var \Yiisoft\Di\Tests\Support\EngineMarkOne $object */
/* @var EngineMarkOne $object */
$object = $container->get($class);

self::assertInstanceOf(LazyLoadingInterface::class, $object);
self::assertFalse($object->isProxyInitialized());
self::assertEquals($number, $object->getNumber());
self::assertTrue($object->isProxyInitialized());

/* @var \Yiisoft\Di\Tests\Support\EngineMarkOne $object */
/* @var EngineMarkOne $object */
$object = $container->get($class);

self::assertInstanceOf(LazyLoadingInterface::class, $object);
Expand All @@ -51,7 +55,9 @@ public function testIsTheSameObject(): void
*/
public function testLazy(array $definitions, string $id): void
{
$container = new Container($definitions);
$config = ContainerConfig::create()
->withDefinitions($definitions);
$container = new Container($config);

$object = $container->get($id);

Expand Down