[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/symfony/twig-bridge/Extension/ -> AssetExtension.php (source)

   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\Bridge\Twig\Extension;
  13  
  14  use Symfony\Component\Asset\Packages;
  15  use Symfony\Component\Asset\VersionStrategy\StaticVersionStrategy;
  16  use Twig\Extension\AbstractExtension;
  17  use Twig\TwigFunction;
  18  
  19  /**
  20   * Twig extension for the Symfony Asset component.
  21   *
  22   * @author Fabien Potencier <fabien@symfony.com>
  23   */
  24  class AssetExtension extends AbstractExtension
  25  {
  26      private $packages;
  27      private $foundationExtension;
  28  
  29      /**
  30       * Passing an HttpFoundationExtension instance as a second argument must not be relied on
  31       * as it's only there to maintain BC with older Symfony version. It will be removed in 3.0.
  32       */
  33      public function __construct(Packages $packages, HttpFoundationExtension $foundationExtension = null)
  34      {
  35          $this->packages = $packages;
  36          $this->foundationExtension = $foundationExtension;
  37      }
  38  
  39      /**
  40       * {@inheritdoc}
  41       */
  42      public function getFunctions()
  43      {
  44          return array(
  45              new TwigFunction('asset', array($this, 'getAssetUrl')),
  46              new TwigFunction('asset_version', array($this, 'getAssetVersion')),
  47              new TwigFunction('assets_version', array($this, 'getAssetsVersion'), array('deprecated' => true, 'alternative' => 'asset_version')),
  48          );
  49      }
  50  
  51      /**
  52       * Returns the public url/path of an asset.
  53       *
  54       * If the package used to generate the path is an instance of
  55       * UrlPackage, you will always get a URL and not a path.
  56       *
  57       * @param string $path        A public path
  58       * @param string $packageName The name of the asset package to use
  59       *
  60       * @return string The public path of the asset
  61       */
  62      public function getAssetUrl($path, $packageName = null, $absolute = false, $version = null)
  63      {
  64          // BC layer to be removed in 3.0
  65          if (2 < $count = \func_num_args()) {
  66              @trigger_error('Generating absolute URLs with the Twig asset() function was deprecated in 2.7 and will be removed in 3.0. Please use absolute_url() instead.', E_USER_DEPRECATED);
  67              if (4 === $count) {
  68                  @trigger_error('Forcing a version with the Twig asset() function was deprecated in 2.7 and will be removed in 3.0.', E_USER_DEPRECATED);
  69              }
  70  
  71              $args = \func_get_args();
  72  
  73              return $this->getLegacyAssetUrl($path, $packageName, $args[2], isset($args[3]) ? $args[3] : null);
  74          }
  75  
  76          return $this->packages->getUrl($path, $packageName);
  77      }
  78  
  79      /**
  80       * Returns the version of an asset.
  81       *
  82       * @param string $path        A public path
  83       * @param string $packageName The name of the asset package to use
  84       *
  85       * @return string The asset version
  86       */
  87      public function getAssetVersion($path, $packageName = null)
  88      {
  89          return $this->packages->getVersion($path, $packageName);
  90      }
  91  
  92      public function getAssetsVersion($packageName = null)
  93      {
  94          @trigger_error('The Twig assets_version() function was deprecated in 2.7 and will be removed in 3.0. Please use asset_version() instead.', E_USER_DEPRECATED);
  95  
  96          return $this->packages->getVersion('/', $packageName);
  97      }
  98  
  99      private function getLegacyAssetUrl($path, $packageName = null, $absolute = false, $version = null)
 100      {
 101          if ($version) {
 102              $package = $this->packages->getPackage($packageName);
 103  
 104              $v = new \ReflectionProperty('Symfony\Component\Asset\Package', 'versionStrategy');
 105              $v->setAccessible(true);
 106  
 107              $currentVersionStrategy = $v->getValue($package);
 108  
 109              if (property_exists($currentVersionStrategy, 'format')) {
 110                  $f = new \ReflectionProperty($currentVersionStrategy, 'format');
 111                  $f->setAccessible(true);
 112  
 113                  $format = $f->getValue($currentVersionStrategy);
 114  
 115                  $v->setValue($package, new StaticVersionStrategy($version, $format));
 116              } else {
 117                  $v->setValue($package, new StaticVersionStrategy($version));
 118              }
 119          }
 120  
 121          try {
 122              $url = $this->packages->getUrl($path, $packageName);
 123          } catch (\Exception $e) {
 124              if ($version) {
 125                  $v->setValue($package, $currentVersionStrategy);
 126              }
 127  
 128              throw $e;
 129          }
 130  
 131          if ($version) {
 132              $v->setValue($package, $currentVersionStrategy);
 133          }
 134  
 135          if ($absolute) {
 136              return $this->foundationExtension->generateAbsoluteUrl($url);
 137          }
 138  
 139          return $url;
 140      }
 141  
 142      /**
 143       * Returns the name of the extension.
 144       *
 145       * @return string The extension name
 146       */
 147      public function getName()
 148      {
 149          return 'asset';
 150      }
 151  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1