|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 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\Twig\Extension; |
| 13 | + |
| 14 | +use Symfony\Component\HttpFoundation\RequestStack; |
| 15 | +use Symfony\Component\Templating\Asset\PackageInterface; |
| 16 | + |
| 17 | +/** |
| 18 | + * Twig extension for Symfony asset packages. |
| 19 | + * |
| 20 | + * @author Fabien Potencier <[email protected]> |
| 21 | + */ |
| 22 | +class AssetPackagesExtension extends \Twig_Extension |
| 23 | +{ |
| 24 | + private $defaultPackage; |
| 25 | + private $namedPackages = array(); |
| 26 | + private $requestStack; |
| 27 | + |
| 28 | + /** |
| 29 | + * @param PackageInterface $defaultPackage The default package |
| 30 | + * @param array $namedPackages Additional packages indexed by name |
| 31 | + */ |
| 32 | + public function __construct(PackageInterface $defaultPackage, array $namedPackages = array(), RequestStack $requestStack = null) |
| 33 | + { |
| 34 | + $this->defaultPackage = $defaultPackage; |
| 35 | + |
| 36 | + foreach ($namedPackages as $name => $package) { |
| 37 | + $this->addPackage($name, $package); |
| 38 | + } |
| 39 | + |
| 40 | + $this->requestStack = $requestStack; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Sets the default package. |
| 45 | + * |
| 46 | + * @param PackageInterface $defaultPackage The default package |
| 47 | + */ |
| 48 | + public function setDefaultPackage(PackageInterface $defaultPackage) |
| 49 | + { |
| 50 | + $this->defaultPackage = $defaultPackage; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Adds an asset package to the helper. |
| 55 | + * |
| 56 | + * @param string $name The package name |
| 57 | + * @param PackageInterface $package The package |
| 58 | + */ |
| 59 | + public function addPackage($name, PackageInterface $package) |
| 60 | + { |
| 61 | + $this->namedPackages[$name] = $package; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Returns a list of functions to add to the existing list. |
| 66 | + * |
| 67 | + * @return array An array of functions |
| 68 | + */ |
| 69 | + public function getFunctions() |
| 70 | + { |
| 71 | + return array( |
| 72 | + new \Twig_SimpleFunction('asset', array($this, 'getAssetUrl')), |
| 73 | + new \Twig_SimpleFunction('assets_version', array($this, 'getAssetsVersion')), |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Returns the public path of an asset. |
| 79 | + * |
| 80 | + * Absolute paths (i.e. http://...) are returned unmodified. |
| 81 | + * |
| 82 | + * @param string $path A public path |
| 83 | + * @param string $packageName The name of the asset package to use |
| 84 | + * @param bool $absolute Whether to return an absolute URL or a relative one |
| 85 | + * @param string|bool|null $version A specific version |
| 86 | + * |
| 87 | + * @return string A public path which takes into account the base path and URL path |
| 88 | + */ |
| 89 | + public function getAssetUrl($path, $packageName = null, $absolute = false, $version = null) |
| 90 | + { |
| 91 | + $url = $this->getPackage($packageName)->getUrl($path, $version); |
| 92 | + |
| 93 | + if (!$absolute) { |
| 94 | + return $url; |
| 95 | + } |
| 96 | + |
| 97 | + return $this->ensureUrlIsAbsolute($url); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Returns the version of the assets in a package. |
| 102 | + * |
| 103 | + * @param string $packageName |
| 104 | + * |
| 105 | + * @return int |
| 106 | + */ |
| 107 | + public function getAssetsVersion($packageName = null) |
| 108 | + { |
| 109 | + return $this->getPackage($packageName)->getVersion(); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Returns an asset package. |
| 114 | + * |
| 115 | + * @param string $name The name of the package or null for the default package |
| 116 | + * |
| 117 | + * @return PackageInterface An asset package |
| 118 | + * |
| 119 | + * @throws \InvalidArgumentException If there is no package by that name |
| 120 | + */ |
| 121 | + private function getPackage($name = null) |
| 122 | + { |
| 123 | + if (null === $name) { |
| 124 | + return $this->defaultPackage; |
| 125 | + } |
| 126 | + |
| 127 | + if (!isset($this->namedPackages[$name])) { |
| 128 | + throw new \InvalidArgumentException(sprintf('There is no "%s" asset package.', $name)); |
| 129 | + } |
| 130 | + |
| 131 | + return $this->namedPackages[$name]; |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Ensures an URL is absolute, if possible. |
| 136 | + * |
| 137 | + * @param string $url The URL that has to be absolute |
| 138 | + * |
| 139 | + * @throws \RuntimeException |
| 140 | + * |
| 141 | + * @return string The absolute URL |
| 142 | + */ |
| 143 | + private function ensureUrlIsAbsolute($url) |
| 144 | + { |
| 145 | + if (false !== strpos($url, '://') || 0 === strpos($url, '//')) { |
| 146 | + return $url; |
| 147 | + } |
| 148 | + |
| 149 | + if (!$this->requestStack) { |
| 150 | + throw new \RuntimeException('To generate an absolute URL for an asset, the Symfony Routing component is required.'); |
| 151 | + } |
| 152 | + |
| 153 | + return $this->requestStack->getMasterRequest()->getUriForPath($url); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Returns the name of the extension. |
| 158 | + * |
| 159 | + * @return string The extension name |
| 160 | + */ |
| 161 | + public function getName() |
| 162 | + { |
| 163 | + return 'asset_packages'; |
| 164 | + } |
| 165 | +} |
0 commit comments