ExtractedEndpointData.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace Knuckles\Camel\Extraction;
  3. use Illuminate\Routing\Route;
  4. use Knuckles\Camel\BaseDTO;
  5. use Knuckles\Scribe\Extracting\Shared\UrlParamsNormalizer;
  6. use Knuckles\Scribe\Tools\Globals;
  7. use Knuckles\Scribe\Tools\Utils as u;
  8. use ReflectionClass;
  9. class ExtractedEndpointData extends BaseDTO
  10. {
  11. /**
  12. * @var array<string>
  13. */
  14. public array $httpMethods;
  15. public string $uri;
  16. public Metadata $metadata;
  17. /**
  18. * @var array<string,string>
  19. */
  20. public array $headers = [];
  21. /**
  22. * @var array<string,\Knuckles\Camel\Extraction\Parameter>
  23. */
  24. public array $urlParameters = [];
  25. /**
  26. * @var array<string,mixed>
  27. */
  28. public array $cleanUrlParameters = [];
  29. /**
  30. * @var array<string,\Knuckles\Camel\Extraction\Parameter>
  31. */
  32. public array $queryParameters = [];
  33. /**
  34. * @var array<string,mixed>
  35. */
  36. public array $cleanQueryParameters = [];
  37. /**
  38. * @var array<string,\Knuckles\Camel\Extraction\Parameter>
  39. */
  40. public array $bodyParameters = [];
  41. /**
  42. * @var array<string,mixed>
  43. */
  44. public array $cleanBodyParameters = [];
  45. /**
  46. * @var array<string,\Illuminate\Http\UploadedFile|array>
  47. */
  48. public array $fileParameters = [];
  49. public ResponseCollection $responses;
  50. /**
  51. * @var array<string,\Knuckles\Camel\Extraction\ResponseField>
  52. */
  53. public array $responseFields = [];
  54. /**
  55. * Authentication info for this endpoint. In the form [{where}, {name}, {sample}]
  56. * Example: ["queryParameters", "api_key", "njiuyiw97865rfyvgfvb1"]
  57. */
  58. public array $auth = [];
  59. public ?ReflectionClass $controller;
  60. public ?\ReflectionFunctionAbstract $method;
  61. public ?Route $route;
  62. public function __construct(array $parameters = [])
  63. {
  64. $parameters['metadata'] = $parameters['metadata'] ?? new Metadata([]);
  65. $parameters['responses'] = $parameters['responses'] ?? new ResponseCollection([]);
  66. parent::__construct($parameters);
  67. $defaultNormalizer = fn() => UrlParamsNormalizer::normalizeParameterNamesInRouteUri($this->route, $this->method);
  68. $this->uri = match (is_callable(Globals::$__normalizeEndpointUrlUsing)) {
  69. true => call_user_func_array(Globals::$__normalizeEndpointUrlUsing,
  70. [$this->route->uri, $this->route, $this->method, $this->controller, $defaultNormalizer]),
  71. default => $defaultNormalizer(),
  72. };
  73. }
  74. /**
  75. * @param Route $route
  76. * @param array $extras Only used for quick overrides in tests
  77. * @return self
  78. * @throws \ReflectionException
  79. */
  80. public static function fromRoute(Route $route, array $extras = []): self
  81. {
  82. $httpMethods = self::getMethods($route);
  83. $uri = $route->uri();
  84. [$controllerName, $methodName] = u::getRouteClassAndMethodNames($route);
  85. $controller = new ReflectionClass($controllerName);
  86. $method = u::getReflectedRouteMethod([$controllerName, $methodName]);
  87. $data = compact('httpMethods', 'uri', 'controller', 'method', 'route');
  88. $data = array_merge($data, $extras);
  89. return new ExtractedEndpointData($data);
  90. }
  91. /**
  92. * @param Route $route
  93. *
  94. * @return array<string>
  95. */
  96. public static function getMethods(Route $route): array
  97. {
  98. $methods = $route->methods();
  99. // Laravel adds an automatic "HEAD" endpoint for each GET request, so we'll strip that out,
  100. // but not if there's only one method (means it was intentional)
  101. if (count($methods) === 1) {
  102. return $methods;
  103. }
  104. return array_diff($methods, ['HEAD']);
  105. }
  106. public function name()
  107. {
  108. return sprintf("[%s] {$this->route->uri}.", implode(',', $this->route->methods));
  109. }
  110. public function endpointId()
  111. {
  112. return $this->httpMethods[0] . str_replace(['/', '?', '{', '}', ':', '\\', '+', '|'], '-', $this->uri);
  113. }
  114. /**
  115. * Prepare the endpoint data for serialising.
  116. */
  117. public function forSerialisation()
  118. {
  119. $copy = $this->except(
  120. // Get rid of all duplicate data
  121. 'cleanQueryParameters', 'cleanUrlParameters', 'fileParameters', 'cleanBodyParameters',
  122. // and objects used only in extraction
  123. 'route', 'controller', 'method', 'auth',
  124. );
  125. // Remove these, since they're on the parent group object
  126. $copy->metadata = $copy->metadata->except('groupName', 'groupDescription');
  127. return $copy;
  128. }
  129. }