Static.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @package Zend_Controller
  16. * @subpackage Router
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @version $Id: Route.php 1847 2006-11-23 11:36:41Z martel $
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /** Zend_Controller_Router_Route_Abstract */
  22. require_once 'Zend/Controller/Router/Route/Abstract.php';
  23. /**
  24. * StaticRoute is used for managing static URIs.
  25. *
  26. * It's a lot faster compared to the standard Route implementation.
  27. *
  28. * @package Zend_Controller
  29. * @subpackage Router
  30. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Controller_Router_Route_Static extends Zend_Controller_Router_Route_Abstract
  34. {
  35. protected $_route = null;
  36. protected $_defaults = array();
  37. public function getVersion() {
  38. return 1;
  39. }
  40. /**
  41. * Instantiates route based on passed Zend_Config structure
  42. *
  43. * @param Zend_Config $config Configuration object
  44. */
  45. public static function getInstance(Zend_Config $config)
  46. {
  47. $defs = ($config->defaults instanceof Zend_Config) ? $config->defaults->toArray() : array();
  48. return new self($config->route, $defs);
  49. }
  50. /**
  51. * Prepares the route for mapping.
  52. *
  53. * @param string $route Map used to match with later submitted URL path
  54. * @param array $defaults Defaults for map variables with keys as variable names
  55. */
  56. public function __construct($route, $defaults = array())
  57. {
  58. $this->_route = trim($route, '/');
  59. $this->_defaults = (array) $defaults;
  60. }
  61. /**
  62. * Matches a user submitted path with a previously defined route.
  63. * Assigns and returns an array of defaults on a successful match.
  64. *
  65. * @param string $path Path used to match against this routing map
  66. * @return array|false An array of assigned values or a false on a mismatch
  67. */
  68. public function match($path, $partial = false)
  69. {
  70. if ($partial) {
  71. if (substr($path, 0, strlen($this->_route)) === $this->_route) {
  72. $this->setMatchedPath($this->_route);
  73. return $this->_defaults;
  74. }
  75. } else {
  76. if (trim($path, '/') == $this->_route) {
  77. return $this->_defaults;
  78. }
  79. }
  80. return false;
  81. }
  82. /**
  83. * Assembles a URL path defined by this route
  84. *
  85. * @param array $data An array of variable and value pairs used as parameters
  86. * @return string Route path with user submitted parameters
  87. */
  88. public function assemble($data = array(), $reset = false, $encode = false, $partial = false)
  89. {
  90. return $this->_route;
  91. }
  92. /**
  93. * Return a single parameter of route's defaults
  94. *
  95. * @param string $name Array key of the parameter
  96. * @return string Previously set default
  97. */
  98. public function getDefault($name) {
  99. if (isset($this->_defaults[$name])) {
  100. return $this->_defaults[$name];
  101. }
  102. return null;
  103. }
  104. /**
  105. * Return an array of defaults
  106. *
  107. * @return array Route defaults
  108. */
  109. public function getDefaults() {
  110. return $this->_defaults;
  111. }
  112. }