>
> This sounds like you are not using DI meant for fibers/multiple requests
at the same time.
>

Spiral already supports DI containers based on *Scope *(like "per request"
injections). Symfony, if I’m not mistaken, does too.

Spiral introduces a restriction to ensure correct handling of Scope
dependencies: they cannot be mixed with regular dependencies. If a Scope
dependency is needed inside a regular dependency, then (using the same
approach that Larry already showed), it should be done through an
additional interface:

```php
final class UserScope
{
    public function __construct(
        private readonly ContainerInterface $container
    ) {
    }

    public function getUserContext(): ?UserContext
    {
        // error checking is omitted
        return $this->container->get(UserContext::class);
    }

    public function getName(): string
    {
        return $this->getUserContext()->getName();
    }
}
```

At the beginning of a request, Spiral initializes a memory area with Scope
dependencies, so it can be said that it is already prepared to work with
coroutines with minimal modifications.

---

Ed.

Reply via email to