AbstractRoute.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ FP+UMLZVpEubqb9lJLv41RooXtJePr1EPIDIP8FDxbA=
  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. * abstract route object
  14. */
  15. namespace Cube\Controller\Router\Route;
  16. abstract class AbstractRoute implements RouteInterface
  17. {
  18. const URI_DELIMITER = '/';
  19. const DEFAULT_MATCH = '#:([a-zA-Z0-9_-]+)#';
  20. const DEFAULT_REGEX = '[a-zA-Z0-9_-]+';
  21. /**
  22. *
  23. * true if route has been matched to given request uri or false otherwise
  24. *
  25. * @var bool|null
  26. */
  27. protected $_matched = null;
  28. /**
  29. *
  30. * the route path from the config that will be matched with the provided url
  31. *
  32. * @var string
  33. */
  34. protected $_route;
  35. /**
  36. *
  37. * the name of the route
  38. *
  39. * @var string
  40. */
  41. protected $_name;
  42. /**
  43. *
  44. * the name of the module this route belongs to
  45. *
  46. * @var string
  47. */
  48. protected $_module = null;
  49. /**
  50. *
  51. * default module, controller, action and params for the route created
  52. *
  53. * @var array
  54. */
  55. protected $_defaults = array();
  56. /**
  57. *
  58. * conditions for the params, regular expressions required
  59. *
  60. * @var array
  61. */
  62. protected $_conditions = array();
  63. /**
  64. *
  65. * holds the params of a matched route so that it can be assembled
  66. *
  67. * @var array
  68. */
  69. protected $_params = array();
  70. /**
  71. *
  72. * class constructor, initialize route object
  73. *
  74. * @param string $route
  75. * @param array $defaults
  76. * @param array $conditions
  77. */
  78. public function __construct($route, $defaults = array(), $conditions = array())
  79. {
  80. $this->setDefaults($defaults)
  81. ->setConditions($conditions)
  82. ->setRoute($route);
  83. }
  84. /**
  85. *
  86. * get route path
  87. *
  88. * @return string
  89. */
  90. public function getRoute()
  91. {
  92. return $this->_route;
  93. }
  94. /**
  95. *
  96. * set route path
  97. *
  98. * @param string $route
  99. *
  100. * @return $this
  101. * @throws \InvalidArgumentException
  102. */
  103. public function setRoute($route)
  104. {
  105. if (empty($route) || !is_string($route)) {
  106. throw new \InvalidArgumentException("The route path must be a string.");
  107. }
  108. $this->_route = $route;
  109. return $this;
  110. }
  111. /**
  112. *
  113. * get the name of the module the route belongs to
  114. *
  115. * @throws \OutOfBoundsException
  116. * @return string
  117. */
  118. public function getModule()
  119. {
  120. if (empty($this->_module)) {
  121. throw new \OutOfBoundsException("The module for this route has not been set.");
  122. }
  123. return $this->_module;
  124. }
  125. /**
  126. *
  127. * set the name of the module this route belongs to
  128. *
  129. * @param string $module
  130. *
  131. * @return $this
  132. */
  133. public function setModule($module)
  134. {
  135. $this->_module = (string)$module;
  136. return $this;
  137. }
  138. /**
  139. *
  140. * get the name of the controller from the route
  141. *
  142. * @return string|null
  143. */
  144. public function getController()
  145. {
  146. if (isset($this->_params['controller'])) {
  147. return $this->_params['controller'];
  148. }
  149. else if (isset($this->_defaults['controller'])) {
  150. return $this->_defaults['controller'];
  151. }
  152. return null;
  153. }
  154. /**
  155. *
  156. * get the name of the action from the route
  157. *
  158. * @return string|null
  159. */
  160. public function getAction()
  161. {
  162. if (isset($this->_params['action'])) {
  163. return $this->_params['action'];
  164. }
  165. else if (isset($this->_defaults['action'])) {
  166. return $this->_defaults['action'];
  167. }
  168. return null;
  169. }
  170. /**
  171. *
  172. * get route default parameters
  173. *
  174. * @return array
  175. */
  176. public function getDefaults()
  177. {
  178. return $this->_defaults;
  179. }
  180. /**
  181. *
  182. * set route default parameters
  183. * if there are keys in the defaults array that are different than controller and action,
  184. * then add these as params
  185. *
  186. * @param array $defaults
  187. *
  188. * @return $this
  189. */
  190. public function setDefaults(array $defaults)
  191. {
  192. foreach ($defaults as $key => $value) {
  193. if (!in_array($key, array('controller', 'action'))) {
  194. $this->setParam($key, $value);
  195. }
  196. }
  197. $this->_defaults = $defaults;
  198. return $this;
  199. }
  200. /**
  201. *
  202. * get route parameter conditions (regex)
  203. *
  204. * @return array
  205. */
  206. public function getConditions()
  207. {
  208. return $this->_conditions;
  209. }
  210. /**
  211. *
  212. * set route parameter conditions (regex)
  213. *
  214. * @param array $conditions
  215. *
  216. * @return $this
  217. */
  218. public function setConditions(array $conditions)
  219. {
  220. $this->_conditions = $conditions;
  221. return $this;
  222. }
  223. /**
  224. *
  225. * get route params (generated when matching the route to the request)
  226. *
  227. * @return array
  228. */
  229. public function getParams()
  230. {
  231. return $this->_params;
  232. }
  233. /**
  234. *
  235. * set route params
  236. *
  237. * @param array $params
  238. *
  239. * @return $this
  240. */
  241. public function setParams(array $params = null)
  242. {
  243. if (!empty($this->_params)) {
  244. foreach ($params as $key => $value) {
  245. $this->setParam($key, $value);
  246. }
  247. }
  248. return $this;
  249. }
  250. /**
  251. *
  252. * get a single param from the route object
  253. *
  254. * @param string $name
  255. *
  256. * @return string
  257. * @throws \InvalidArgumentException
  258. */
  259. public function getParam($name)
  260. {
  261. if (!isset($this->_params[$name])) {
  262. throw new \InvalidArgumentException(
  263. sprintf("The parameter '%s' does not exist in the route object.", $name));
  264. }
  265. return $this->_params[$name];
  266. }
  267. /**
  268. *
  269. * set a single param to the route object.
  270. *
  271. * @param string $name
  272. * @param string $value
  273. *
  274. * @return $this
  275. * @throws \InvalidArgumentException
  276. */
  277. public function setParam($name, $value)
  278. {
  279. $this->_params[(string)$name] = (string)$value;
  280. return $this;
  281. }
  282. /**
  283. *
  284. * get the name of the route
  285. *
  286. * @return string
  287. */
  288. public function getName()
  289. {
  290. return $this->_name;
  291. }
  292. /**
  293. *
  294. * set a name for the route object
  295. *
  296. * @param string $name
  297. *
  298. * @return $this
  299. */
  300. public function setName($name)
  301. {
  302. $this->_name = $name;
  303. return $this;
  304. }
  305. /**
  306. *
  307. * match the route to a request uri
  308. *
  309. * @param string $requestUri
  310. *
  311. * @return bool
  312. */
  313. public function match($requestUri)
  314. {
  315. if ($this->_matched !== null) {
  316. return $this->_matched;
  317. }
  318. $this->_params = array();
  319. $params = array();
  320. $values = array();
  321. preg_match_all(self::DEFAULT_MATCH, $this->_route, $params, PREG_PATTERN_ORDER);
  322. $urlRegex = trim(
  323. preg_replace_callback(self::DEFAULT_MATCH, array($this, '_matchCallback'), $this->_route), self::URI_DELIMITER);
  324. $requestUri = trim($requestUri, self::URI_DELIMITER) . self::URI_DELIMITER;
  325. if (!empty($urlRegex) &&
  326. preg_match('#^' . $urlRegex . '(.+)$#', $requestUri, $values)
  327. ) {
  328. $this->_matched = true;
  329. array_shift($values);
  330. }
  331. else {
  332. $this->_matched = false;
  333. }
  334. foreach ((array)$params[1] as $key) {
  335. $this->setParam($key, array_shift($values));
  336. if (!array_key_exists($key, $this->_conditions)) {
  337. $this->_conditions[$key] = self::DEFAULT_REGEX;
  338. }
  339. }
  340. // now we get any params that were matched by the route itself, format and include them
  341. // in the request
  342. $parts = (array)explode(self::URI_DELIMITER, trim(array_shift($values), self::URI_DELIMITER));
  343. while ($param = array_shift($parts)) {
  344. $this->setParam($param, array_shift($parts));
  345. }
  346. return $this->_matched;
  347. }
  348. /**
  349. *
  350. * normalize url parts for modules/controllers/actions in order for them to be parsable by the router
  351. *
  352. * - will first convert the input to lowercase
  353. * - will remove any non alpha-numeric and '-' characters from the value
  354. * - will convert '-' to camel cased and will capitalize the first letter of the string
  355. *
  356. * if reverse is set to true, we convert a router router parsable string into a url string
  357. *
  358. * @param string $input
  359. * @param bool $reverse
  360. *
  361. * @return string
  362. */
  363. public function normalize($input, $reverse = false)
  364. {
  365. if ($reverse) {
  366. return strtolower(
  367. preg_replace("/([a-z])([A-Z])/", '$1-$2', $input));
  368. }
  369. else {
  370. $input = str_replace('-', ' ', strtolower(
  371. preg_replace("/[^a-zA-Z0-9\_\-]/", '', $input)));
  372. return str_replace(' ', '', ucwords($input));
  373. }
  374. }
  375. /**
  376. *
  377. * method used to replace parameters with regular expressions in the route path
  378. *
  379. * @param array $matches
  380. *
  381. * @return string
  382. */
  383. protected function _matchCallback($matches)
  384. {
  385. $key = str_replace(':', '', $matches[0]);
  386. if (array_key_exists($key, $this->_conditions)) {
  387. return '(' . $this->_conditions[$key] . ')';
  388. }
  389. return '([a-zA-Z0-9_\+\-%]+)';
  390. }
  391. /**
  392. *
  393. * get an array of params and if the route matches, return a routed url
  394. * the method will also route requests if all params in the route match the params in
  395. * the request, but there are extra params in the request
  396. *
  397. * @param array $params
  398. * @param bool $named if the flag is set to true, we need to match by params only
  399. *
  400. * @return string|null the assembled url or null if the route doesnt match
  401. */
  402. abstract public function assemble($params, $named = false);
  403. }