vendor/liip/imagine-bundle/DependencyInjection/Compiler/AssetsVersionCompilerPass.php line 37

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the `liip/LiipImagineBundle` project.
  4.  *
  5.  * (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE.md
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Liip\ImagineBundle\DependencyInjection\Compiler;
  11. use Symfony\Component\Asset\VersionStrategy\JsonManifestVersionStrategy;
  12. use Symfony\Component\Asset\VersionStrategy\StaticVersionStrategy;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. /**
  15.  * Inject the Symfony framework assets version parameter to the
  16.  * LiipImagineBundle twig extension if possible.
  17.  *
  18.  * We extract either:
  19.  *  - the version parameter from the StaticVersionStrategy service
  20.  *  - the json manifest from the JsonManifestVersionStrategy service
  21.  * If anything is not as expected, we log a warning and do nothing.
  22.  *
  23.  * The expectation is for the user to configure the assets version in liip
  24.  * imagine for custom setups.
  25.  *
  26.  * Anything other than StaticVersionStrategy or JsonManifestVersionStrategy needs
  27.  * to be implemented by the user in CacheResolveEvent event listeners.
  28.  */
  29. class AssetsVersionCompilerPass extends AbstractCompilerPass
  30. {
  31.     public function process(ContainerBuilder $container): void
  32.     {
  33.         if (!class_exists(StaticVersionStrategy::class)
  34.             // this application has no asset version configured
  35.             || !$container->has('assets._version__default')
  36.             // we are not using the new LazyFilterRuntime
  37.             || !$container->hasDefinition('liip_imagine.templating.filter_runtime')
  38.         ) {
  39.             return;
  40.         }
  41.         $runtimeDefinition $container->getDefinition('liip_imagine.templating.filter_runtime');
  42.         if (null !== $runtimeDefinition->getArgument(1)) {
  43.             // the asset version has been set explicitly
  44.             return;
  45.         }
  46.         $versionStrategyDefinition $container->findDefinition('assets._version__default');
  47.         if (!is_a($versionStrategyDefinition->getClass(), StaticVersionStrategy::class, true)
  48.             && !is_a($versionStrategyDefinition->getClass(), JsonManifestVersionStrategy::class, true)
  49.         ) {
  50.             $this->log($container'Symfony assets versioning strategy "'.$versionStrategyDefinition->getClass().'" not automatically supported. Configure liip_imagine.twig.assets_version if you have problems with assets versioning');
  51.             return;
  52.         }
  53.         $version $versionStrategyDefinition->getArgument(0);
  54.         $format $versionStrategyDefinition->getArgument(1);
  55.         $format $container->resolveEnvPlaceholders($format);
  56.         if ($format && !$this->str_ends_with($format'?%%s')) {
  57.             $this->log($container'Can not handle assets versioning with custom format "'.$format.'". asset twig function can likely not be used with the imagine_filter');
  58.             return;
  59.         }
  60.         $runtimeDefinition->setArgument(1$version);
  61.         if (is_a($versionStrategyDefinition->getClass(), JsonManifestVersionStrategy::class, true)) {
  62.             $jsonManifestString file_get_contents($version);
  63.             if (!\is_string($jsonManifestString)) {
  64.                 $this->log($container'Can not handle assets versioning with "'.$versionStrategyDefinition->getClass().'". The manifest file at "'.$version.' " could not be read');
  65.                 return;
  66.             }
  67.             $jsonManifest json_decode($jsonManifestStringtrue);
  68.             if (!\is_array($jsonManifest)) {
  69.                 $this->log($container'Can not handle assets versioning with "'.$versionStrategyDefinition->getClass().'". The manifest file at "'.$version.' " does not contain valid JSON');
  70.                 return;
  71.             }
  72.             $runtimeDefinition->setArgument(1null);
  73.             $runtimeDefinition->setArgument(2$jsonManifest);
  74.         }
  75.     }
  76.     /**
  77.      * Can be replaced with the built-in method when dropping support for PHP < 8.0
  78.      */
  79.     private function str_ends_with(string $haystackstring $needle): bool
  80.     {
  81.         return mb_substr($haystack, -mb_strlen($needle)) === $needle;
  82.     }
  83. }