Created
July 3, 2020 14:15
-
-
Save pauci/b34dde239659390646bd01303fa25547 to your computer and use it in GitHub Desktop.
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
| <?php | |
| declare(strict_types=1); | |
| use Doctrine\ORM\EntityManagerInterface; | |
| use ProxyManager\Factory\LazyLoadingValueHolderFactory; | |
| use ProxyManager\Proxy\VirtualProxyInterface; | |
| use Psr\Container\ContainerInterface; | |
| use Swoole\ArrayObject; | |
| use Swoole\Coroutine; | |
| final class CoroutineEntityManagerDelegator | |
| { | |
| public function __invoke(ContainerInterface $container, string $name, callable $callback): VirtualProxyInterface | |
| { | |
| $factory = new LazyLoadingValueHolderFactory(); | |
| return $factory->createProxy( | |
| EntityManagerInterface::class, | |
| static function (?EntityManagerInterface &$wrappedObject) use ($name, $callback): bool { | |
| static $globalContext; | |
| $context = Coroutine::getContext() ?? ($globalContext ??= new ArrayObject()); | |
| // Return existing entity manager for current coroutine or create one | |
| $wrappedObject = $context[$name] ??= $callback(); | |
| return true; | |
| } | |
| ); | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Radiergummi does it work for you with enabled coroutines
Swoole\Coroutine::enableCoroutine()? I do not know how Symfony DI works, but it seems to me that your entity-manager proxy is using same entity-manager instance that was initially passed to__invoke(), for each coroutine. In my example, the$callbackis actual entity-manager factory and is called directly from proxy for every coroutine, so every coroutine gets separate entity-manager instance.