Repository navigation
feat(container): add singleton lifetime support #2292
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
Draft
brendt
wants to merge
22
commits into
3.x
Choose a base branch
from
feat-container-lifetime
base: 3.x
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.
Draft
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
0439fa4
wip
brendt bd71fef
wip
brendt 1056a47
wip
brendt d0da26a
wip
brendt d8f3132
wip
brendt 94917bc
wip
brendt e634d40
wip
brendt cbeea06
wip
brendt 12aa71f
wip
brendt 1c8b57c
wip
brendt f8ef1da
wip
brendt 0cf89ab
wip
brendt 8ae088e
Merge remote-tracking branch 'origin/feat-container-lifetime' into fe…
brendt 6e88e3b
wip
brendt 9b60678
wip
brendt ac7a422
wip
brendt e3f47f3
Merge origin/3.x into feat-container-lifetime
brendt 3229bef
test(container): move discovery-based lifetime tests to integration s…
brendt 433501e
fix ci
brendt bac4af8
fix ci
brendt 57a81d3
fix ci
brendt a7143ce
fix ci
brendt 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
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 | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -21,6 +21,7 @@ | |||||||
| use Tempest\Reflection\TypeReflector; | ||||||||
| use Throwable; | ||||||||
| use UnitEnum; | ||||||||
| use WeakMap; | ||||||||
|
|
||||||||
| use const ARRAY_FILTER_USE_BOTH; | ||||||||
|
|
||||||||
|
|
@@ -31,6 +32,12 @@ final class GenericContainer implements Container | |||||||
| /** @var array<class-string<DynamicInitializer>, DynamicInitializer> */ | ||||||||
| private array $resolvedDynamicInitializers = []; | ||||||||
|
|
||||||||
| /** @var WeakMap<object, Lifetime> */ | ||||||||
| private WeakMap $singletonLifetimes; | ||||||||
|
|
||||||||
| /** @var array<string, Lifetime> */ | ||||||||
| private array $registeredSingletonLifetimes = []; | ||||||||
|
|
||||||||
| public function __construct( | ||||||||
| /** @var ArrayIterator<array-key, mixed> $definitions */ | ||||||||
| private(set) ArrayIterator $definitions = new ArrayIterator(), | ||||||||
|
|
@@ -55,6 +62,7 @@ public function __construct( | |||||||
|
|
||||||||
| private(set) ?DependencyChain $chain = null, | ||||||||
| ) { | ||||||||
| $this->singletonLifetimes = new WeakMap(); | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| $this->singleton(Container::class, $this); | ||||||||
| $this->singleton(ContainerInterface::class, $this); | ||||||||
| $this->singleton(GenericContainer::class, $this); | ||||||||
|
|
@@ -146,6 +154,7 @@ public function unregister(string $className, bool $tagged = false): self | |||||||
| unset($this->definitions[$className]); | ||||||||
| unset($this->singletonDefinitions[$className]); | ||||||||
| unset($this->resolvedSingletons[$className]); | ||||||||
| unset($this->registeredSingletonLifetimes[$className]); | ||||||||
|
|
||||||||
| if ($tagged) { | ||||||||
| foreach ($this->singletonDefinitions as $key => $definition) { | ||||||||
|
|
@@ -154,6 +163,7 @@ public function unregister(string $className, bool $tagged = false): self | |||||||
| } | ||||||||
|
|
||||||||
| unset($this->singletonDefinitions[$key]); | ||||||||
| unset($this->registeredSingletonLifetimes[$key]); | ||||||||
| } | ||||||||
|
|
||||||||
| foreach ($this->resolvedSingletons as $key => $definition) { | ||||||||
|
|
@@ -173,14 +183,24 @@ public function has(string $className, string|UnitEnum|null $tag = null): bool | |||||||
| return isset($this->definitions[$className]) || isset($this->singletonDefinitions[$this->resolveTaggedName($className, $tag)]); | ||||||||
| } | ||||||||
|
|
||||||||
| public function singleton(string $className, mixed $definition, string|UnitEnum|null $tag = null): self | ||||||||
| public function singleton(string $className, mixed $definition, string|UnitEnum|null $tag = null, ?Lifetime $lifetime = null): self | ||||||||
| { | ||||||||
| if ($definition instanceof HasTag) { | ||||||||
| $tag = $definition->tag; | ||||||||
| } | ||||||||
|
|
||||||||
| $dependencyName = $this->resolveTaggedName($className, $tag); | ||||||||
|
|
||||||||
| if ($lifetime instanceof Lifetime) { | ||||||||
| $this->registeredSingletonLifetimes[$dependencyName] = $lifetime; | ||||||||
|
|
||||||||
| if (is_object($definition) && ! $definition instanceof Closure) { | ||||||||
| $this->singletonLifetimes[$definition] = $lifetime; | ||||||||
| } | ||||||||
| } else { | ||||||||
| unset($this->registeredSingletonLifetimes[$dependencyName]); | ||||||||
| } | ||||||||
|
|
||||||||
| $this->singletonDefinitions[$dependencyName] = $definition; | ||||||||
| unset($this->resolvedSingletons[$dependencyName]); | ||||||||
|
|
||||||||
|
|
@@ -393,12 +413,21 @@ private function resolveDependency(string $className, string|UnitEnum|null $tag | |||||||
|
|
||||||||
| // Check if the class has been registered as a singleton. | ||||||||
| if ($singletonDefinition = $this->singletonDefinitions[$dependencyName] ?? null) { | ||||||||
| $this->resolveChain()->add($class); | ||||||||
| // Only closures need to be tracked: an already constructed instance cannot take part in a cycle. | ||||||||
| if ($singletonDefinition instanceof Closure) { | ||||||||
| $this->resolveChain()->add($class); | ||||||||
|
|
||||||||
| try { | ||||||||
| $instance = $singletonDefinition($this); | ||||||||
| } finally { | ||||||||
| $this->resolveChain()->pop(); | ||||||||
| } | ||||||||
| } else { | ||||||||
| $instance = $singletonDefinition; | ||||||||
| } | ||||||||
|
|
||||||||
| try { | ||||||||
| $instance = $singletonDefinition instanceof Closure ? $singletonDefinition($this) : $singletonDefinition; | ||||||||
| } finally { | ||||||||
| $this->resolveChain()->pop(); | ||||||||
| if (isset($this->registeredSingletonLifetimes[$dependencyName])) { | ||||||||
| $this->singletonLifetimes[$instance] = $this->registeredSingletonLifetimes[$dependencyName]; | ||||||||
| } | ||||||||
|
|
||||||||
| $this->resolvedSingletons[$dependencyName] = $instance; | ||||||||
|
|
@@ -432,9 +461,12 @@ private function resolveDependency(string $className, string|UnitEnum|null $tag | |||||||
| $this->resolveChain()->pop(); | ||||||||
| } | ||||||||
|
|
||||||||
| $singleton = $initializerClass->getAttribute(Singleton::class) ?? $initializerClass->getMethod('initialize')->getAttribute(Singleton::class); | ||||||||
| $singleton = $initializerClass->getAttribute(Singleton::class) ?? $initializerClass | ||||||||
| ->getMethod('initialize') | ||||||||
| ->getAttribute(Singleton::class) ?? $class->getAttribute(Singleton::class); | ||||||||
|
|
||||||||
| if ($singleton !== null) { | ||||||||
| $this->singletonLifetimes[$object] = $singleton->lifetime; | ||||||||
| $this->singleton($className, $object, $tag); | ||||||||
| } | ||||||||
|
|
||||||||
|
|
@@ -531,8 +563,9 @@ private function autowire(string $className, string|UnitEnum|null $tag, mixed .. | |||||||
| if ( | ||||||||
| ! $classReflector->getType()->matches(Initializer::class) | ||||||||
| && ! $classReflector->getType()->matches(DynamicInitializer::class) | ||||||||
| && $classReflector->hasAttribute(Singleton::class) | ||||||||
| && ($singleton = $classReflector->getAttribute(Singleton::class)) !== null | ||||||||
| ) { | ||||||||
| $this->singletonLifetimes[$instance] = $singleton->lifetime; | ||||||||
| $this->singleton($className, $instance, $tag); | ||||||||
| } | ||||||||
|
|
||||||||
|
|
@@ -776,7 +809,6 @@ public function addResettable(string|ClassReflector $resettableClass): Container | |||||||
|
|
||||||||
| public function reset(): self | ||||||||
| { | ||||||||
| $this->resolvedSingletons = new ArrayIterator(); | ||||||||
| $this->resolvedDynamicInitializers = []; | ||||||||
|
|
||||||||
| foreach ($this->resettables as $resettableClass) { | ||||||||
|
|
@@ -786,6 +818,25 @@ public function reset(): self | |||||||
| $resettable->reset(); | ||||||||
| } | ||||||||
|
|
||||||||
| foreach ([$this->singletonDefinitions, $this->resolvedSingletons] as $singletons) { | ||||||||
| foreach ($singletons->getArrayCopy() as $dependencyName => $instance) { | ||||||||
| // Factories remain registered so the next request can create a fresh instance. | ||||||||
| if (! is_object($instance)) { | ||||||||
| continue; | ||||||||
| } | ||||||||
|
|
||||||||
| if ($instance instanceof Closure) { | ||||||||
| continue; | ||||||||
| } | ||||||||
|
|
||||||||
| $this->singletonLifetimes[$instance] ??= new ClassReflector($instance)->getAttribute(Singleton::class)->lifetime ?? Lifetime::PROCESS; | ||||||||
|
|
||||||||
| if ($this->singletonLifetimes[$instance] === Lifetime::REQUEST) { | ||||||||
| unset($singletons[$dependencyName]); | ||||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| return $this; | ||||||||
| } | ||||||||
|
|
||||||||
|
|
||||||||
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,14 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tempest\Container; | ||
|
|
||
| enum Lifetime | ||
| { | ||
| /** Keep a singleton alive as long as the process is running */ | ||
| case PROCESS; | ||
|
|
||
| /** Keep a singleton alive within the lifetime of a request */ | ||
| case REQUEST; | ||
| } |
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.