Camel.php 8.2 KB

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