|
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 basic package will add a version to asset URLs. |
|
|
16 |
* |
|
|
17 |
* @author Kris Wallsmith <kris@symfony.com> |
|
|
18 |
*/ |
|
|
19 |
class Package implements PackageInterface |
|
|
20 |
{ |
|
|
21 |
private $version; |
|
|
22 |
private $format; |
|
|
23 |
|
|
|
24 |
/** |
|
|
25 |
* Constructor. |
|
|
26 |
* |
|
|
27 |
* @param string $version The package version |
|
|
28 |
* @param string $format The format used to apply the version |
|
|
29 |
*/ |
|
|
30 |
public function __construct($version = null, $format = null) |
|
|
31 |
{ |
|
|
32 |
$this->version = $version; |
|
|
33 |
$this->format = $format ?: '%s?%s'; |
|
|
34 |
} |
|
|
35 |
|
|
|
36 |
public function getVersion() |
|
|
37 |
{ |
|
|
38 |
return $this->version; |
|
|
39 |
} |
|
|
40 |
|
|
|
41 |
public function getUrl($path) |
|
|
42 |
{ |
|
|
43 |
if (false !== strpos($path, '://') || 0 === strpos($path, '//')) { |
|
|
44 |
return $path; |
|
|
45 |
} |
|
|
46 |
|
|
|
47 |
return $this->applyVersion($path); |
|
|
48 |
} |
|
|
49 |
|
|
|
50 |
/** |
|
|
51 |
* Applies version to the supplied path. |
|
|
52 |
* |
|
|
53 |
* @param string $path A path |
|
|
54 |
* |
|
|
55 |
* @return string The versionized path |
|
|
56 |
*/ |
|
|
57 |
protected function applyVersion($path) |
|
|
58 |
{ |
|
|
59 |
if (null === $this->version) { |
|
|
60 |
return $path; |
|
|
61 |
} |
|
|
62 |
|
|
|
63 |
$versionized = sprintf($this->format, ltrim($path, '/'), $this->version); |
|
|
64 |
|
|
|
65 |
if ($path && '/' == $path[0]) { |
|
|
66 |
$versionized = '/'.$versionized; |
|
|
67 |
} |
|
|
68 |
|
|
|
69 |
return $versionized; |
|
|
70 |
} |
|
|
71 |
} |