Route.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCP\AppFramework\Http\Attribute;
  8. use Attribute;
  9. /**
  10. * This attribute can be used to define routes on controller methods.
  11. *
  12. * It works in addition to the traditional routes.php method and has the same parameters
  13. * (except for the `name` parameter which is not needed).
  14. *
  15. * @since 29.0.0
  16. */
  17. #[Attribute(Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
  18. class Route {
  19. /**
  20. * Corresponds to the `ocs` key in routes.php
  21. *
  22. * @see ApiRoute
  23. * @since 29.0.0
  24. */
  25. public const TYPE_API = 'ocs';
  26. /**
  27. * Corresponds to the `routes` key in routes.php
  28. *
  29. * @see FrontpageRoute
  30. * @since 29.0.0
  31. */
  32. public const TYPE_FRONTPAGE = 'routes';
  33. /**
  34. * @param string $type Either Route::TYPE_API or Route::TYPE_FRONTPAGE.
  35. * @psalm-param Route::TYPE_* $type
  36. * @param string $verb HTTP method of the route.
  37. * @psalm-param 'GET'|'HEAD'|'POST'|'PUT'|'DELETE'|'OPTIONS'|'PATCH' $verb
  38. * @param string $url The path of the route.
  39. * @param ?array<string, string> $requirements Array of regexes mapped to the path parameters.
  40. * @param ?array<string, mixed> $defaults Array of default values mapped to the path parameters.
  41. * @param ?string $root Custom root. For OCS all apps are allowed, but for index.php only some can use it.
  42. * @param ?string $postfix Postfix for the route name.
  43. * @since 29.0.0
  44. */
  45. public function __construct(
  46. protected string $type,
  47. protected string $verb,
  48. protected string $url,
  49. protected ?array $requirements = null,
  50. protected ?array $defaults = null,
  51. protected ?string $root = null,
  52. protected ?string $postfix = null,
  53. ) {
  54. }
  55. /**
  56. * @return array{
  57. * verb: string,
  58. * url: string,
  59. * requirements?: array<string, string>,
  60. * defaults?: array<string, mixed>,
  61. * root?: string,
  62. * postfix?: string,
  63. * }
  64. * @since 29.0.0
  65. */
  66. public function toArray() {
  67. $route = [
  68. 'verb' => $this->verb,
  69. 'url' => $this->url,
  70. ];
  71. if ($this->requirements !== null) {
  72. $route['requirements'] = $this->requirements;
  73. }
  74. if ($this->defaults !== null) {
  75. $route['defaults'] = $this->defaults;
  76. }
  77. if ($this->root !== null) {
  78. $route['root'] = $this->root;
  79. }
  80. if ($this->postfix !== null) {
  81. $route['postfix'] = $this->postfix;
  82. }
  83. return $route;
  84. }
  85. /**
  86. * @since 29.0.0
  87. */
  88. public function getType(): string {
  89. return $this->type;
  90. }
  91. /**
  92. * @since 29.0.0
  93. */
  94. public function getVerb(): string {
  95. return $this->verb;
  96. }
  97. /**
  98. * @since 29.0.0
  99. */
  100. public function getUrl(): string {
  101. return $this->url;
  102. }
  103. /**
  104. * @since 29.0.0
  105. */
  106. public function getRequirements(): ?array {
  107. return $this->requirements;
  108. }
  109. /**
  110. * @since 29.0.0
  111. */
  112. public function getDefaults(): ?array {
  113. return $this->defaults;
  114. }
  115. /**
  116. * @since 29.0.0
  117. */
  118. public function getRoot(): ?string {
  119. return $this->root;
  120. }
  121. /**
  122. * @since 29.0.0
  123. */
  124. public function getPostfix(): ?string {
  125. return $this->postfix;
  126. }
  127. }