ExtractedEndpointData.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace Knuckles\Camel\Extraction;
  3. use Illuminate\Routing\Route;
  4. use Knuckles\Camel\BaseDTO;
  5. use Knuckles\Scribe\Tools\Utils as u;
  6. use ReflectionClass;
  7. use ReflectionFunctionAbstract;
  8. class ExtractedEndpointData extends BaseDTO
  9. {
  10. /**
  11. * @var array<string>
  12. */
  13. public array $methods;
  14. public string $uri;
  15. public Metadata $metadata;
  16. /**
  17. * @var array<string,string>
  18. */
  19. public array $headers = [];
  20. /**
  21. * @var array
  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
  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. public array $bodyParameters = [];
  39. /**
  40. * @var array<string,mixed>
  41. */
  42. public array $cleanBodyParameters = [];
  43. /**
  44. * @var array<string,\Illuminate\Http\UploadedFile|array>
  45. */
  46. public array $fileParameters = [];
  47. /**
  48. * @var ResponseCollection|array
  49. */
  50. public $responses;
  51. /**
  52. * @var array
  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['metadata'] = $parameters['metadata'] ?? new Metadata([]);
  67. $parameters['responses'] = $parameters['responses'] ?? new ResponseCollection([]);
  68. parent::__construct($parameters);
  69. }
  70. public static function fromRoute(Route $route, array $extras = []): self
  71. {
  72. // $this->id = md5($this->getUri($route) . ':' . implode($this->getMethods($route))),
  73. $methods = 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('methods', '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->methods[0] . str_replace(['/', '?', '{', '}', ':'], '-', $this->uri);
  104. }
  105. /**
  106. * Prepare the endpoint data for serialising.
  107. */
  108. public function forSerialisation()
  109. {
  110. $this->metadata = $this->metadata->except('groupName', 'groupDescription');
  111. return $this->except(
  112. // Get rid of all duplicate data
  113. 'cleanQueryParameters', 'cleanUrlParameters', 'fileParameters', 'cleanBodyParameters',
  114. // and objects used only in extraction
  115. 'route', 'controller', 'method', 'auth',
  116. );
  117. }
  118. }