Camel.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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, $endpoint): bool
  94. {
  95. return boolval(Arr::first($group['endpoints'], function ($e) use ($endpoint) {
  96. return $e->endpointId() === $endpoint->endpointId();
  97. }));
  98. }
  99. /**
  100. * @param array[] $endpoints
  101. *
  102. * @return array[]
  103. */
  104. public static function groupEndpoints(array $endpoints): array
  105. {
  106. $groupedEndpoints = collect($endpoints)
  107. ->groupBy('metadata.groupName')
  108. ->sortKeys(SORT_NATURAL);
  109. return $groupedEndpoints->map(fn(Collection $group) => [
  110. 'name' => $group[0]->metadata->groupName,
  111. 'description' => Arr::first($group, function (ExtractedEndpointData $endpointData) {
  112. return $endpointData->metadata->groupDescription !== '';
  113. })->metadata->groupDescription ?? '',
  114. 'endpoints' => $group->map(fn(ExtractedEndpointData $endpointData) => $endpointData->forSerialisation()->toArray())->all(),
  115. ])->values()->all();
  116. }
  117. public static function prepareGroupedEndpointsForOutput(array $groupedEndpoints): array
  118. {
  119. $groups = array_map(function (array $group) {
  120. return [
  121. 'name' => $group['name'],
  122. 'description' => $group['description'],
  123. 'fileName' => self::$groupFileNames[$group['name']] ?? null,
  124. 'endpoints' => array_map(function (array $endpoint) {
  125. return OutputEndpointData::fromExtractedEndpointArray($endpoint);
  126. }, $group['endpoints']),
  127. ];
  128. }, $groupedEndpoints);
  129. return Arr::sort($groups, 'fileName');
  130. }
  131. }