PageRenderTime 28ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php

https://gitlab.com/daniruizcamacho/pfcascensores
PHP | 496 lines | 210 code | 56 blank | 230 comment | 11 complexity | a2318808b4257f80f79ec515cf73d2d3 MD5 | raw file
  1. <?php namespace Illuminate\Routing;
  2. use InvalidArgumentException;
  3. use Symfony\Component\HttpFoundation\Request;
  4. class UrlGenerator {
  5. /**
  6. * The route collection.
  7. *
  8. * @var \Illuminate\Routing\RouteCollection
  9. */
  10. protected $routes;
  11. /**
  12. * The request instance.
  13. *
  14. * @var \Symfony\Component\HttpFoundation\Request
  15. */
  16. protected $request;
  17. /**
  18. * Characters that should not be URL encoded.
  19. *
  20. * @var array
  21. */
  22. protected $dontEncode = array(
  23. '%2F' => '/',
  24. '%40' => '@',
  25. '%3A' => ':',
  26. '%3B' => ';',
  27. '%2C' => ',',
  28. '%3D' => '=',
  29. '%2B' => '+',
  30. '%21' => '!',
  31. '%2A' => '*',
  32. '%7C' => '|',
  33. );
  34. /**
  35. * Create a new URL Generator instance.
  36. *
  37. * @param \Illuminate\Routing\RouteCollection $routes
  38. * @param \Symfony\Component\HttpFoundation\Request $request
  39. * @return void
  40. */
  41. public function __construct(RouteCollection $routes, Request $request)
  42. {
  43. $this->routes = $routes;
  44. $this->setRequest($request);
  45. }
  46. /**
  47. * Get the full URL for the current request.
  48. *
  49. * @return string
  50. */
  51. public function full()
  52. {
  53. return $this->request->fullUrl();
  54. }
  55. /**
  56. * Get the current URL for the request.
  57. *
  58. * @return string
  59. */
  60. public function current()
  61. {
  62. return $this->to($this->request->getPathInfo());
  63. }
  64. /**
  65. * Get the URL for the previous request.
  66. *
  67. * @return string
  68. */
  69. public function previous()
  70. {
  71. return $this->to($this->request->headers->get('referer'));
  72. }
  73. /**
  74. * Generate a absolute URL to the given path.
  75. *
  76. * @param string $path
  77. * @param mixed $extra
  78. * @param bool $secure
  79. * @return string
  80. */
  81. public function to($path, $extra = array(), $secure = null)
  82. {
  83. // First we will check if the URL is already a valid URL. If it is we will not
  84. // try to generate a new one but will simply return the URL as is, which is
  85. // convenient since developers do not always have to check if it's valid.
  86. if ($this->isValidUrl($path)) return $path;
  87. $scheme = $this->getScheme($secure);
  88. $tail = implode('/', array_map(
  89. 'rawurlencode', (array) $extra)
  90. );
  91. // Once we have the scheme we will compile the "tail" by collapsing the values
  92. // into a single string delimited by slashes. This just makes it convenient
  93. // for passing the array of parameters to this URL as a list of segments.
  94. $root = $this->getRootUrl($scheme);
  95. return $this->trimUrl($root, $path, $tail);
  96. }
  97. /**
  98. * Generate a secure, absolute URL to the given path.
  99. *
  100. * @param string $path
  101. * @param array $parameters
  102. * @return string
  103. */
  104. public function secure($path, $parameters = array())
  105. {
  106. return $this->to($path, $parameters, true);
  107. }
  108. /**
  109. * Generate a URL to an application asset.
  110. *
  111. * @param string $path
  112. * @param bool $secure
  113. * @return string
  114. */
  115. public function asset($path, $secure = null)
  116. {
  117. if ($this->isValidUrl($path)) return $path;
  118. // Once we get the root URL, we will check to see if it contains an index.php
  119. // file in the paths. If it does, we will remove it since it is not needed
  120. // for asset paths, but only for routes to endpoints in the application.
  121. $root = $this->getRootUrl($this->getScheme($secure));
  122. return $this->removeIndex($root).'/'.trim($path, '/');
  123. }
  124. /**
  125. * Remove the index.php file from a path.
  126. *
  127. * @param string $root
  128. * @return string
  129. */
  130. protected function removeIndex($root)
  131. {
  132. $i = 'index.php';
  133. return str_contains($root, $i) ? str_replace('/'.$i, '', $root) : $root;
  134. }
  135. /**
  136. * Generate a URL to a secure asset.
  137. *
  138. * @param string $path
  139. * @return string
  140. */
  141. public function secureAsset($path)
  142. {
  143. return $this->asset($path, true);
  144. }
  145. /**
  146. * Get the scheme for a raw URL.
  147. *
  148. * @param bool $secure
  149. * @return string
  150. */
  151. protected function getScheme($secure)
  152. {
  153. if (is_null($secure))
  154. {
  155. return $this->request->getScheme().'://';
  156. }
  157. else
  158. {
  159. return $secure ? 'https://' : 'http://';
  160. }
  161. }
  162. /**
  163. * Get the URL to a named route.
  164. *
  165. * @param string $name
  166. * @param mixed $parameters
  167. * @param bool $absolute
  168. * @param \Illuminate\Routing\Route $route
  169. * @return string
  170. *
  171. * @throws \InvalidArgumentException
  172. */
  173. public function route($name, $parameters = array(), $absolute = true, $route = null)
  174. {
  175. $route = $route ?: $this->routes->getByName($name);
  176. $parameters = (array) $parameters;
  177. if ( ! is_null($route))
  178. {
  179. return $this->toRoute($route, $parameters, $absolute);
  180. }
  181. else
  182. {
  183. throw new InvalidArgumentException("Route [{$name}] not defined.");
  184. }
  185. }
  186. /**
  187. * Get the URL for a given route instance.
  188. *
  189. * @param \Illuminate\Routing\Route $route
  190. * @param array $parameters
  191. * @param bool $absolute
  192. * @return string
  193. */
  194. protected function toRoute($route, array $parameters, $absolute)
  195. {
  196. $domain = $this->getRouteDomain($route, $parameters);
  197. $uri = strtr(rawurlencode($this->trimUrl(
  198. $root = $this->replaceRoot($route, $domain, $parameters),
  199. $this->replaceRouteParameters($route->uri(), $parameters)
  200. )), $this->dontEncode).$this->getRouteQueryString($parameters);
  201. return $absolute ? $uri : '/'.ltrim(str_replace($root, '', $uri), '/');
  202. }
  203. /**
  204. * Replace the parameters on the root path.
  205. *
  206. * @param \Illuminate\Routing\Route $route
  207. * @param string $domain
  208. * @param array $parameters
  209. * @return string
  210. */
  211. protected function replaceRoot($route, $domain, &$parameters)
  212. {
  213. return $this->replaceRouteParameters($this->getRouteRoot($route, $domain), $parameters);
  214. }
  215. /**
  216. * Replace all of the wildcard parameters for a route path.
  217. *
  218. * @param string $path
  219. * @param array $parameters
  220. * @return string
  221. */
  222. protected function replaceRouteParameters($path, array &$parameters)
  223. {
  224. if (count($parameters))
  225. {
  226. $path = preg_replace_sub(
  227. '/\{.*?\}/', $parameters, $this->replaceNamedParameters($path, $parameters)
  228. );
  229. }
  230. return trim(preg_replace('/\{.*?\?\}/', '', $path), '/');
  231. }
  232. /**
  233. * Replace all of the named parameters in the path.
  234. *
  235. * @param string $path
  236. * @param array $parameters
  237. * @return string
  238. */
  239. protected function replaceNamedParameters($path, &$parameters)
  240. {
  241. return preg_replace_callback('/\{(.*?)\??\}/', function($m) use (&$parameters)
  242. {
  243. return isset($parameters[$m[1]]) ? array_pull($parameters, $m[1]) : $m[0];
  244. }, $path);
  245. }
  246. /**
  247. * Get the query string for a given route.
  248. *
  249. * @param array $parameters
  250. * @return string
  251. */
  252. protected function getRouteQueryString(array $parameters)
  253. {
  254. // First we will get all of the string parameters that are remaining after we
  255. // have replaced the route wildcards. We'll then build a query string from
  256. // these string parameters then use it as a starting point for the rest.
  257. if (count($parameters) == 0) return '';
  258. $query = http_build_query(
  259. $keyed = $this->getStringParameters($parameters)
  260. );
  261. // Lastly, if there are still parameters remaining, we will fetch the numeric
  262. // parameters that are in the array and add them to the query string or we
  263. // will build the intial query string if it wasn't started with strings.
  264. if (count($keyed) < count($parameters))
  265. {
  266. $query .= '&'.implode(
  267. '&', $this->getNumericParameters($parameters)
  268. );
  269. }
  270. return '?'.trim($query, '&');
  271. }
  272. /**
  273. * Get the string parameters from a given list.
  274. *
  275. * @param array $parameters
  276. * @return array
  277. */
  278. protected function getStringParameters(array $parameters)
  279. {
  280. return array_where($parameters, function($k, $v) { return is_string($k); });
  281. }
  282. /**
  283. * Get the numeric parameters from a given list.
  284. *
  285. * @param array $parameters
  286. * @return array
  287. */
  288. protected function getNumericParameters(array $parameters)
  289. {
  290. return array_where($parameters, function($k, $v) { return is_numeric($k); });
  291. }
  292. /**
  293. * Get the formatted domain for a given route.
  294. *
  295. * @param \Illuminate\Routing\Route $route
  296. * @param array $parameters
  297. * @return string
  298. */
  299. protected function getRouteDomain($route, &$parameters)
  300. {
  301. return $route->domain() ? $this->formatDomain($route, $parameters) : null;
  302. }
  303. /**
  304. * Format the domain and port for the route and request.
  305. *
  306. * @param \Illuminate\Routing\Route $route
  307. * @param array $parameters
  308. * @return string
  309. */
  310. protected function formatDomain($route, &$parameters)
  311. {
  312. return $this->addPortToDomain($this->getDomainAndScheme($route));
  313. }
  314. /**
  315. * Get the domain and scheme for the route.
  316. *
  317. * @param \Illuminate\Routing\Route $route
  318. * @return string
  319. */
  320. protected function getDomainAndScheme($route)
  321. {
  322. return $this->getRouteScheme($route).$route->domain();
  323. }
  324. /**
  325. * Add the port to the domain if necessary.
  326. *
  327. * @param string $domain
  328. * @return string
  329. */
  330. protected function addPortToDomain($domain)
  331. {
  332. if (in_array($this->request->getPort(), array('80', '443')))
  333. {
  334. return $domain;
  335. }
  336. else
  337. {
  338. return $domain .= ':'.$this->request->getPort();
  339. }
  340. }
  341. /**
  342. * Get the root of the route URL.
  343. *
  344. * @param \Illuminate\Routing\Route $route
  345. * @param string $domain
  346. * @return string
  347. */
  348. protected function getRouteRoot($route, $domain)
  349. {
  350. return $this->getRootUrl($this->getRouteScheme($route), $domain);
  351. }
  352. /**
  353. * Get the scheme for the given route.
  354. *
  355. * @param \Illuminate\Routing\Route $route
  356. * @return string
  357. */
  358. protected function getRouteScheme($route)
  359. {
  360. if ($route->httpOnly())
  361. {
  362. return $this->getScheme(false);
  363. }
  364. elseif ($route->httpsOnly())
  365. {
  366. return $this->getScheme(true);
  367. }
  368. else
  369. {
  370. return $this->getScheme(null);
  371. }
  372. }
  373. /**
  374. * Get the URL to a controller action.
  375. *
  376. * @param string $action
  377. * @param mixed $parameters
  378. * @param bool $absolute
  379. * @return string
  380. */
  381. public function action($action, $parameters = array(), $absolute = true)
  382. {
  383. return $this->route($action, $parameters, $absolute, $this->routes->getByAction($action));
  384. }
  385. /**
  386. * Get the base URL for the request.
  387. *
  388. * @param string $scheme
  389. * @param string $root
  390. * @return string
  391. */
  392. protected function getRootUrl($scheme, $root = null)
  393. {
  394. $root = $root ?: $this->request->root();
  395. $start = starts_with($root, 'http://') ? 'http://' : 'https://';
  396. return preg_replace('~'.$start.'~', $scheme, $root, 1);
  397. }
  398. /**
  399. * Determine if the given path is a valid URL.
  400. *
  401. * @param string $path
  402. * @return bool
  403. */
  404. public function isValidUrl($path)
  405. {
  406. if (starts_with($path, array('#', '//', 'mailto:', 'tel:'))) return true;
  407. return filter_var($path, FILTER_VALIDATE_URL) !== false;
  408. }
  409. /**
  410. * Format the given URL segments into a single URL.
  411. *
  412. * @param string $root
  413. * @param string $path
  414. * @param string $tail
  415. * @return string
  416. */
  417. protected function trimUrl($root, $path, $tail = '')
  418. {
  419. return trim($root.'/'.trim($path.'/'.$tail, '/'), '/');
  420. }
  421. /**
  422. * Get the request instance.
  423. *
  424. * @return \Symfony\Component\HttpFoundation\Request
  425. */
  426. public function getRequest()
  427. {
  428. return $this->request;
  429. }
  430. /**
  431. * Set the current request instance.
  432. *
  433. * @param \Symfony\Component\HttpFoundation\Request $request
  434. * @return void
  435. */
  436. public function setRequest(Request $request)
  437. {
  438. $this->request = $request;
  439. }
  440. }