You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
3.8 KiB

3 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author David Prévot <taffit@debian.org>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Robin McCorkell <robin@mccorkell.me.uk>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author Thomas Tanghus <thomas@tanghus.net>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OC\Route;
  30. use OCP\Route\IRoute;
  31. use Symfony\Component\Routing\Route as SymfonyRoute;
  32. class Route extends SymfonyRoute implements IRoute {
  33. /**
  34. * Specify the method when this route is to be used
  35. *
  36. * @param string $method HTTP method (uppercase)
  37. * @return \OC\Route\Route
  38. */
  39. public function method($method) {
  40. $this->setMethods($method);
  41. return $this;
  42. }
  43. /**
  44. * Specify POST as the method to use with this route
  45. * @return \OC\Route\Route
  46. */
  47. public function post() {
  48. $this->method('POST');
  49. return $this;
  50. }
  51. /**
  52. * Specify GET as the method to use with this route
  53. * @return \OC\Route\Route
  54. */
  55. public function get() {
  56. $this->method('GET');
  57. return $this;
  58. }
  59. /**
  60. * Specify PUT as the method to use with this route
  61. * @return \OC\Route\Route
  62. */
  63. public function put() {
  64. $this->method('PUT');
  65. return $this;
  66. }
  67. /**
  68. * Specify DELETE as the method to use with this route
  69. * @return \OC\Route\Route
  70. */
  71. public function delete() {
  72. $this->method('DELETE');
  73. return $this;
  74. }
  75. /**
  76. * Specify PATCH as the method to use with this route
  77. * @return \OC\Route\Route
  78. */
  79. public function patch() {
  80. $this->method('PATCH');
  81. return $this;
  82. }
  83. /**
  84. * Defaults to use for this route
  85. *
  86. * @param array $defaults The defaults
  87. * @return \OC\Route\Route
  88. */
  89. public function defaults($defaults) {
  90. $action = $this->getDefault('action');
  91. $this->setDefaults($defaults);
  92. if (isset($defaults['action'])) {
  93. $action = $defaults['action'];
  94. }
  95. $this->action($action);
  96. return $this;
  97. }
  98. /**
  99. * Requirements for this route
  100. *
  101. * @param array $requirements The requirements
  102. * @return \OC\Route\Route
  103. */
  104. public function requirements($requirements) {
  105. $method = $this->getMethods();
  106. $this->setRequirements($requirements);
  107. if (isset($requirements['_method'])) {
  108. $method = $requirements['_method'];
  109. }
  110. if ($method) {
  111. $this->method($method);
  112. }
  113. return $this;
  114. }
  115. /**
  116. * The action to execute when this route matches
  117. *
  118. * @param string|callable $class the class or a callable
  119. * @param string $function the function to use with the class
  120. * @return \OC\Route\Route
  121. *
  122. * This function is called with $class set to a callable or
  123. * to the class with $function
  124. */
  125. public function action($class, $function = null) {
  126. $action = [$class, $function];
  127. if (is_null($function)) {
  128. $action = $class;
  129. }
  130. $this->setDefault('action', $action);
  131. return $this;
  132. }
  133. /**
  134. * The action to execute when this route matches, includes a file like
  135. * it is called directly
  136. * @param string $file
  137. * @return void
  138. */
  139. public function actionInclude($file) {
  140. $function = function ($param) use ($file) {
  141. unset($param["_route"]);
  142. $_GET=array_merge($_GET, $param);
  143. unset($param);
  144. require_once "$file";
  145. } ;
  146. $this->action($function);
  147. }
  148. }