Camel.php 6.2 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 League\Flysystem\Adapter\Local;
  9. use League\Flysystem\Filesystem;
  10. use Symfony\Component\Yaml\Yaml;
  11. class Camel
  12. {
  13. /**
  14. * Mapping of group names to their generated file names. Helps us respect user reordering.
  15. * @var array<string, string>
  16. */
  17. public static array $groupFileNames = [];
  18. public static string $cacheDir = ".scribe/endpoints.cache";
  19. public static string $camelDir = ".scribe/endpoints";
  20. /**
  21. * Load endpoints from the Camel files into groups (arrays).
  22. *
  23. * @param string $folder
  24. *
  25. * @return array[]
  26. */
  27. public static function loadEndpointsIntoGroups(string $folder): array
  28. {
  29. $groups = [];
  30. self::loadEndpointsFromCamelFiles($folder, function ($group) use (&$groups) {
  31. $group['endpoints'] = array_map(function (array $endpoint) {
  32. return OutputEndpointData::fromExtractedEndpointArray($endpoint);
  33. }, $group['endpoints']);
  34. $groups[] = $group;
  35. });
  36. return $groups;
  37. }
  38. /**
  39. * Load endpoints from the Camel files into a flat list of endpoint arrays.
  40. *
  41. * @param string $folder
  42. *
  43. * @return array[]
  44. */
  45. public static function loadEndpointsToFlatPrimitivesArray(string $folder, bool $isFromCache = false): array
  46. {
  47. $endpoints = [];
  48. self::loadEndpointsFromCamelFiles($folder, function ($group) use (&$endpoints) {
  49. foreach ($group['endpoints'] as $endpoint) {
  50. $endpoints[] = $endpoint;
  51. }
  52. }, !$isFromCache);
  53. return $endpoints;
  54. }
  55. public static function loadEndpointsFromCamelFiles(string $folder, callable $callback, bool $storeGroupFilePaths = true)
  56. {
  57. $adapter = new Local(getcwd());
  58. $fs = new Filesystem($adapter);
  59. $contents = $fs->listContents($folder);;
  60. foreach ($contents as $object) {
  61. if (
  62. $object['type'] == 'file'
  63. && Str::endsWith($object['basename'], '.yaml')
  64. && !Str::startsWith($object['basename'], 'custom.')
  65. ) {
  66. $group = Yaml::parseFile($object['path']);
  67. if ($storeGroupFilePaths) {
  68. $filePathParts = explode('/', $object['path']);
  69. self::$groupFileNames[$group['name']] = end($filePathParts);
  70. }
  71. $callback($group);
  72. }
  73. }
  74. }
  75. public static function loadUserDefinedEndpoints(string $folder): array
  76. {
  77. $adapter = new Local(getcwd());
  78. $fs = new Filesystem($adapter);
  79. $contents = $fs->listContents($folder);;
  80. $userDefinedEndpoints = [];
  81. foreach ($contents as $object) {
  82. if (
  83. $object['type'] == 'file'
  84. && Str::endsWith($object['basename'], '.yaml')
  85. && Str::startsWith($object['basename'], 'custom.')
  86. ) {
  87. $endpoints = Yaml::parseFile($object['path']);
  88. foreach (($endpoints ?: []) as $endpoint) {
  89. $userDefinedEndpoints[] = $endpoint;
  90. }
  91. }
  92. }
  93. return $userDefinedEndpoints;
  94. }
  95. public static function doesGroupContainEndpoint(array $group, OutputEndpointData $endpoint): bool
  96. {
  97. return boolval(Arr::first($group['endpoints'], function ($e) use ($endpoint) {
  98. return $e->endpointId() === $endpoint->endpointId();
  99. }));
  100. }
  101. public static function getEndpointIndexInGroup(array $groups, OutputEndpointData $endpoint): ?int
  102. {
  103. foreach ($groups as $group) {
  104. foreach ($group['endpoints'] as $index => $endpointInGroup) {
  105. if ($endpointInGroup->endpointId() === $endpoint->endpointId()) {
  106. return $index;
  107. }
  108. }
  109. }
  110. return null;
  111. }
  112. /**
  113. * @param array[] $endpoints
  114. * @param array $endpointGroupIndexes Mapping of endpoint IDs to their index within their group
  115. *
  116. * @return array[]
  117. */
  118. public static function groupEndpoints(array $endpoints, array $endpointGroupIndexes): array
  119. {
  120. $groupedEndpoints = collect($endpoints)
  121. ->groupBy('metadata.groupName')
  122. ->sortKeys(SORT_NATURAL);
  123. return $groupedEndpoints->map(function (Collection $endpointsInGroup) use ($endpointGroupIndexes) {
  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. }