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.

route.php 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. * Defaults to use for this route
  49. *
  50. * @param array $defaults The defaults
  51. */
  52. public function defaults($defaults) {
  53. $action = $this->getDefault('action');
  54. $this->setDefaults($defaults);
  55. if (isset($defaults['action'])) {
  56. $action = $defaults['action'];
  57. }
  58. $this->action($action);
  59. return $this;
  60. }
  61. /**
  62. * Requirements for this route
  63. *
  64. * @param array $requirements The requirements
  65. */
  66. public function requirements($requirements) {
  67. $method = $this->getRequirement('_method');
  68. $this->setRequirements($requirements);
  69. if (isset($requirements['_method'])) {
  70. $method = $requirements['_method'];
  71. }
  72. if ($method) {
  73. $this->method($method);
  74. }
  75. return $this;
  76. }
  77. /**
  78. * The action to execute when this route matches
  79. * @param string|callable $class the class or a callable
  80. * @param string $function the function to use with the class
  81. *
  82. * This function is called with $class set to a callable or
  83. * to the class with $function
  84. */
  85. public function action($class, $function = null) {
  86. $action = array($class, $function);
  87. if (is_null($function)) {
  88. $action = $class;
  89. }
  90. $this->setDefault('action', $action);
  91. return $this;
  92. }
  93. /**
  94. * The action to execute when this route matches, includes a file like
  95. * it is called directly
  96. * @param $file
  97. */
  98. public function actionInclude($file) {
  99. $function = create_function('$param',
  100. 'unset($param["_route"]);'
  101. .'$_GET=array_merge($_GET, $param);'
  102. .'unset($param);'
  103. .'require_once "'.$file.'";');
  104. $this->action($function);
  105. }
  106. }