Camel.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. $subGroupOrEndpointsOrder = Utils::getTopLevelItemsFromMixedConfigList($configFileOrder[$groupName]);
  113. $sortedEndpoints = $sortedEndpoints->sortBy(
  114. function (OutputEndpointData $e) use ($configFileOrder, $subGroupOrEndpointsOrder) {
  115. $endpointIdentifier = $e->httpMethods[0] . ' /' . $e->uri;
  116. // First, check if there's an ordering specified for the endpoint's subgroup
  117. $index = array_search($e->metadata->subgroup, $subGroupOrEndpointsOrder);
  118. if ($index !== false) {
  119. // There's a subgroup order; check if there's an endpoints order within that
  120. $endpointsOrderInSubgroup = $configFileOrder[$e->metadata->groupName][$e->metadata->subgroup] ?? null;
  121. if ($endpointsOrderInSubgroup) {
  122. $indexInSubGroup = array_search($endpointIdentifier, $endpointsOrderInSubgroup);
  123. $index = ($indexInSubGroup === false) ? $index : ($index + ($indexInSubGroup * 0.1));
  124. }
  125. } else {
  126. // No subgroup order; check if there's an ordering for this endpoint
  127. $index = array_search($endpointIdentifier, $subGroupOrEndpointsOrder);
  128. }
  129. return $index === false ? INF : $index;
  130. },
  131. );
  132. }
  133. return [
  134. 'name' => $groupName,
  135. 'description' => $group['description'],
  136. 'endpoints' => $sortedEndpoints->all(),
  137. ];
  138. })->values()->all();
  139. }
  140. /**
  141. * Prepare endpoints to be turned into HTML.
  142. * Map them into OutputEndpointData DTOs, and sort them by the specified order in the config file.
  143. *
  144. * @param array<string,array[]> $groupedEndpoints
  145. *
  146. * @return array
  147. */
  148. public static function prepareGroupedEndpointsForOutput(array $groupedEndpoints, array $configFileOrder = []): array
  149. {
  150. $groups = array_map(function (array $group) {
  151. return [
  152. 'name' => $group['name'],
  153. 'description' => $group['description'],
  154. 'endpoints' => array_map(
  155. fn(array $endpoint) => OutputEndpointData::fromExtractedEndpointArray($endpoint), $group['endpoints']
  156. ),
  157. ];
  158. }, $groupedEndpoints);
  159. return Camel::sortByConfigFileOrder($groups, $configFileOrder);
  160. }
  161. /**
  162. * Given an $order list like ['first', 'second', ...], return a compare function that can be used to sort
  163. * a list of strings based on the order of items in $order.
  164. * Any strings not in the list are sorted with natural sort.
  165. *
  166. * @param array $order
  167. */
  168. public static function getOrderListComparator(array $order): \Closure
  169. {
  170. return function ($a, $b) use ($order) {
  171. $indexOfA = array_search($a, $order);
  172. $indexOfB = array_search($b, $order);
  173. // If both are in the $order list, compare them normally based on their position in the list
  174. if ($indexOfA !== false && $indexOfB !== false) {
  175. return $indexOfA <=> $indexOfB;
  176. }
  177. // If only A is in the $order list, then it must come before B.
  178. if ($indexOfA !== false) {
  179. return -1;
  180. }
  181. // If only B is in the $order list, then it must come before A.
  182. if ($indexOfB !== false) {
  183. return 1;
  184. }
  185. // If neither is present, fall back to natural sort
  186. return strnatcmp($a, $b);
  187. };
  188. }
  189. }