Camel.php 6.8 KB

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