ExtractedEndpointData.php 4.9 KB

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