Camel.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 League\Flysystem\Adapter\Local;
  9. use League\Flysystem\Filesystem;
  10. use Symfony\Component\Yaml\Yaml;
  11. class Camel
  12. {
  13. /**
  14. * Mapping of group names to their generated file names. Helps us respect user reordering.
  15. * @var array<string, string>
  16. */
  17. public static array $groupFileNames = [];
  18. /**
  19. * Load endpoints from the Camel files into groups (arrays).
  20. *
  21. * @param string $folder
  22. *
  23. * @return array[]
  24. */
  25. public static function loadEndpointsIntoGroups(string $folder): array
  26. {
  27. $groups = [];
  28. self::loadEndpointsFromCamelFiles($folder, function ($group) use (&$groups) {
  29. $group['endpoints'] = array_map(function (array $endpoint) {
  30. return OutputEndpointData::fromExtractedEndpointArray($endpoint);
  31. }, $group['endpoints']);
  32. $groups[] = $group;
  33. });
  34. return $groups;
  35. }
  36. /**
  37. * Load endpoints from the Camel files into a flat list of endpoint arrays.
  38. *
  39. * @param string $folder
  40. *
  41. * @return array[]
  42. */
  43. public static function loadEndpointsToFlatPrimitivesArray(string $folder, bool $isFromCache = false): array
  44. {
  45. $endpoints = [];
  46. self::loadEndpointsFromCamelFiles($folder, function ($group) use (&$endpoints) {
  47. foreach ($group['endpoints'] as $endpoint) {
  48. $endpoints[] = $endpoint;
  49. }
  50. }, !$isFromCache);
  51. return $endpoints;
  52. }
  53. public static function loadEndpointsFromCamelFiles(string $folder, callable $callback, bool $storeGroupFilePaths = true)
  54. {
  55. $adapter = new Local(getcwd());
  56. $fs = new Filesystem($adapter);
  57. $contents = $fs->listContents($folder);;
  58. foreach ($contents as $object) {
  59. if (
  60. $object['type'] == 'file'
  61. && Str::endsWith($object['basename'], '.yaml')
  62. && !Str::startsWith($object['basename'], 'custom.')
  63. ) {
  64. $group = Yaml::parseFile($object['path']);
  65. if ($storeGroupFilePaths) {
  66. $filePathParts = explode('/', $object['path']);
  67. self::$groupFileNames[$group['name']] = end($filePathParts);
  68. }
  69. $callback($group);
  70. }
  71. }
  72. }
  73. public static function loadUserDefinedEndpoints(string $folder): array
  74. {
  75. $adapter = new Local(getcwd());
  76. $fs = new Filesystem($adapter);
  77. $contents = $fs->listContents($folder);;
  78. $userDefinedEndpoints = [];
  79. foreach ($contents as $object) {
  80. if (
  81. $object['type'] == 'file'
  82. && Str::endsWith($object['basename'], '.yaml')
  83. && Str::startsWith($object['basename'], 'custom.')
  84. ) {
  85. $endpoints = Yaml::parseFile($object['path']);
  86. foreach (($endpoints ?: []) as $endpoint) {
  87. $userDefinedEndpoints[] = $endpoint;
  88. }
  89. }
  90. }
  91. return $userDefinedEndpoints;
  92. }
  93. public static function doesGroupContainEndpoint(array $group, OutputEndpointData $endpoint): bool
  94. {
  95. return boolval(Arr::first($group['endpoints'], function ($e) use ($endpoint) {
  96. return $e->endpointId() === $endpoint->endpointId();
  97. }));
  98. }
  99. public static function getEndpointIndexInGroup(array $groups, OutputEndpointData $endpoint): ?int
  100. {
  101. foreach ($groups as $group) {
  102. foreach ($group['endpoints'] as $index => $endpointInGroup) {
  103. if ($endpointInGroup->endpointId() === $endpoint->endpointId()) {
  104. return $index;
  105. }
  106. }
  107. }
  108. return null;
  109. }
  110. /**
  111. * @param array[] $endpoints
  112. * @param array $endpointGroupIndexes Mapping of endpoint IDs to their index within their group
  113. *
  114. * @return array[]
  115. */
  116. public static function groupEndpoints(array $endpoints, array $endpointGroupIndexes): array
  117. {
  118. $groupedEndpoints = collect($endpoints)
  119. ->groupBy('metadata.groupName')
  120. ->sortKeys(SORT_NATURAL);
  121. return $groupedEndpoints->map(function (Collection $endpointsInGroup) use ($endpointGroupIndexes) {
  122. $sortedEndpoints = $endpointsInGroup;
  123. if (!empty($endpointGroupIndexes)) {
  124. $sortedEndpoints = $endpointsInGroup->sortBy(
  125. fn(ExtractedEndpointData $e) => $endpointGroupIndexes[$e->endpointId()] ?? INF,
  126. );
  127. }
  128. return [
  129. 'name' => Arr::first($endpointsInGroup, function (ExtractedEndpointData $endpointData) {
  130. return !empty($endpointData->metadata->groupName);
  131. })->metadata->groupName ?? '',
  132. 'description' => Arr::first($endpointsInGroup, function (ExtractedEndpointData $endpointData) {
  133. return !empty($endpointData->metadata->groupDescription);
  134. })->metadata->groupDescription ?? '',
  135. 'endpoints' => $sortedEndpoints->map(fn(ExtractedEndpointData $endpointData) => $endpointData->forSerialisation()->toArray())->values()->all(),
  136. ];
  137. })->values()->all();
  138. }
  139. public static function prepareGroupedEndpointsForOutput(array $groupedEndpoints): array
  140. {
  141. $groups = array_map(function (array $group) {
  142. return [
  143. 'name' => $group['name'],
  144. 'description' => $group['description'],
  145. 'fileName' => self::$groupFileNames[$group['name']] ?? null,
  146. 'endpoints' => array_map(function (array $endpoint) {
  147. return OutputEndpointData::fromExtractedEndpointArray($endpoint);
  148. }, $group['endpoints']),
  149. ];
  150. }, $groupedEndpoints);
  151. return Arr::sort($groups, 'fileName');
  152. }
  153. }