ExtractedEndpointData.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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\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. public ResponseCollection $responses;
  49. /**
  50. * @var array<string,\Knuckles\Camel\Extraction\ResponseField>
  51. */
  52. public array $responseFields = [];
  53. /**
  54. * Authentication info for this endpoint. In the form [{where}, {name}, {sample}]
  55. * Example: ["queryParameters", "api_key", "njiuyiw97865rfyvgfvb1"]
  56. */
  57. public array $auth = [];
  58. public ?ReflectionClass $controller;
  59. public ?\ReflectionFunctionAbstract $method;
  60. public ?Route $route;
  61. public function __construct(array $parameters = [])
  62. {
  63. $parameters['metadata'] = $parameters['metadata'] ?? new Metadata([]);
  64. $parameters['responses'] = $parameters['responses'] ?? new ResponseCollection([]);
  65. parent::__construct($parameters);
  66. $this->uri = UrlParamsNormalizer::normalizeParameterNamesInRouteUri($this->route, $this->method);
  67. }
  68. public static function fromRoute(Route $route, array $extras = []): self
  69. {
  70. $httpMethods = self::getMethods($route);
  71. $uri = $route->uri();
  72. [$controllerName, $methodName] = u::getRouteClassAndMethodNames($route);
  73. $controller = new ReflectionClass($controllerName);
  74. $method = u::getReflectedRouteMethod([$controllerName, $methodName]);
  75. $data = compact('httpMethods', 'uri', 'controller', 'method', 'route');
  76. $data = array_merge($data, $extras);
  77. return new ExtractedEndpointData($data);
  78. }
  79. /**
  80. * @param Route $route
  81. *
  82. * @return array<string>
  83. */
  84. public static function getMethods(Route $route): array
  85. {
  86. $methods = $route->methods();
  87. // Laravel adds an automatic "HEAD" endpoint for each GET request, so we'll strip that out,
  88. // but not if there's only one method (means it was intentional)
  89. if (count($methods) === 1) {
  90. return $methods;
  91. }
  92. return array_diff($methods, ['HEAD']);
  93. }
  94. public function name()
  95. {
  96. return sprintf("[%s] {$this->route->uri}.", implode(',', $this->route->methods));
  97. }
  98. public function endpointId()
  99. {
  100. return $this->httpMethods[0] . str_replace(['/', '?', '{', '}', ':', '\\', '+', '|'], '-', $this->uri);
  101. }
  102. /**
  103. * Prepare the endpoint data for serialising.
  104. */
  105. public function forSerialisation()
  106. {
  107. $copy = $this->except(
  108. // Get rid of all duplicate data
  109. 'cleanQueryParameters', 'cleanUrlParameters', 'fileParameters', 'cleanBodyParameters',
  110. // and objects used only in extraction
  111. 'route', 'controller', 'method', 'auth',
  112. );
  113. // Remove these, since they're on the parent group object
  114. $copy->metadata = $copy->metadata->except('groupName', 'groupDescription');
  115. return $copy;
  116. }
  117. }