Static.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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-2015 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-2015 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. {
  55. return 1;
  56. }
  57. /**
  58. * Instantiates route based on passed Zend_Config structure
  59. *
  60. * @param Zend_Config $config Configuration object
  61. * @return Zend_Controller_Router_Route_Static
  62. */
  63. public static function getInstance(Zend_Config $config)
  64. {
  65. $defs = ($config->defaults instanceof Zend_Config) ? $config->defaults->toArray() : array();
  66. return new self($config->route, $defs);
  67. }
  68. /**
  69. * Prepares the route for mapping.
  70. *
  71. * @param string $route Map used to match with later submitted URL path
  72. * @param array $defaults Defaults for map variables with keys as variable names
  73. */
  74. public function __construct($route, $defaults = array())
  75. {
  76. $this->_route = trim($route, self::URI_DELIMITER);
  77. $this->_defaults = (array) $defaults;
  78. }
  79. /**
  80. * Matches a user submitted path with a previously defined route.
  81. * Assigns and returns an array of defaults on a successful match.
  82. *
  83. * @param string $path Path used to match against this routing map
  84. * @return array|false An array of assigned values or a false on a mismatch
  85. */
  86. public function match($path, $partial = false)
  87. {
  88. if ($partial) {
  89. if ((empty($path) && empty($this->_route))
  90. || (substr($path, 0, strlen($this->_route)) === $this->_route)
  91. ) {
  92. $this->setMatchedPath($this->_route);
  93. return $this->_defaults;
  94. }
  95. } else {
  96. if (trim($path, self::URI_DELIMITER) == $this->_route) {
  97. return $this->_defaults;
  98. }
  99. }
  100. return false;
  101. }
  102. /**
  103. * Assembles a URL path defined by this route
  104. *
  105. * @param array $data An array of variable and value pairs used as parameters
  106. * @return string Route path with user submitted parameters
  107. */
  108. public function assemble($data = array(), $reset = false, $encode = false, $partial = false)
  109. {
  110. return $this->_route;
  111. }
  112. /**
  113. * Return a single parameter of route's defaults
  114. *
  115. * @param string $name Array key of the parameter
  116. * @return string Previously set default
  117. */
  118. public function getDefault($name)
  119. {
  120. if (isset($this->_defaults[$name])) {
  121. return $this->_defaults[$name];
  122. }
  123. return null;
  124. }
  125. /**
  126. * Return an array of defaults
  127. *
  128. * @return array Route defaults
  129. */
  130. public function getDefaults()
  131. {
  132. return $this->_defaults;
  133. }
  134. }