Rewrite.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ g7GTI4QmKgRt7X9nB6RlXOGrwAd9ek35ZXUkjipREEA=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2017 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.9 [rev.1.9.01]
  11. */
  12. /**
  13. * route with mod rewrite object
  14. */
  15. namespace Cube\Controller\Router\Route;
  16. class Rewrite extends AbstractRoute
  17. {
  18. /**
  19. *
  20. * used by assemble method
  21. *
  22. * @var array
  23. */
  24. private $_vars;
  25. /**
  26. *
  27. * array of assembled urls
  28. *
  29. * @var array
  30. */
  31. protected $_assembled = array();
  32. /**
  33. *
  34. * get an array of params and if the route matches, return a routed url
  35. * the method will also route requests if all params in the route match the params in
  36. * the request, but there are extra params in the request
  37. *
  38. * @param array $params
  39. * @param bool $named if the flag is set to true, we need to match by params only
  40. *
  41. * @return string|null the assembled url or null if the route doesnt match
  42. */
  43. public function assemble($params, $named = false)
  44. {
  45. $k = json_encode($params, $named);
  46. if (array_key_exists($k, $this->_assembled)) {
  47. return $this->_assembled[$k];
  48. }
  49. if (!is_array($params)) {
  50. $params = (array)$params;
  51. }
  52. $routeParams = $this->getParams();
  53. if ($named === false) {
  54. $routeParams['module'] = $this->normalize(
  55. $this->getModule(), true);
  56. foreach ($this->_defaults as $key => $value) {
  57. $routeParams[(string)$key] = (string)$value;
  58. }
  59. }
  60. $this->_vars = $params;
  61. if (array_key_exists('module', $params)) {
  62. if (strcmp($routeParams['module'], $params['module']) === 0) {
  63. unset($routeParams['module']);
  64. unset($params['module']);
  65. }
  66. }
  67. foreach ($params as $key => $value) {
  68. if (isset($this->_defaults[$key])) {
  69. if ($this->_defaults[$key] == $value) {
  70. unset($params[$key]);
  71. unset($routeParams[$key]);
  72. }
  73. }
  74. if (isset($this->_conditions[$key]) && is_string($value)) {
  75. if (preg_match('#(' . $this->_conditions[$key] . ')#', $value)) {
  76. unset($params[$key]);
  77. unset($routeParams[$key]);
  78. }
  79. }
  80. if (array_key_exists($key, $this->_params)) {
  81. unset($routeParams[$key]);
  82. }
  83. }
  84. if (empty($routeParams)) {
  85. $url = array();
  86. $get = array();
  87. foreach ((array)$params as $key => $value) {
  88. if (preg_match('#^[a-zA-Z0-9_-]+$#', $key)) {
  89. if (!is_array($value)) {
  90. $get[] = $key . '=' . $value;
  91. }
  92. else {
  93. foreach ((array)$value as $val) {
  94. if (!empty($val)) {
  95. $get[] = $key . '[]=' . $val;
  96. }
  97. }
  98. }
  99. }
  100. }
  101. $uri = @implode(self::URI_DELIMITER, $url);
  102. if (count($get) > 0) {
  103. $uri .= '?' . implode('&amp;', $get);
  104. }
  105. $this->_assembled[$k] = rtrim(preg_replace_callback(self::DEFAULT_MATCH, array($this, '_defaultMatchCallback'), $this->_route)
  106. . self::URI_DELIMITER
  107. . $uri, self::URI_DELIMITER);
  108. }
  109. else {
  110. $this->_assembled[$k] = null;
  111. }
  112. return $this->_assembled[$k];
  113. }
  114. /**
  115. *
  116. * used to replace parameters for default route
  117. *
  118. * @param array $matches
  119. *
  120. * @return string
  121. */
  122. private function _defaultMatchCallback($matches)
  123. {
  124. return $this->_vars[$matches[1]];
  125. }
  126. }