route.php 3.0 KB

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