Camel.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace Knuckles\Camel;
  3. use Illuminate\Support\Arr;
  4. use Illuminate\Support\Collection;
  5. use Illuminate\Support\Str;
  6. use Knuckles\Camel\Extraction\ExtractedEndpointData;
  7. use Knuckles\Camel\Output\OutputEndpointData;
  8. use Knuckles\Scribe\Tools\Utils;
  9. use Symfony\Component\Yaml\Yaml;
  10. class Camel
  11. {
  12. /**
  13. * Mapping of group names to their generated file names. Helps us respect user reordering.
  14. * @var array<string, string>
  15. */
  16. public static array $groupFileNames = [];
  17. public static string $cacheDir = ".scribe/endpoints.cache";
  18. public static string $camelDir = ".scribe/endpoints";
  19. /**
  20. * Load endpoints from the Camel files into groups (arrays).
  21. *
  22. * @param string $folder
  23. *
  24. * @return array[]
  25. */
  26. public static function loadEndpointsIntoGroups(string $folder): array
  27. {
  28. $groups = [];
  29. self::loadEndpointsFromCamelFiles($folder, function ($group) use (&$groups) {
  30. $group['endpoints'] = array_map(function (array $endpoint) {
  31. return OutputEndpointData::fromExtractedEndpointArray($endpoint);
  32. }, $group['endpoints']);
  33. $groups[] = $group;
  34. });
  35. return $groups;
  36. }
  37. /**
  38. * Load endpoints from the Camel files into a flat list of endpoint arrays.
  39. *
  40. * @param string $folder
  41. *
  42. * @return array[]
  43. */
  44. public static function loadEndpointsToFlatPrimitivesArray(string $folder, bool $isFromCache = false): array
  45. {
  46. $endpoints = [];
  47. self::loadEndpointsFromCamelFiles($folder, function ($group) use (&$endpoints) {
  48. foreach ($group['endpoints'] as $endpoint) {
  49. $endpoints[] = $endpoint;
  50. }
  51. }, !$isFromCache);
  52. return $endpoints;
  53. }
  54. public static function loadEndpointsFromCamelFiles(string $folder, callable $callback, bool $storeGroupFilePaths = true)
  55. {
  56. $contents = Utils::listDirectoryContents($folder);
  57. foreach ($contents as $object) {
  58. // Flysystem v1 had items as arrays; v2 has objects.
  59. // v2 allows ArrayAccess, but when we drop v1 support (Laravel <9), we should switch to methods
  60. if (
  61. $object['type'] == 'file'
  62. && Str::endsWith(basename($object['path']), '.yaml')
  63. && !Str::startsWith(basename($object['path']), 'custom.')
  64. ) {
  65. $group = Yaml::parseFile($object['path']);
  66. if ($storeGroupFilePaths) {
  67. $filePathParts = explode('/', $object['path']);
  68. self::$groupFileNames[$group['name']] = end($filePathParts);
  69. }
  70. $callback($group);
  71. }
  72. }
  73. }
  74. public static function loadUserDefinedEndpoints(string $folder): array
  75. {
  76. $contents = Utils::listDirectoryContents($folder);
  77. $userDefinedEndpoints = [];
  78. foreach ($contents as $object) {
  79. // Flysystem v1 had items as arrays; v2 has objects.
  80. // v2 allows ArrayAccess, but when we drop v1 support (Laravel <9), we should switch to methods
  81. if (
  82. $object['type'] == 'file'
  83. && Str::endsWith(basename($object['path']), '.yaml')
  84. && Str::startsWith(basename($object['path']), 'custom.')
  85. ) {
  86. $endpoints = Yaml::parseFile($object['path']);
  87. foreach (($endpoints ?: []) as $endpoint) {
  88. $userDefinedEndpoints[] = $endpoint;
  89. }
  90. }
  91. }
  92. return $userDefinedEndpoints;
  93. }
  94. public static function doesGroupContainEndpoint(array $group, OutputEndpointData $endpoint): bool
  95. {
  96. return boolval(Arr::first($group['endpoints'], function ($e) use ($endpoint) {
  97. return $e->endpointId() === $endpoint->endpointId();
  98. }));
  99. }
  100. public static function getEndpointIndexInGroup(array $groups, OutputEndpointData $endpoint): ?int
  101. {
  102. foreach ($groups as $group) {
  103. foreach ($group['endpoints'] as $index => $endpointInGroup) {
  104. if ($endpointInGroup->endpointId() === $endpoint->endpointId()) {
  105. return $index;
  106. }
  107. }
  108. }
  109. return null;
  110. }
  111. /**
  112. * @param array[] $endpoints
  113. * @param array $endpointGroupIndexes Mapping of endpoint IDs to their index within their group
  114. *
  115. * @return array[]
  116. */
  117. public static function groupEndpoints(array $endpoints, array $endpointGroupIndexes): array
  118. {
  119. $groupedEndpoints = collect($endpoints)
  120. ->groupBy('metadata.groupName')
  121. ->sortKeys(SORT_NATURAL);
  122. return $groupedEndpoints->map(function (Collection $endpointsInGroup) use ($endpointGroupIndexes) {
  123. /** @var Collection<(int|string),ExtractedEndpointData> $endpointsInGroup */
  124. $sortedEndpoints = $endpointsInGroup;
  125. if (!empty($endpointGroupIndexes)) {
  126. $sortedEndpoints = $endpointsInGroup->sortBy(
  127. fn(ExtractedEndpointData $e) => $endpointGroupIndexes[$e->endpointId()] ?? INF,
  128. );
  129. }
  130. return [
  131. 'name' => Arr::first($endpointsInGroup, function (ExtractedEndpointData $endpointData) {
  132. return !empty($endpointData->metadata->groupName);
  133. })->metadata->groupName ?? '',
  134. 'description' => Arr::first($endpointsInGroup, function (ExtractedEndpointData $endpointData) {
  135. return !empty($endpointData->metadata->groupDescription);
  136. })->metadata->groupDescription ?? '',
  137. 'endpoints' => $sortedEndpoints->map(
  138. fn(ExtractedEndpointData $endpointData) => $endpointData->forSerialisation()->toArray()
  139. )->values()->all(),
  140. ];
  141. })->values()->all();
  142. }
  143. public static function prepareGroupedEndpointsForOutput(array $groupedEndpoints): array
  144. {
  145. $groups = array_map(function (array $group) {
  146. return [
  147. 'name' => $group['name'],
  148. 'description' => $group['description'],
  149. 'fileName' => self::$groupFileNames[$group['name']] ?? null,
  150. 'endpoints' => array_map(function (array $endpoint) {
  151. return OutputEndpointData::fromExtractedEndpointArray($endpoint);
  152. }, $group['endpoints']),
  153. ];
  154. }, $groupedEndpoints);
  155. return array_values(Arr::sort($groups, 'fileName'));
  156. }
  157. }