vendor/symfony/twig-bridge/Extension/AssetExtension.php line 60

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Bridge\Twig\Extension;
  11. use Symfony\Component\Asset\Packages;
  12. use Twig\Extension\AbstractExtension;
  13. use Twig\TwigFunction;
  14. /**
  15.  * Twig extension for the Symfony Asset component.
  16.  *
  17.  * @author Fabien Potencier <fabien@symfony.com>
  18.  *
  19.  * @final since Symfony 4.4
  20.  */
  21. class AssetExtension extends AbstractExtension
  22. {
  23.     private $packages;
  24.     public function __construct(Packages $packages)
  25.     {
  26.         $this->packages $packages;
  27.     }
  28.     /**
  29.      * {@inheritdoc}
  30.      *
  31.      * @return TwigFunction[]
  32.      */
  33.     public function getFunctions()
  34.     {
  35.         return [
  36.             new TwigFunction('asset', [$this'getAssetUrl']),
  37.             new TwigFunction('asset_version', [$this'getAssetVersion']),
  38.         ];
  39.     }
  40.     /**
  41.      * Returns the public url/path of an asset.
  42.      *
  43.      * If the package used to generate the path is an instance of
  44.      * UrlPackage, you will always get a URL and not a path.
  45.      *
  46.      * @param string $path        A public path
  47.      * @param string $packageName The name of the asset package to use
  48.      *
  49.      * @return string The public path of the asset
  50.      */
  51.     public function getAssetUrl($path$packageName null)
  52.     {
  53.         return $this->packages->getUrl($path$packageName);
  54.     }
  55.     /**
  56.      * Returns the version of an asset.
  57.      *
  58.      * @param string $path        A public path
  59.      * @param string $packageName The name of the asset package to use
  60.      *
  61.      * @return string The asset version
  62.      */
  63.     public function getAssetVersion($path$packageName null)
  64.     {
  65.         return $this->packages->getVersion($path$packageName);
  66.     }
  67.     /**
  68.      * Returns the name of the extension.
  69.      *
  70.      * @return string The extension name
  71.      */
  72.     public function getName()
  73.     {
  74.         return 'asset';
  75.     }
  76. }