API-Skeletons /
doctrine-graphql
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace ApiSkeletons\Doctrine\GraphQL; |
||
| 6 | |||
| 7 | use Doctrine\ORM\EntityManager; |
||
| 8 | use League\Event\EventDispatcher; |
||
| 9 | use Psr\Container\ContainerInterface; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * This trait is used to remove complexity from the Driver class. |
||
| 13 | * It doesn't change what the Driver does. It just separates the container work |
||
| 14 | * from the GraphQL Driver. |
||
| 15 | */ |
||
| 16 | trait Services |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * This is the shared TypeManger for all Drivers |
||
| 20 | */ |
||
| 21 | private static Type\TypeManager|null $typeManagerShared = null; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @param string $entityManagerAlias required |
||
| 25 | * @param Config $config required |
||
| 26 | * @param Metadata\Metadata|null $metadata optional so cached metadata can be loaded |
||
| 27 | */ |
||
| 28 | public function __construct( |
||
| 29 | EntityManager $entityManager, |
||
| 30 | Config|null $config = null, |
||
| 31 | array|null $metadataConfig = null, |
||
| 32 | ) { |
||
| 33 | if (! $config) { |
||
| 34 | $config = new Config(); |
||
| 35 | } |
||
| 36 | |||
| 37 | $this |
||
| 38 | // Plain classes |
||
| 39 | ->set(EntityManager::class, $entityManager) |
||
| 40 | ->set( |
||
| 41 | Config::class, |
||
| 42 | static function () use ($config) { |
||
| 43 | return $config; |
||
| 44 | }, |
||
| 45 | ) |
||
| 46 | ->set( |
||
| 47 | EventDispatcher::class, |
||
| 48 | static fn () => new EventDispatcher() |
||
| 49 | ) |
||
| 50 | ->set( |
||
| 51 | Type\TypeManager::class, |
||
| 52 | static function (ContainerInterface $container) { |
||
| 53 | if (! $container->get(Config::class)->getSharedTypeManager()) { |
||
| 54 | return new Type\TypeManager(); |
||
| 55 | } |
||
| 56 | |||
| 57 | if (! self::$typeManagerShared) { |
||
| 58 | self::$typeManagerShared = new Type\TypeManager(); |
||
| 59 | } |
||
| 60 | |||
| 61 | return self::$typeManagerShared; |
||
| 62 | }, |
||
| 63 | ) |
||
| 64 | ->set( |
||
| 65 | Metadata\Metadata::class, |
||
| 66 | static function (ContainerInterface $container) use ($metadataConfig) { |
||
| 67 | return (new Metadata\MetadataFactory($container, $metadataConfig))->getMetadata(); |
||
| 68 | }, |
||
| 69 | ) |
||
| 70 | ->set( |
||
| 71 | Resolve\FieldResolver::class, |
||
| 72 | static function (ContainerInterface $container) { |
||
| 73 | return new Resolve\FieldResolver( |
||
| 74 | $container->get(Config::class), |
||
| 75 | $container->get(Metadata\Metadata::class), |
||
| 76 | ); |
||
| 77 | }, |
||
| 78 | ) |
||
| 79 | ->set( |
||
| 80 | Resolve\ResolveCollectionFactory::class, |
||
| 81 | static function (ContainerInterface $container) { |
||
| 82 | return new Resolve\ResolveCollectionFactory( |
||
| 83 | $container->get(EntityManager::class), |
||
| 84 | $container->get(Config::class), |
||
| 85 | $container->get(Resolve\FieldResolver::class), |
||
| 86 | $container->get(Type\TypeManager::class), |
||
| 87 | ); |
||
| 88 | }, |
||
| 89 | ) |
||
| 90 | ->set( |
||
| 91 | Resolve\ResolveEntityFactory::class, |
||
| 92 | static function (ContainerInterface $container) { |
||
| 93 | return new Resolve\ResolveEntityFactory( |
||
| 94 | $container->get(Config::class), |
||
| 95 | $container->get(EntityManager::class), |
||
| 96 | $container->get(EventDispatcher::class), |
||
| 97 | ); |
||
| 98 | }, |
||
| 99 | ) |
||
| 100 | ->set( |
||
| 101 | Criteria\CriteriaFactory::class, |
||
| 102 | static function (ContainerInterface $container) { |
||
| 103 | return new Criteria\CriteriaFactory( |
||
| 104 | $container->get(Config::class), |
||
| 105 | $container->get(EntityManager::class), |
||
| 106 | $container->get(Type\TypeManager::class), |
||
| 107 | $container->get(EventDispatcher::class), |
||
| 108 | ); |
||
| 109 | }, |
||
| 110 | ) |
||
| 111 | ->set( |
||
| 112 | Hydrator\HydratorFactory::class, |
||
| 113 | static function (ContainerInterface $container) { |
||
| 114 | return new Hydrator\HydratorFactory( |
||
| 115 | $container->get(EntityManager::class), |
||
| 116 | $container->get(Metadata\Metadata::class), |
||
| 117 | ); |
||
| 118 | }, |
||
| 119 | ) |
||
| 120 | ->set( |
||
| 121 | Input\InputFactory::class, |
||
| 122 | static function (ContainerInterface $container) { |
||
| 123 | return new Input\InputFactory( |
||
| 124 | $container->get(Config::class), |
||
| 125 | $container->get(EntityManager::class), |
||
| 126 | $container->get(Type\TypeManager::class), |
||
| 127 | $container->get(Metadata\Metadata::class), |
||
| 128 | ); |
||
| 129 | }, |
||
| 130 | ); |
||
| 131 | |||
| 132 | if (! $this->get(Config::class)->getGlobalEnable()) { |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 133 | return; |
||
| 134 | } |
||
| 135 | |||
| 136 | $this->set(Type\TypeManager::class, new Type\TypeManager()); |
||
| 137 | } |
||
| 138 | |||
| 139 | abstract public function set(string $id, mixed $value): mixed; |
||
| 140 | } |
||
| 141 |