Skip to content

Commit

Permalink
The #[Service] attribute at class will make prepareSharedObject insta…
Browse files Browse the repository at this point in the history
…nt create instance #1141
  • Loading branch information
asika32764 committed Dec 11, 2024
1 parent fc788e8 commit 78fc73d
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions packages/di/src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public static function getDefaultAttributes(): array
*/
public function set(string $id, mixed $value, int $options = 0): StoreDefinitionInterface
{
$definition = $this->getDefinition($id);
$definition = $this->findDefinition($id, false);

if ($definition && $definition->isProtected()) {
throw new DefinitionException(
Expand Down Expand Up @@ -380,7 +380,7 @@ public function has(string $id): bool

public function hasCached(string $id): bool
{
return $this->getDefinition($id)?->getCache() !== null;
return $this->findDefinition($id, false)?->getCache() !== null;
}

/**
Expand Down Expand Up @@ -416,7 +416,7 @@ public function remove(string $id): static
*/
public function fork(string $id, string $newId, bool $forceNew = false): mixed
{
$raw = clone $this->getDefinition($id);
$raw = clone $this->findDefinition($id, true);

$this->storage[$newId] = $raw;

Expand All @@ -433,6 +433,11 @@ public function fork(string $id, string $newId, bool $forceNew = false): mixed
* @since 2.0
*/
public function getDefinition(string $id): ?StoreDefinitionInterface
{
return $this->findDefinition($id, true);
}

protected function findDefinition(string $id, bool $serviceAutowire = false): ?StoreDefinitionInterface
{
$id = $this->resolveAlias($id);

Expand All @@ -441,7 +446,7 @@ public function getDefinition(string $id): ?StoreDefinitionInterface
}

// Get instant service
if (class_exists($id) && $service = static::getServiceAttribute(new ReflectionClass($id))) {
if ($serviceAutowire && class_exists($id) && $service = static::getServiceAttribute(new ReflectionClass($id))) {
$definition = new StoreDefinition($id, $this->newInstance($id));
$definition->providedIn($service->providedIn);

Expand All @@ -451,7 +456,7 @@ public function getDefinition(string $id): ?StoreDefinitionInterface
}

if ($this->parent instanceof static) {
$parentDefinition = $this->parent->getDefinition($id);
$parentDefinition = $this->parent->findDefinition($id, $serviceAutowire);

// Store parent definition as self
if ($parentDefinition) {
Expand Down Expand Up @@ -481,7 +486,7 @@ public function clear(): void
public function clearCache(?string $id = null): void
{
if ($id !== null) {
$this->getDefinition($id)?->reset();
$this->findDefinition($id, false)?->reset();

return;
}
Expand Down

0 comments on commit 78fc73d

Please sign in to comment.