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 | * @param string $entityManagerAlias required |
||
| 20 | * @param Config $config required |
||
| 21 | * @param Metadata\Metadata|null $metadata optional so cached metadata can be loaded |
||
| 22 | */ |
||
| 23 | public function __construct( |
||
| 24 | EntityManager $entityManager, |
||
| 25 | Config|null $config = null, |
||
| 26 | array|null $metadataConfig = null, |
||
| 27 | ) { |
||
| 28 | $this |
||
| 29 | // Plain classes |
||
| 30 | ->set(EntityManager::class, $entityManager) |
||
| 31 | ->set( |
||
| 32 | Config::class, |
||
| 33 | static function () use ($config) { |
||
| 34 | if (! $config) { |
||
| 35 | $config = new Config(); |
||
| 36 | } |
||
| 37 | |||
| 38 | return $config; |
||
| 39 | }, |
||
| 40 | ) |
||
| 41 | ->set( |
||
| 42 | EventDispatcher::class, |
||
| 43 | static fn () => new EventDispatcher() |
||
| 44 | ) |
||
| 45 | ->set( |
||
| 46 | Type\TypeManager::class, |
||
| 47 | static fn () => new Type\TypeManager() |
||
| 48 | ) |
||
| 49 | ->set( |
||
| 50 | Metadata\Metadata::class, |
||
| 51 | static function (ContainerInterface $container) use ($metadataConfig) { |
||
| 52 | return (new Metadata\MetadataFactory($container, $metadataConfig))->getMetadata(); |
||
| 53 | }, |
||
| 54 | ) |
||
| 55 | ->set( |
||
| 56 | Resolve\FieldResolver::class, |
||
| 57 | static function (ContainerInterface $container) { |
||
| 58 | return new Resolve\FieldResolver( |
||
| 59 | $container->get(Config::class), |
||
| 60 | $container->get(Metadata\Metadata::class), |
||
| 61 | ); |
||
| 62 | }, |
||
| 63 | ) |
||
| 64 | ->set( |
||
| 65 | Resolve\ResolveCollectionFactory::class, |
||
| 66 | static function (ContainerInterface $container) { |
||
| 67 | return new Resolve\ResolveCollectionFactory( |
||
| 68 | $container->get(EntityManager::class), |
||
| 69 | $container->get(Config::class), |
||
| 70 | $container->get(Resolve\FieldResolver::class), |
||
| 71 | $container->get(Type\TypeManager::class), |
||
| 72 | ); |
||
| 73 | }, |
||
| 74 | ) |
||
| 75 | ->set( |
||
| 76 | Resolve\ResolveEntityFactory::class, |
||
| 77 | static function (ContainerInterface $container) { |
||
| 78 | return new Resolve\ResolveEntityFactory( |
||
| 79 | $container->get(Config::class), |
||
| 80 | $container->get(EntityManager::class), |
||
| 81 | $container->get(EventDispatcher::class), |
||
| 82 | ); |
||
| 83 | }, |
||
| 84 | ) |
||
| 85 | ->set( |
||
| 86 | Criteria\CriteriaFactory::class, |
||
| 87 | static function (ContainerInterface $container) { |
||
| 88 | return new Criteria\CriteriaFactory( |
||
| 89 | $container->get(Config::class), |
||
| 90 | $container->get(EntityManager::class), |
||
| 91 | $container->get(Type\TypeManager::class), |
||
| 92 | $container->get(EventDispatcher::class), |
||
| 93 | ); |
||
| 94 | }, |
||
| 95 | ) |
||
| 96 | ->set( |
||
| 97 | Hydrator\HydratorFactory::class, |
||
| 98 | static function (ContainerInterface $container) { |
||
| 99 | return new Hydrator\HydratorFactory( |
||
| 100 | $container->get(EntityManager::class), |
||
| 101 | $container->get(Metadata\Metadata::class), |
||
| 102 | ); |
||
| 103 | }, |
||
| 104 | ) |
||
| 105 | ->set( |
||
| 106 | Input\InputFactory::class, |
||
| 107 | static function (ContainerInterface $container) { |
||
| 108 | return new Input\InputFactory( |
||
| 109 | $container->get(Config::class), |
||
| 110 | $container->get(EntityManager::class), |
||
| 111 | $container->get(Type\TypeManager::class), |
||
| 112 | $container->get(Metadata\Metadata::class), |
||
| 113 | ); |
||
| 114 | }, |
||
| 115 | ); |
||
| 116 | } |
||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 117 | |||
| 118 | |||
| 119 | abstract public function set(string $id, mixed $value): mixed; |
||
| 120 | } |
||
| 121 |