ExtractedEndpointData.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. $this->uri = match (is_callable(Globals::$__normalizeEndpointUrlUsing)) {
  68. true => call_user_func_array(Globals::$__normalizeEndpointUrlUsing,
  69. [$this->route->uri, $this->route, $this->method, $this->controller]),
  70. default => UrlParamsNormalizer::normalizeParameterNamesInRouteUri($this->route, $this->method),
  71. };
  72. }
  73. public static function fromRoute(Route $route, array $extras = []): self
  74. {
  75. $httpMethods = self::getMethods($route);
  76. $uri = $route->uri();
  77. [$controllerName, $methodName] = u::getRouteClassAndMethodNames($route);
  78. $controller = new ReflectionClass($controllerName);
  79. $method = u::getReflectedRouteMethod([$controllerName, $methodName]);
  80. $data = compact('httpMethods', 'uri', 'controller', 'method', 'route');
  81. $data = array_merge($data, $extras);
  82. return new ExtractedEndpointData($data);
  83. }
  84. /**
  85. * @param Route $route
  86. *
  87. * @return array<string>
  88. */
  89. public static function getMethods(Route $route): array
  90. {
  91. $methods = $route->methods();
  92. // Laravel adds an automatic "HEAD" endpoint for each GET request, so we'll strip that out,
  93. // but not if there's only one method (means it was intentional)
  94. if (count($methods) === 1) {
  95. return $methods;
  96. }
  97. return array_diff($methods, ['HEAD']);
  98. }
  99. public function name()
  100. {
  101. return sprintf("[%s] {$this->route->uri}.", implode(',', $this->route->methods));
  102. }
  103. public function endpointId()
  104. {
  105. return $this->httpMethods[0] . str_replace(['/', '?', '{', '}', ':', '\\', '+', '|'], '-', $this->uri);
  106. }
  107. /**
  108. * Prepare the endpoint data for serialising.
  109. */
  110. public function forSerialisation()
  111. {
  112. $copy = $this->except(
  113. // Get rid of all duplicate data
  114. 'cleanQueryParameters', 'cleanUrlParameters', 'fileParameters', 'cleanBodyParameters',
  115. // and objects used only in extraction
  116. 'route', 'controller', 'method', 'auth',
  117. );
  118. // Remove these, since they're on the parent group object
  119. $copy->metadata = $copy->metadata->except('groupName', 'groupDescription');
  120. return $copy;
  121. }
  122. }