Camel.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. namespace Knuckles\Camel;
  3. use Illuminate\Support\Arr;
  4. use Illuminate\Support\Str;
  5. use Knuckles\Camel\Output\OutputEndpointData;
  6. use Knuckles\Scribe\Tools\PathConfig;
  7. use Knuckles\Scribe\Tools\Utils;
  8. use Symfony\Component\Yaml\Yaml;
  9. class Camel
  10. {
  11. public static function cacheDir(PathConfig $paths): string
  12. {
  13. return $paths->intermediateOutputPath('endpoints.cache');
  14. }
  15. public static function camelDir(PathConfig $paths): string
  16. {
  17. return $paths->intermediateOutputPath('endpoints');
  18. }
  19. /**
  20. * Load endpoints from the Camel files into groups (arrays).
  21. *
  22. * @param string $folder
  23. *
  24. * @return array[] Each array is a group with keys including `name` and `endpoints`.
  25. */
  26. public static function loadEndpointsIntoGroups(string $folder): array
  27. {
  28. $groups = [];
  29. self::loadEndpointsFromCamelFiles($folder, function (array $group) use (&$groups) {
  30. $groups[$group['name']] = $group;
  31. });
  32. return $groups;
  33. }
  34. /**
  35. * Load endpoints from the Camel files into a flat list of endpoint arrays.
  36. * Useful when we don't care about groups, but simply want to compare endpoints contents
  37. * to see if anything changed.
  38. *
  39. * @param string $folder
  40. *
  41. * @return array[] List of endpoint arrays.
  42. */
  43. public static function loadEndpointsToFlatPrimitivesArray(string $folder): array
  44. {
  45. $endpoints = [];
  46. self::loadEndpointsFromCamelFiles($folder, function (array $group) use (&$endpoints) {
  47. foreach ($group['endpoints'] as $endpoint) {
  48. $endpoints[] = $endpoint;
  49. }
  50. });
  51. return $endpoints;
  52. }
  53. public static function loadEndpointsFromCamelFiles(string $folder, callable $callback): void
  54. {
  55. $contents = Utils::listDirectoryContents($folder);
  56. foreach ($contents as $object) {
  57. if (
  58. $object->isFile()
  59. && Str::endsWith(basename($object->path()), '.yaml')
  60. && !Str::startsWith(basename($object->path()), 'custom.')
  61. ) {
  62. $group = Yaml::parseFile($object['path']);
  63. $callback($group);
  64. }
  65. }
  66. }
  67. public static function loadUserDefinedEndpoints(string $folder): array
  68. {
  69. $contents = Utils::listDirectoryContents($folder);
  70. $userDefinedEndpoints = [];
  71. foreach ($contents as $object) {
  72. if (
  73. $object->isFile()
  74. && Str::endsWith(basename($object->path()), '.yaml')
  75. && Str::startsWith(basename($object->path()), 'custom.')
  76. ) {
  77. $endpoints = Yaml::parseFile($object->path());
  78. foreach (($endpoints ?: []) as $endpoint) {
  79. $userDefinedEndpoints[] = $endpoint;
  80. }
  81. }
  82. }
  83. return $userDefinedEndpoints;
  84. }
  85. public static function doesGroupContainEndpoint(array $group, OutputEndpointData $endpoint): bool
  86. {
  87. return boolval(Arr::first($group['endpoints'], function ($e) use ($endpoint) {
  88. return $e->endpointId() === $endpoint->endpointId();
  89. }));
  90. }
  91. /**
  92. * @param array[] $groupedEndpoints
  93. * @param array $configFileOrder The order for groups that users specified in their config file.
  94. *
  95. * @return array[]
  96. */
  97. public static function sortByConfigFileOrder(array $groupedEndpoints, array $configFileOrder): array
  98. {
  99. if (empty($configFileOrder)) {
  100. ksort($groupedEndpoints, SORT_NATURAL);
  101. return $groupedEndpoints;
  102. }
  103. // First, sort groups
  104. $groupsOrder = Utils::getTopLevelItemsFromMixedConfigList($configFileOrder);
  105. $groupsCollection = collect($groupedEndpoints);
  106. $wildcardPosition = array_search('*', $groupsOrder);
  107. if ($wildcardPosition !== false) {
  108. $promotedGroups = array_splice($groupsOrder, 0, $wildcardPosition);
  109. $demotedGroups = array_splice($groupsOrder, 1);
  110. $promotedOrderedGroups = $groupsCollection->filter(fn ($group, $groupName) => in_array($groupName, $promotedGroups))
  111. ->sortKeysUsing(self::getOrderListComparator($promotedGroups));
  112. $demotedOrderedGroups = $groupsCollection->filter(fn ($group, $groupName) => in_array($groupName, $demotedGroups))
  113. ->sortKeysUsing(self::getOrderListComparator($demotedGroups));
  114. $nonWildcardGroups = array_merge($promotedGroups, $demotedGroups);
  115. $wildCardOrderedGroups = $groupsCollection->filter(fn ($group, $groupName) => !in_array($groupName, $nonWildcardGroups))
  116. ->sortKeysUsing(self::getOrderListComparator($demotedGroups));
  117. $groupedEndpoints = $promotedOrderedGroups->merge($wildCardOrderedGroups)
  118. ->merge($demotedOrderedGroups);
  119. } else {
  120. $groupedEndpoints = $groupsCollection->sortKeysUsing(self::getOrderListComparator($groupsOrder));
  121. }
  122. return $groupedEndpoints->map(function (array $group, string $groupName) use ($configFileOrder) {
  123. $sortedEndpoints = collect($group['endpoints']);
  124. if (isset($configFileOrder[$groupName])) {
  125. // Second-level order list. Can contain endpoint or subgroup names
  126. $level2Order = Utils::getTopLevelItemsFromMixedConfigList($configFileOrder[$groupName]);
  127. $sortedEndpoints = $sortedEndpoints->sortBy(
  128. function (OutputEndpointData $e) use ($configFileOrder, $level2Order) {
  129. $endpointIdentifier = $e->httpMethods[0] . ' /' . $e->uri;
  130. // First, check if there's an ordering specified for the endpoint itself
  131. $indexOfEndpointInL2Order = array_search($endpointIdentifier, $level2Order);
  132. if ($indexOfEndpointInL2Order !== false) {
  133. return $indexOfEndpointInL2Order;
  134. }
  135. // Check if there's an ordering for the endpoint's subgroup
  136. $indexOfSubgroupInL2Order = array_search($e->metadata->subgroup, $level2Order);
  137. if ($indexOfSubgroupInL2Order !== false) {
  138. // There's a subgroup order; check if there's an endpoints order within that
  139. $orderOfEndpointsInSubgroup = $configFileOrder[$e->metadata->groupName][$e->metadata->subgroup] ?? [];
  140. $indexOfEndpointInSubGroup = array_search($endpointIdentifier, $orderOfEndpointsInSubgroup);
  141. return ($indexOfEndpointInSubGroup === false)
  142. ? $indexOfSubgroupInL2Order
  143. : ($indexOfSubgroupInL2Order + ($indexOfEndpointInSubGroup * 0.1));
  144. }
  145. return INF;
  146. },
  147. );
  148. }
  149. return [
  150. 'name' => $groupName,
  151. 'description' => $group['description'],
  152. 'endpoints' => $sortedEndpoints->all(),
  153. ];
  154. })->values()->all();
  155. }
  156. /**
  157. * Prepare endpoints to be turned into HTML.
  158. * Map them into OutputEndpointData DTOs, and sort them by the specified order in the config file.
  159. *
  160. * @param array<string,array[]> $groupedEndpoints
  161. *
  162. * @return array
  163. */
  164. public static function prepareGroupedEndpointsForOutput(array $groupedEndpoints, array $configFileOrder = []): array
  165. {
  166. $groups = array_map(function (array $group) {
  167. return [
  168. 'name' => $group['name'],
  169. 'description' => $group['description'],
  170. 'endpoints' => array_map(
  171. fn(array $endpoint) => OutputEndpointData::fromExtractedEndpointArray($endpoint), $group['endpoints']
  172. ),
  173. ];
  174. }, $groupedEndpoints);
  175. return Camel::sortByConfigFileOrder($groups, $configFileOrder);
  176. }
  177. /**
  178. * Given an $order list like ['first', 'second', ...], return a compare function that can be used to sort
  179. * a list of strings based on the order of items in $order.
  180. * Any strings not in the list are sorted with natural sort.
  181. *
  182. * @param array $order
  183. */
  184. protected static function getOrderListComparator(array $order): \Closure
  185. {
  186. return function ($a, $b) use ($order) {
  187. $indexOfA = array_search($a, $order);
  188. $indexOfB = array_search($b, $order);
  189. // If both are in the $order list, compare them normally based on their position in the list
  190. if ($indexOfA !== false && $indexOfB !== false) {
  191. return $indexOfA <=> $indexOfB;
  192. }
  193. // If only A is in the $order list, then it must come before B.
  194. if ($indexOfA !== false) {
  195. return -1;
  196. }
  197. // If only B is in the $order list, then it must come before A.
  198. if ($indexOfB !== false) {
  199. return 1;
  200. }
  201. // If neither is present, fall back to natural sort
  202. return strnatcmp($a, $b);
  203. };
  204. }
  205. }