PageProvider.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace Webkul\BagistoApi\State;
  3. use ApiPlatform\Metadata\Get;
  4. use ApiPlatform\Metadata\GetCollection;
  5. use ApiPlatform\Metadata\Operation;
  6. use ApiPlatform\State\ProviderInterface;
  7. use Webkul\BagistoApi\Models\Page;
  8. use Webkul\CMS\Repositories\PageRepository;
  9. /**
  10. * Provider for fetching CMS pages
  11. * Returns arrays for REST API and Eloquent models for GraphQL
  12. */
  13. class PageProvider implements ProviderInterface
  14. {
  15. public function __construct(
  16. private readonly PageRepository $pageRepository
  17. ) {}
  18. public function provide(
  19. Operation $operation,
  20. array $uriVariables = [],
  21. array $context = []
  22. ): object|array|null {
  23. // Check if it's a GraphQL operation (class name contains "GraphQl")
  24. $isGraphQL = str_contains($operation::class, 'GraphQl');
  25. // For GraphQL operations, return Eloquent models
  26. if ($isGraphQL) {
  27. return $this->provideForGraphQL($operation, $uriVariables, $context);
  28. }
  29. // For REST operations, return formatted arrays
  30. return $this->provideForRest($operation, $uriVariables, $context);
  31. }
  32. /**
  33. * Provide data for GraphQL - return Eloquent models
  34. */
  35. private function provideForGraphQL(Operation $operation, array $uriVariables, array $context): mixed
  36. {
  37. $name = $operation->getName();
  38. // Handle pageByUrlKey - this goes through the resolver
  39. if ($name === 'pageByUrlKey') {
  40. return null; // Let the resolver handle it
  41. }
  42. // Check if it's an item query
  43. $isItem = $operation instanceof Get;
  44. if ($isItem) {
  45. if (isset($uriVariables['id'])) {
  46. return Page::with('translations')->find($uriVariables['id']);
  47. }
  48. return null;
  49. }
  50. // Collection query
  51. return Page::with('translations')
  52. ->orderBy('created_at', 'desc')
  53. ->get();
  54. }
  55. /**
  56. * Provide data for REST API - return formatted arrays
  57. */
  58. private function provideForRest(Operation $operation, array $uriVariables, array $context)
  59. {
  60. if ($operation instanceof Get) {
  61. return $this->provideItem($uriVariables, $context);
  62. }
  63. if ($operation instanceof GetCollection) {
  64. return $this->provideCollection($context);
  65. }
  66. return null;
  67. }
  68. /**
  69. * Provide single page item
  70. */
  71. private function provideItem(array $uriVariables, array $context): ?array
  72. {
  73. if (isset($uriVariables['id'])) {
  74. $id = $uriVariables['id'];
  75. $page = Page::with('translations')->find($id);
  76. if (! $page) {
  77. return null;
  78. }
  79. return $this->formatPage($page);
  80. }
  81. return null;
  82. }
  83. /**
  84. * Provide collection of pages
  85. */
  86. private function provideCollection(array $context): iterable
  87. {
  88. $pages = Page::with('translations')
  89. ->orderBy('created_at', 'desc')
  90. ->get();
  91. return $pages->map(function ($page) {
  92. return $this->formatPage($page);
  93. });
  94. }
  95. /**
  96. * Format page for REST API response
  97. */
  98. private function formatPage($page): array
  99. {
  100. $translation = $page->translations->firstWhere('locale', app()->getLocale())
  101. ?? $page->translations->first();
  102. return [
  103. 'id' => '/api/shop/pages/'.$page->id,
  104. '_id' => $page->id,
  105. 'layout' => $page->layout,
  106. 'createdAt' => $page->created_at?->toIso8601String(),
  107. 'updatedAt' => $page->updated_at?->toIso8601String(),
  108. 'translation' => $translation ? $this->formatTranslation($translation) : null,
  109. ];
  110. }
  111. /**
  112. * Format translation for REST API response
  113. */
  114. private function formatTranslation($translation): array
  115. {
  116. return [
  117. 'id' => '/api/shop/page_translations/'.$translation->id,
  118. '_id' => $translation->id,
  119. 'pageTitle' => $translation->page_title,
  120. 'urlKey' => $translation->url_key,
  121. 'htmlContent' => $translation->html_content,
  122. 'metaTitle' => $translation->meta_title,
  123. 'metaDescription' => $translation->meta_description,
  124. 'metaKeywords' => $translation->meta_keywords,
  125. 'locale' => $translation->locale,
  126. 'cmsPageId' => (string) $translation->cms_page_id,
  127. ];
  128. }
  129. }