|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Symfony package. |
|
|
5 |
* |
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com> |
|
|
7 |
* |
|
|
8 |
* For the full copyright and license information, please view the LICENSE |
|
|
9 |
* file that was distributed with this source code. |
|
|
10 |
*/ |
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\Templating\Asset; |
|
|
13 |
|
|
|
14 |
/** |
|
|
15 |
* The URL packages adds a version and a base URL to asset URLs. |
|
|
16 |
* |
|
|
17 |
* @author Kris Wallsmith <kris@symfony.com> |
|
|
18 |
*/ |
|
|
19 |
class UrlPackage extends Package |
|
|
20 |
{ |
|
|
21 |
private $baseUrls; |
|
|
22 |
|
|
|
23 |
/** |
|
|
24 |
* Constructor. |
|
|
25 |
* |
|
|
26 |
* @param string|array $baseUrls Base asset URLs |
|
|
27 |
* @param string $version The package version |
|
|
28 |
* @param string $format The format used to apply the version |
|
|
29 |
*/ |
|
|
30 |
public function __construct($baseUrls = array(), $version = null, $format = null) |
|
|
31 |
{ |
|
|
32 |
parent::__construct($version, $format); |
|
|
33 |
|
|
|
34 |
if (!is_array($baseUrls)) { |
|
|
35 |
$baseUrls = (array) $baseUrls; |
|
|
36 |
} |
|
|
37 |
|
|
|
38 |
$this->baseUrls = array(); |
|
|
39 |
foreach ($baseUrls as $baseUrl) { |
|
|
40 |
$this->baseUrls[] = rtrim($baseUrl, '/'); |
|
|
41 |
} |
|
|
42 |
} |
|
|
43 |
|
|
|
44 |
public function getUrl($path) |
|
|
45 |
{ |
|
|
46 |
if (false !== strpos($path, '://') || 0 === strpos($path, '//')) { |
|
|
47 |
return $path; |
|
|
48 |
} |
|
|
49 |
|
|
|
50 |
$url = $this->applyVersion($path); |
|
|
51 |
|
|
|
52 |
if ($url && '/' != $url[0]) { |
|
|
53 |
$url = '/'.$url; |
|
|
54 |
} |
|
|
55 |
|
|
|
56 |
return $this->getBaseUrl($path).$url; |
|
|
57 |
} |
|
|
58 |
|
|
|
59 |
/** |
|
|
60 |
* Returns the base URL for a path. |
|
|
61 |
* |
|
|
62 |
* @return string The base URL |
|
|
63 |
*/ |
|
|
64 |
public function getBaseUrl($path) |
|
|
65 |
{ |
|
|
66 |
switch ($count = count($this->baseUrls)) { |
|
|
67 |
case 0: |
|
|
68 |
return ''; |
|
|
69 |
|
|
|
70 |
case 1: |
|
|
71 |
return $this->baseUrls[0]; |
|
|
72 |
|
|
|
73 |
default: |
|
|
74 |
return $this->baseUrls[fmod(hexdec(substr(md5($path), 0, 10)), $count)]; |
|
|
75 |
} |
|
|
76 |
} |
|
|
77 |
} |