Camel.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. /**
  13. * Mapping of group names to their generated file names. Helps us respect user reordering.
  14. * @var array<string, string>
  15. */
  16. public static array $groupFileNames = [];
  17. /**
  18. * @deprecated Use the cacheDir() method instead
  19. */
  20. public static string $cacheDir = ".scribe/endpoints.cache";
  21. /**
  22. * @deprecated Use the camelDir() method instead
  23. */
  24. public static string $camelDir = ".scribe/endpoints";
  25. public static function cacheDir(string $docsName = 'scribe')
  26. {
  27. return ".$docsName/endpoints.cache";
  28. }
  29. public static function camelDir(string $docsName = 'scribe')
  30. {
  31. return ".$docsName/endpoints";
  32. }
  33. /**
  34. * Load endpoints from the Camel files into groups (arrays).
  35. *
  36. * @param string $folder
  37. *
  38. * @return array[]
  39. */
  40. public static function loadEndpointsIntoGroups(string $folder): array
  41. {
  42. $groups = [];
  43. self::loadEndpointsFromCamelFiles($folder, function ($group) use (&$groups) {
  44. $group['endpoints'] = array_map(function (array $endpoint) {
  45. return OutputEndpointData::fromExtractedEndpointArray($endpoint);
  46. }, $group['endpoints']);
  47. $groups[] = $group;
  48. });
  49. return $groups;
  50. }
  51. /**
  52. * Load endpoints from the Camel files into a flat list of endpoint arrays.
  53. *
  54. * @param string $folder
  55. *
  56. * @return array[]
  57. */
  58. public static function loadEndpointsToFlatPrimitivesArray(string $folder, bool $isFromCache = false): array
  59. {
  60. $endpoints = [];
  61. self::loadEndpointsFromCamelFiles($folder, function ($group) use (&$endpoints) {
  62. foreach ($group['endpoints'] as $endpoint) {
  63. $endpoints[] = $endpoint;
  64. }
  65. }, !$isFromCache);
  66. return $endpoints;
  67. }
  68. public static function loadEndpointsFromCamelFiles(string $folder, callable $callback, bool $storeGroupFilePaths = true)
  69. {
  70. $contents = Utils::listDirectoryContents($folder);
  71. foreach ($contents as $object) {
  72. // Flysystem v1 had items as arrays; v2 has objects.
  73. // v2 allows ArrayAccess, but when we drop v1 support (Laravel <9), we should switch to methods
  74. if (
  75. $object['type'] == 'file'
  76. && Str::endsWith(basename($object['path']), '.yaml')
  77. && !Str::startsWith(basename($object['path']), 'custom.')
  78. ) {
  79. $group = Yaml::parseFile($object['path']);
  80. if ($storeGroupFilePaths) {
  81. $filePathParts = explode('/', $object['path']);
  82. self::$groupFileNames[$group['name']] = end($filePathParts);
  83. }
  84. $callback($group);
  85. }
  86. }
  87. }
  88. public static function loadUserDefinedEndpoints(string $folder): array
  89. {
  90. $contents = Utils::listDirectoryContents($folder);
  91. $userDefinedEndpoints = [];
  92. foreach ($contents as $object) {
  93. // Flysystem v1 had items as arrays; v2 has objects.
  94. // v2 allows ArrayAccess, but when we drop v1 support (Laravel <9), we should switch to methods
  95. if (
  96. $object['type'] == 'file'
  97. && Str::endsWith(basename($object['path']), '.yaml')
  98. && Str::startsWith(basename($object['path']), 'custom.')
  99. ) {
  100. $endpoints = Yaml::parseFile($object['path']);
  101. foreach (($endpoints ?: []) as $endpoint) {
  102. $userDefinedEndpoints[] = $endpoint;
  103. }
  104. }
  105. }
  106. return $userDefinedEndpoints;
  107. }
  108. public static function doesGroupContainEndpoint(array $group, OutputEndpointData $endpoint): bool
  109. {
  110. return boolval(Arr::first($group['endpoints'], function ($e) use ($endpoint) {
  111. return $e->endpointId() === $endpoint->endpointId();
  112. }));
  113. }
  114. public static function getEndpointIndexInGroup(array $groups, OutputEndpointData $endpoint): ?int
  115. {
  116. foreach ($groups as $group) {
  117. foreach ($group['endpoints'] as $index => $endpointInGroup) {
  118. if ($endpointInGroup->endpointId() === $endpoint->endpointId()) {
  119. return $index;
  120. }
  121. }
  122. }
  123. return null;
  124. }
  125. /**
  126. * @param array[] $endpoints
  127. * @param array $endpointGroupIndexes Mapping of endpoint IDs to their index within their group
  128. * @param array $defaultGroupsOrder The order for groups that users specified in their config file.
  129. *
  130. * @return array[]
  131. */
  132. public static function groupEndpoints(array $endpoints, array $endpointGroupIndexes, array $defaultGroupsOrder = []): array
  133. {
  134. $groupedEndpoints = collect($endpoints)->groupBy('metadata.groupName');
  135. if ($defaultGroupsOrder) {
  136. $groupsOrder = Utils::getTopLevelItemsFromMixedConfigList($defaultGroupsOrder);
  137. $groupedEndpoints = $groupedEndpoints->sortKeysUsing(self::getOrderListComparator($groupsOrder));
  138. } else {
  139. $groupedEndpoints = $groupedEndpoints->sortKeys(SORT_NATURAL);
  140. }
  141. return $groupedEndpoints->map(function (Collection $endpointsInGroup) use ($defaultGroupsOrder, $endpointGroupIndexes) {
  142. /** @var Collection<(int|string),ExtractedEndpointData> $endpointsInGroup */
  143. $sortedEndpoints = $endpointsInGroup;
  144. if (empty($endpointGroupIndexes)) {
  145. $groupName = data_get($endpointsInGroup[0], 'metadata.groupName');
  146. if ($defaultGroupsOrder && isset($defaultGroupsOrder[$groupName])) {
  147. $subGroupOrEndpointsOrder = Utils::getTopLevelItemsFromMixedConfigList($defaultGroupsOrder[$groupName]);
  148. $sortedEndpoints = $endpointsInGroup->sortBy(
  149. function (ExtractedEndpointData $e) use ($defaultGroupsOrder, $subGroupOrEndpointsOrder) {
  150. $endpointIdentifier = $e->httpMethods[0].' /'.$e->uri;
  151. $index = array_search($e->metadata->subgroup, $subGroupOrEndpointsOrder);
  152. if ($index !== false) {
  153. // This is a subgroup
  154. $endpointsOrderInSubgroup = $defaultGroupsOrder[$e->metadata->groupName][$e->metadata->subgroup] ?? null;
  155. if ($endpointsOrderInSubgroup) {
  156. $indexInSubGroup = array_search($endpointIdentifier, $endpointsOrderInSubgroup);
  157. $index = ($indexInSubGroup === false) ? $index : ($index + ($indexInSubGroup * 0.1));
  158. }
  159. } else {
  160. // This is an endpoint
  161. $index = array_search($endpointIdentifier, $subGroupOrEndpointsOrder);
  162. }
  163. return $index === false ? INF : $index;
  164. },
  165. );
  166. }
  167. } else {
  168. $sortedEndpoints = $endpointsInGroup->sortBy(
  169. fn(ExtractedEndpointData $e) => $endpointGroupIndexes[$e->endpointId()] ?? INF,
  170. );
  171. }
  172. return [
  173. 'name' => Arr::first($endpointsInGroup, function (ExtractedEndpointData $endpointData) {
  174. return !empty($endpointData->metadata->groupName);
  175. })->metadata->groupName ?? '',
  176. 'description' => Arr::first($endpointsInGroup, function (ExtractedEndpointData $endpointData) {
  177. return !empty($endpointData->metadata->groupDescription);
  178. })->metadata->groupDescription ?? '',
  179. 'endpoints' => $sortedEndpoints->map(
  180. fn(ExtractedEndpointData $endpointData) => $endpointData->forSerialisation()->toArray()
  181. )->values()->all(),
  182. ];
  183. })->values()->all();
  184. }
  185. public static function prepareGroupedEndpointsForOutput(array $groupedEndpoints): array
  186. {
  187. $groups = array_map(function (array $group) {
  188. return [
  189. 'name' => $group['name'],
  190. 'description' => $group['description'],
  191. 'fileName' => self::$groupFileNames[$group['name']] ?? null,
  192. 'endpoints' => array_map(function (array $endpoint) {
  193. return OutputEndpointData::fromExtractedEndpointArray($endpoint);
  194. }, $group['endpoints']),
  195. ];
  196. }, $groupedEndpoints);
  197. return array_values(Arr::sort($groups, 'fileName'));
  198. }
  199. /**
  200. * Given an $order list like ['first', 'second', ...], return a compare function that can be used to sort
  201. * a list of strings based on the $order list. Any strings not in the list are sorted with natural sort.
  202. *
  203. * @param array $order
  204. */
  205. public static function getOrderListComparator(array $order): \Closure
  206. {
  207. return function ($a, $b) use ($order) {
  208. $indexOfA = array_search($a, $order);
  209. $indexOfB = array_search($b, $order);
  210. if ($indexOfA !== false && $indexOfB !== false) {
  211. return $indexOfA <=> $indexOfB;
  212. }
  213. // If only the first is in the default order, then it must come before the second.
  214. if ($indexOfA !== false) {
  215. return -1;
  216. }
  217. // If only the second is in the default order, then first must come after it.
  218. if ($indexOfB !== false) {
  219. return 1;
  220. }
  221. // If neither is present, fall back to natural sort
  222. return strnatcmp($a, $b);
  223. };
  224. }
  225. }