route.php 3.8 KB

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