Camel.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. * Load endpoints from the Camel files into groups (arrays).
  15. *
  16. * @param string $folder
  17. *
  18. * @return array[]
  19. */
  20. public static function loadEndpointsIntoGroups(string $folder): array
  21. {
  22. $groups = [];
  23. self::loadEndpointsFromCamelFiles($folder, function ($group) use (&$groups) {
  24. $group['endpoints'] = array_map(function (array $endpoint) {
  25. return OutputEndpointData::fromExtractedEndpointArray($endpoint);
  26. }, $group['endpoints']);
  27. $groups[] = $group;
  28. });
  29. return $groups;
  30. }
  31. /**
  32. * Load endpoints from the Camel files into a flat list of endpoint arrays.
  33. *
  34. * @param string $folder
  35. *
  36. * @return array[]
  37. */
  38. public static function loadEndpointsToFlatPrimitivesArray(string $folder): array
  39. {
  40. $endpoints = [];
  41. self::loadEndpointsFromCamelFiles($folder, function ($group) use (&$endpoints) {
  42. foreach ($group['endpoints'] as $endpoint) {
  43. $endpoints[] = $endpoint;
  44. }
  45. });
  46. return $endpoints;
  47. }
  48. public static function loadEndpointsFromCamelFiles(string $folder, callable $callback)
  49. {
  50. $adapter = new Local(getcwd());
  51. $fs = new Filesystem($adapter);
  52. $contents = $fs->listContents($folder);;
  53. foreach ($contents as $object) {
  54. if (
  55. $object['type'] == 'file'
  56. && Str::endsWith($object['basename'], '.yaml')
  57. && !Str::startsWith($object['basename'], 'custom.')
  58. ) {
  59. $group = Yaml::parseFile($object['path']);
  60. $callback($group);
  61. }
  62. }
  63. }
  64. public static function loadUserDefinedEndpoints(string $folder): array
  65. {
  66. $adapter = new Local(getcwd());
  67. $fs = new Filesystem($adapter);
  68. $contents = $fs->listContents($folder);;
  69. $userDefinedEndpoints = [];
  70. foreach ($contents as $object) {
  71. if (
  72. $object['type'] == 'file'
  73. && Str::endsWith($object['basename'], '.yaml')
  74. && Str::startsWith($object['basename'], 'custom.')
  75. ) {
  76. $endpoints = Yaml::parseFile($object['path']);
  77. foreach (($endpoints ?: []) as $endpoint) {
  78. $userDefinedEndpoints[] = $endpoint;
  79. }
  80. }
  81. }
  82. return $userDefinedEndpoints;
  83. }
  84. public static function doesGroupContainEndpoint(array $group, $endpoint): bool
  85. {
  86. return boolval(Arr::first($group['endpoints'], function ($e) use ($endpoint) {
  87. return $e->endpointId() === $endpoint->endpointId();
  88. }));
  89. }
  90. /**
  91. * @param array[] $endpoints
  92. *
  93. * @return array[]
  94. */
  95. public static function groupEndpoints(array $endpoints): array
  96. {
  97. $groupedEndpoints = collect($endpoints)
  98. ->groupBy('metadata.groupName')
  99. ->sortKeys(SORT_NATURAL);
  100. return $groupedEndpoints->map(fn(Collection $group) => [
  101. 'name' => $group[0]->metadata->groupName,
  102. 'description' => Arr::first($group, function (ExtractedEndpointData $endpointData) {
  103. return $endpointData->metadata->groupDescription !== '';
  104. })->metadata->groupDescription ?? '',
  105. 'endpoints' => $group->map(fn(ExtractedEndpointData $endpointData) => $endpointData->forSerialisation()->toArray())->all(),
  106. ])->values()->all();
  107. }
  108. public static function prepareGroupedEndpointsForOutput(array $groupedEndpoints): array
  109. {
  110. return array_map(function (array $group) {
  111. return [
  112. 'name' => $group['name'],
  113. 'description' => $group['description'],
  114. 'endpoints' => array_map(function (array $endpoint) {
  115. return OutputEndpointData::fromExtractedEndpointArray($endpoint);
  116. }, $group['endpoints']),
  117. ];
  118. }, $groupedEndpoints);
  119. }
  120. }