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 psalm
  • Loading branch information
xepozz committed Dec 3, 2022
commit 95cdd9ea5a8b7da8d69c1a444feb64e22ef47222
22 changes: 14 additions & 8 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ final class Container implements ContainerInterface
*/
private array $resetters = [];
private bool $useResettersFromMeta = true;
private LazyLoadingValueHolderFactory $lazyFactory;
private ?LazyLoadingValueHolderFactory $lazyFactory = null;

/**
* @param ContainerConfigInterface $config Container configuration.
Expand Down Expand Up @@ -633,17 +633,17 @@ private function buildProvider(mixed $provider): ServiceProviderInterface
return $providerInstance;
}

private function decorateLazy(string $id, $definition): DefinitionInterface
private function decorateLazy(string $id, mixed $definition): DefinitionInterface
{
$factory = $this->getLazyLoadingValueHolderFactory();
if (class_exists($id) || interface_exists($id)) {
$class = $id;
} elseif (is_array($definition)) {
$class = $definition['class'];
} elseif (is_array($definition) && array_key_exists(ArrayDefinition::CLASS_NAME, $definition)) {
$class = (string) $definition[ArrayDefinition::CLASS_NAME];
} else {
throw new RuntimeException(
throw new InvalidConfigException(
sprintf(
'Could not handle definition type "%s" with definition class "%s"',
'Invalid definition: lazy services are only available with array definitions or references. Got type "%s" for definition ID: "%s"',
get_debug_type($definition),
$id,
)
Expand All @@ -656,8 +656,14 @@ private function decorateLazy(string $id, $definition): DefinitionInterface
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 `friendsofphp/proxy-manager-lts` if you want to use lazy services.'
);
}
if ($this->lazyFactory === null) {
$this->lazyFactory = new LazyLoadingValueHolderFactory();
}
return $this->lazyFactory ??= new LazyLoadingValueHolderFactory();

return $this->lazyFactory;
}
}
4 changes: 3 additions & 1 deletion tests/Unit/LazyServiceContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class LazyServiceContainerTest extends TestCase
protected function setUp(): void
{
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.');
$this->markTestSkipped('You should install `friendsofphp/proxy-manager-lts` if you want to use lazy services.');
}
}

Expand All @@ -39,6 +39,7 @@ public function testIsTheSameObject(): void
$object = $container->get($class);

self::assertInstanceOf(LazyLoadingInterface::class, $object);
self::assertInstanceOf(EngineMarkOne::class, $object);
self::assertFalse($object->isProxyInitialized());
self::assertEquals($number, $object->getNumber());
self::assertTrue($object->isProxyInitialized());
Expand All @@ -47,6 +48,7 @@ public function testIsTheSameObject(): void
$object = $container->get($class);

self::assertInstanceOf(LazyLoadingInterface::class, $object);
self::assertInstanceOf(EngineMarkOne::class, $object);
self::assertTrue($object->isProxyInitialized());
}

Expand Down