ExtractedEndpointData.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace Knuckles\Camel\Extraction;
  3. use Illuminate\Routing\Route;
  4. use Illuminate\Support\Str;
  5. use Knuckles\Camel\BaseDTO;
  6. use Knuckles\Scribe\Tools\Utils as u;
  7. use ReflectionClass;
  8. use ReflectionFunctionAbstract;
  9. class ExtractedEndpointData extends BaseDTO
  10. {
  11. /**
  12. * @var array<string>
  13. */
  14. public array $methods;
  15. public string $uri;
  16. public Metadata $metadata;
  17. /**
  18. * @var array<string,string>
  19. */
  20. public array $headers = [];
  21. /**
  22. * @var array
  23. * @var array<string,\Knuckles\Camel\Extraction\Parameter>
  24. */
  25. public array $urlParameters = [];
  26. /**
  27. * @var array<string,mixed>
  28. */
  29. public array $cleanUrlParameters = [];
  30. /**
  31. * @var array
  32. * @var array<string,\Knuckles\Camel\Extraction\Parameter>
  33. */
  34. public array $queryParameters = [];
  35. /**
  36. * @var array<string,mixed>
  37. */
  38. public array $cleanQueryParameters = [];
  39. public array $bodyParameters = [];
  40. /**
  41. * @var array<string,mixed>
  42. */
  43. public array $cleanBodyParameters = [];
  44. /**
  45. * @var array<string,\Illuminate\Http\UploadedFile|array>
  46. */
  47. public array $fileParameters = [];
  48. /**
  49. * @var ResponseCollection|array
  50. */
  51. public $responses;
  52. /**
  53. * @var array
  54. * @var array<string,\Knuckles\Camel\Extraction\ResponseField>
  55. */
  56. public array $responseFields = [];
  57. /**
  58. * Authentication info for this endpoint. In the form [{where}, {name}, {sample}]
  59. * Example: ["queryParameters", "api_key", "njiuyiw97865rfyvgfvb1"]
  60. */
  61. public array $auth = [];
  62. public ?ReflectionClass $controller;
  63. public ?ReflectionFunctionAbstract $method;
  64. public ?Route $route;
  65. public function __construct(array $parameters = [])
  66. {
  67. $parameters['uri'] = $this->normalizeResourceParamName($parameters['uri'], $parameters['route']);
  68. $parameters['metadata'] = $parameters['metadata'] ?? new Metadata([]);
  69. $parameters['responses'] = $parameters['responses'] ?? new ResponseCollection([]);
  70. parent::__construct($parameters);
  71. }
  72. public static function fromRoute(Route $route, array $extras = []): self
  73. {
  74. $methods = self::getMethods($route);
  75. $uri = $route->uri();
  76. [$controllerName, $methodName] = u::getRouteClassAndMethodNames($route);
  77. $controller = new ReflectionClass($controllerName);
  78. $method = u::getReflectedRouteMethod([$controllerName, $methodName]);
  79. $data = compact('methods', 'uri', 'controller', 'method', 'route');
  80. $data = array_merge($data, $extras);
  81. return new ExtractedEndpointData($data);
  82. }
  83. /**
  84. * @param Route $route
  85. *
  86. * @return array<string>
  87. */
  88. public static function getMethods(Route $route): array
  89. {
  90. $methods = $route->methods();
  91. // Laravel adds an automatic "HEAD" endpoint for each GET request, so we'll strip that out,
  92. // but not if there's only one method (means it was intentional)
  93. if (count($methods) === 1) {
  94. return $methods;
  95. }
  96. return array_diff($methods, ['HEAD']);
  97. }
  98. public function name()
  99. {
  100. return sprintf("[%s] {$this->route->uri}.", implode(',', $this->route->methods));
  101. }
  102. public function endpointId()
  103. {
  104. return $this->methods[0] . str_replace(['/', '?', '{', '}', ':'], '-', $this->uri);
  105. }
  106. public function normalizeResourceParamName(string $uri, Route $route): string
  107. {
  108. $params = [];
  109. preg_match_all('#\{(\w+?)}#', $uri, $params);
  110. $foundResourceParam = false;
  111. foreach ($params[1] as $param) {
  112. $pluralParam = Str::plural($param);
  113. $resourceRouteNames = ["$pluralParam.show", "$pluralParam.update", "$pluralParam.destroy"];
  114. if (Str::contains($route->action['as'], $resourceRouteNames)) {
  115. $search = sprintf("%s/{%s}", $pluralParam, $param);
  116. if (!$foundResourceParam) {
  117. // Only the first resource param should be {id}
  118. $replace = "$pluralParam/{id}";
  119. $foundResourceParam = true;
  120. } else {
  121. // Subsequent ones should be {<param>_id}
  122. $replace = sprintf("%s/{%s}", $pluralParam, $param.'_id');
  123. }
  124. $uri = str_replace($search, $replace, $uri);
  125. }
  126. }
  127. return $uri;
  128. }
  129. /**
  130. * Prepare the endpoint data for serialising.
  131. */
  132. public function forSerialisation()
  133. {
  134. $this->metadata = $this->metadata->except('groupName', 'groupDescription');
  135. return $this->except(
  136. // Get rid of all duplicate data
  137. 'cleanQueryParameters', 'cleanUrlParameters', 'fileParameters', 'cleanBodyParameters',
  138. // and objects used only in extraction
  139. 'route', 'controller', 'method', 'auth',
  140. );
  141. }
  142. }