vendor/overblog/graphql-bundle/src/Resolver/AbstractResolver.php line 77

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Overblog\GraphQLBundle\Resolver;
  4. use function array_keys;
  5. abstract class AbstractResolver implements FluentResolverInterface
  6. {
  7. private array $solutions = [];
  8. private array $aliases = [];
  9. private array $solutionOptions = [];
  10. private array $fullyLoadedSolutions = [];
  11. public function addSolution(string $id, callable $factory, array $aliases = [], array $options = []): self
  12. {
  13. $this->fullyLoadedSolutions[$id] = false;
  14. $this->addAliases($id, $aliases);
  15. $this->solutions[$id] = $factory;
  16. $this->solutionOptions[$id] = $options;
  17. return $this;
  18. }
  19. public function hasSolution(string $id): bool
  20. {
  21. $id = $this->resolveAlias($id);
  22. return isset($this->solutions[$id]);
  23. }
  24. /**
  25. * @return mixed
  26. */
  27. public function getSolution(string $id)
  28. {
  29. return $this->loadSolution($id);
  30. }
  31. public function getSolutions(): array
  32. {
  33. return $this->loadSolutions();
  34. }
  35. public function getSolutionAliases(string $id): array
  36. {
  37. return array_keys($this->aliases, $id);
  38. }
  39. public function getAliases(): array
  40. {
  41. return $this->aliases;
  42. }
  43. /**
  44. * @return mixed
  45. */
  46. public function getSolutionOptions(string $id)
  47. {
  48. $id = $this->resolveAlias($id);
  49. return $this->solutionOptions[$id] ?? [];
  50. }
  51. /**
  52. * @param mixed $solution
  53. */
  54. protected function onLoadSolution($solution): void
  55. {
  56. }
  57. /**
  58. * @return mixed
  59. */
  60. private function loadSolution(string $id)
  61. {
  62. $id = $this->resolveAlias($id);
  63. if (!$this->hasSolution($id)) {
  64. return null;
  65. }
  66. if ($this->fullyLoadedSolutions[$id]) {
  67. return $this->solutions[$id];
  68. } else {
  69. $loader = $this->solutions[$id];
  70. $this->solutions[$id] = $solution = $loader();
  71. $this->onLoadSolution($solution);
  72. $this->fullyLoadedSolutions[$id] = true;
  73. return $solution;
  74. }
  75. }
  76. private function addAliases(string $id, array $aliases): void
  77. {
  78. foreach ($aliases as $alias) {
  79. $this->aliases[$alias] = $id;
  80. }
  81. }
  82. private function resolveAlias(string $alias): string
  83. {
  84. return $this->aliases[$alias] ?? $alias;
  85. }
  86. /**
  87. * @return mixed[]
  88. */
  89. private function loadSolutions(): array
  90. {
  91. foreach ($this->solutions as $name => &$solution) {
  92. $solution = $this->loadSolution($name);
  93. }
  94. return $this->solutions;
  95. }
  96. }