sfRequestRoute.class.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * sfRequestRoute represents a route that is request aware.
  11. *
  12. * It implements the sf_method requirement.
  13. *
  14. * @package symfony
  15. * @subpackage routing
  16. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  17. * @version SVN: $Id: sfRequestRoute.class.php 11794 2008-09-26 09:05:48Z fabien $
  18. */
  19. class sfRequestRoute extends sfRoute
  20. {
  21. /**
  22. * Returns true if the URL matches this route, false otherwise.
  23. *
  24. * @param string $url The URL
  25. * @param array $context The context
  26. *
  27. * @return array An array of parameters
  28. */
  29. public function matchesUrl($url, $context = array())
  30. {
  31. if (false === $parameters = parent::matchesUrl($url, $context))
  32. {
  33. return false;
  34. }
  35. if (!isset($this->requirements['sf_method']))
  36. {
  37. $this->requirements['sf_method'] = array('get', 'head');
  38. }
  39. // enforce the sf_method requirement
  40. $methods = is_array($this->requirements['sf_method']) ? $this->requirements['sf_method'] : array($this->requirements['sf_method']);
  41. foreach ($methods as $method)
  42. {
  43. if (0 == strcasecmp($method, $context['method']))
  44. {
  45. return $parameters;
  46. }
  47. }
  48. return false;
  49. }
  50. /**
  51. * Returns true if the parameters matches this route, false otherwise.
  52. *
  53. * @param mixed $params The parameters
  54. * @param array $context The context
  55. *
  56. * @return Boolean true if the parameters matches this route, false otherwise.
  57. */
  58. public function matchesParameters($params, $context = array())
  59. {
  60. if (isset($params['sf_method']))
  61. {
  62. if (!isset($this->requirements['sf_method']))
  63. {
  64. $this->requirements['sf_method'] = 'get';
  65. }
  66. // enforce the sf_method requirement
  67. if ($this->requirements['sf_method'] != $params['sf_method'])
  68. {
  69. return false;
  70. }
  71. unset($params['sf_method']);
  72. }
  73. return parent::matchesParameters($params, $context);
  74. }
  75. /**
  76. * Generates a URL from the given parameters.
  77. *
  78. * @param mixed $params The parameter values
  79. * @param array $context The context
  80. * @param Boolean $absolute Whether to generate an absolute URL
  81. *
  82. * @return string The generated URL
  83. */
  84. public function generate($params, $context = array(), $absolute = false)
  85. {
  86. unset($params['sf_method']);
  87. return parent::generate($params, $context, $absolute);
  88. }
  89. }