Camel.php 8.2 KB

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