Route.php 3.8 KB

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