route.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. use Symfony\Component\Routing\Route;
  9. class OC_Route extends Route {
  10. /**
  11. * Specify the method when this route is to be used
  12. *
  13. * @param string $method HTTP method (uppercase)
  14. */
  15. public function method($method) {
  16. $this->setRequirement('_method', strtoupper($method));
  17. return $this;
  18. }
  19. /**
  20. * Specify POST as the method to use with this route
  21. */
  22. public function post() {
  23. $this->method('POST');
  24. return $this;
  25. }
  26. /**
  27. * Specify GET as the method to use with this route
  28. */
  29. public function get() {
  30. $this->method('GET');
  31. return $this;
  32. }
  33. /**
  34. * Specify PUT as the method to use with this route
  35. */
  36. public function put() {
  37. $this->method('PUT');
  38. return $this;
  39. }
  40. /**
  41. * Specify DELETE as the method to use with this route
  42. */
  43. public function delete() {
  44. $this->method('DELETE');
  45. return $this;
  46. }
  47. /**
  48. * Specify PATCH as the method to use with this route
  49. */
  50. public function patch() {
  51. $this->method('PATCH');
  52. return $this;
  53. }
  54. /**
  55. * Defaults to use for this route
  56. *
  57. * @param array $defaults The defaults
  58. */
  59. public function defaults($defaults) {
  60. $action = $this->getDefault('action');
  61. $this->setDefaults($defaults);
  62. if (isset($defaults['action'])) {
  63. $action = $defaults['action'];
  64. }
  65. $this->action($action);
  66. return $this;
  67. }
  68. /**
  69. * Requirements for this route
  70. *
  71. * @param array $requirements The requirements
  72. */
  73. public function requirements($requirements) {
  74. $method = $this->getRequirement('_method');
  75. $this->setRequirements($requirements);
  76. if (isset($requirements['_method'])) {
  77. $method = $requirements['_method'];
  78. }
  79. if ($method) {
  80. $this->method($method);
  81. }
  82. return $this;
  83. }
  84. /**
  85. * The action to execute when this route matches
  86. * @param string|callable $class the class or a callable
  87. * @param string $function the function to use with the class
  88. *
  89. * This function is called with $class set to a callable or
  90. * to the class with $function
  91. */
  92. public function action($class, $function = null) {
  93. $action = array($class, $function);
  94. if (is_null($function)) {
  95. $action = $class;
  96. }
  97. $this->setDefault('action', $action);
  98. return $this;
  99. }
  100. /**
  101. * The action to execute when this route matches, includes a file like
  102. * it is called directly
  103. * @param $file
  104. */
  105. public function actionInclude($file) {
  106. $function = create_function('$param',
  107. 'unset($param["_route"]);'
  108. .'$_GET=array_merge($_GET, $param);'
  109. .'unset($param);'
  110. .'require_once "'.$file.'";');
  111. $this->action($function);
  112. }
  113. }