Static.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. * @category Zend
  16. * @package Zend_Controller
  17. * @subpackage Router
  18. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @version $Id$
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. */
  22. /** Zend_Controller_Router_Route_Abstract */
  23. require_once 'Zend/Controller/Router/Route/Abstract.php';
  24. /**
  25. * StaticRoute is used for managing static URIs.
  26. *
  27. * It's a lot faster compared to the standard Route implementation.
  28. *
  29. * @package Zend_Controller
  30. * @subpackage Router
  31. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. class Zend_Controller_Router_Route_Static extends Zend_Controller_Router_Route_Abstract
  35. {
  36. /**
  37. * Route
  38. *
  39. * @var string|null
  40. */
  41. protected $_route = null;
  42. /**
  43. * Default values for the route (ie. module, controller, action, params)
  44. *
  45. * @var array
  46. */
  47. protected $_defaults = array();
  48. /**
  49. * Get the version of the route
  50. *
  51. * @return int
  52. */
  53. public function getVersion() {
  54. return 1;
  55. }
  56. /**
  57. * Instantiates route based on passed Zend_Config structure
  58. *
  59. * @param Zend_Config $config Configuration object
  60. * @return Zend_Controller_Router_Route_Static
  61. */
  62. public static function getInstance(Zend_Config $config)
  63. {
  64. $defs = ($config->defaults instanceof Zend_Config) ? $config->defaults->toArray() : array();
  65. return new self($config->route, $defs);
  66. }
  67. /**
  68. * Prepares the route for mapping.
  69. *
  70. * @param string $route Map used to match with later submitted URL path
  71. * @param array $defaults Defaults for map variables with keys as variable names
  72. */
  73. public function __construct($route, $defaults = array())
  74. {
  75. $this->_route = trim($route, self::URI_DELIMITER);
  76. $this->_defaults = (array) $defaults;
  77. }
  78. /**
  79. * Matches a user submitted path with a previously defined route.
  80. * Assigns and returns an array of defaults on a successful match.
  81. *
  82. * @param string $path Path used to match against this routing map
  83. * @return array|false An array of assigned values or a false on a mismatch
  84. */
  85. public function match($path, $partial = false)
  86. {
  87. if ($partial) {
  88. if ((empty($path) && empty($this->_route))
  89. || (substr($path, 0, strlen($this->_route)) === $this->_route)
  90. ) {
  91. $this->setMatchedPath($this->_route);
  92. return $this->_defaults;
  93. }
  94. } else {
  95. if (trim($path, self::URI_DELIMITER) == $this->_route) {
  96. return $this->_defaults;
  97. }
  98. }
  99. return false;
  100. }
  101. /**
  102. * Assembles a URL path defined by this route
  103. *
  104. * @param array $data An array of variable and value pairs used as parameters
  105. * @return string Route path with user submitted parameters
  106. */
  107. public function assemble($data = array(), $reset = false, $encode = false, $partial = false)
  108. {
  109. return $this->_route;
  110. }
  111. /**
  112. * Return a single parameter of route's defaults
  113. *
  114. * @param string $name Array key of the parameter
  115. * @return string Previously set default
  116. */
  117. public function getDefault($name) {
  118. if (isset($this->_defaults[$name])) {
  119. return $this->_defaults[$name];
  120. }
  121. return null;
  122. }
  123. /**
  124. * Return an array of defaults
  125. *
  126. * @return array Route defaults
  127. */
  128. public function getDefaults() {
  129. return $this->_defaults;
  130. }
  131. }