ExtractedEndpointData.php 4.0 KB

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