Route.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Route;
  8. use OCP\Route\IRoute;
  9. use Symfony\Component\Routing\Route as SymfonyRoute;
  10. class Route extends SymfonyRoute implements IRoute {
  11. /**
  12. * Specify the method when this route is to be used
  13. *
  14. * @param string $method HTTP method (uppercase)
  15. * @return \OC\Route\Route
  16. */
  17. public function method($method) {
  18. $this->setMethods($method);
  19. return $this;
  20. }
  21. /**
  22. * Specify POST as the method to use with this route
  23. * @return \OC\Route\Route
  24. */
  25. public function post() {
  26. $this->method('POST');
  27. return $this;
  28. }
  29. /**
  30. * Specify GET as the method to use with this route
  31. * @return \OC\Route\Route
  32. */
  33. public function get() {
  34. $this->method('GET');
  35. return $this;
  36. }
  37. /**
  38. * Specify PUT as the method to use with this route
  39. * @return \OC\Route\Route
  40. */
  41. public function put() {
  42. $this->method('PUT');
  43. return $this;
  44. }
  45. /**
  46. * Specify DELETE as the method to use with this route
  47. * @return \OC\Route\Route
  48. */
  49. public function delete() {
  50. $this->method('DELETE');
  51. return $this;
  52. }
  53. /**
  54. * Specify PATCH as the method to use with this route
  55. * @return \OC\Route\Route
  56. */
  57. public function patch() {
  58. $this->method('PATCH');
  59. return $this;
  60. }
  61. /**
  62. * Defaults to use for this route
  63. *
  64. * @param array $defaults The defaults
  65. * @return \OC\Route\Route
  66. */
  67. public function defaults($defaults) {
  68. $action = $this->getDefault('action');
  69. $this->setDefaults($defaults);
  70. if (isset($defaults['action'])) {
  71. $action = $defaults['action'];
  72. }
  73. $this->action($action);
  74. return $this;
  75. }
  76. /**
  77. * Requirements for this route
  78. *
  79. * @param array $requirements The requirements
  80. * @return \OC\Route\Route
  81. */
  82. public function requirements($requirements) {
  83. $method = $this->getMethods();
  84. $this->setRequirements($requirements);
  85. if (isset($requirements['_method'])) {
  86. $method = $requirements['_method'];
  87. }
  88. if ($method) {
  89. $this->method($method);
  90. }
  91. return $this;
  92. }
  93. /**
  94. * The action to execute when this route matches
  95. *
  96. * @param string|callable $class the class or a callable
  97. * @param string $function the function to use with the class
  98. * @return \OC\Route\Route
  99. *
  100. * This function is called with $class set to a callable or
  101. * to the class with $function
  102. */
  103. public function action($class, $function = null) {
  104. $action = [$class, $function];
  105. if (is_null($function)) {
  106. $action = $class;
  107. }
  108. $this->setDefault('action', $action);
  109. return $this;
  110. }
  111. /**
  112. * The action to execute when this route matches, includes a file like
  113. * it is called directly
  114. * @param string $file
  115. * @return void
  116. */
  117. public function actionInclude($file) {
  118. $function = function ($param) use ($file) {
  119. unset($param['_route']);
  120. $_GET = array_merge($_GET, $param);
  121. unset($param);
  122. require_once "$file";
  123. } ;
  124. $this->action($function);
  125. }
  126. }