• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

liip / LiipImagineBundle / 9579790821

19 Jun 2024 09:42AM UTC coverage: 80.313% (-1.5%) from 81.835%
9579790821

Pull #1597

github

web-flow
Merge 2ff7d2d7f into 0c95437a7
Pull Request #1597: 2 to 3

0 of 3 new or added lines in 1 file covered. (0.0%)

1795 of 2235 relevant lines covered (80.31%)

77.47 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/DependencyInjection/Compiler/AssetsVersionCompilerPass.php
1
<?php
2

3
/*
4
 * This file is part of the `liip/LiipImagineBundle` project.
5
 *
6
 * (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
7
 *
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11

12
namespace Liip\ImagineBundle\DependencyInjection\Compiler;
13

14
use Symfony\Component\Asset\VersionStrategy\JsonManifestVersionStrategy;
15
use Symfony\Component\Asset\VersionStrategy\StaticVersionStrategy;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17

18
/**
19
 * Inject the Symfony framework assets version parameter to the
20
 * LiipImagineBundle twig extension if possible.
21
 *
22
 * We extract either:
23
 *  - the version parameter from the StaticVersionStrategy service
24
 *  - the json manifest from the JsonManifestVersionStrategy service
25
 * If anything is not as expected, we log a warning and do nothing.
26
 *
27
 * The expectation is for the user to configure the assets version in liip
28
 * imagine for custom setups.
29
 *
30
 * Anything other than StaticVersionStrategy or JsonManifestVersionStrategy needs
31
 * to be implemented by the user in CacheResolveEvent event listeners.
32
 */
33
class AssetsVersionCompilerPass extends AbstractCompilerPass
34
{
35
    public function process(ContainerBuilder $container): void
36
    {
37
        if (!class_exists(StaticVersionStrategy::class)
×
38
            // this application has no asset version configured
39
            || !$container->has('assets._version__default')
×
40
            // we are not using the new LazyFilterRuntime
41
            || !$container->hasDefinition('liip_imagine.templating.filter_runtime')
×
42
        ) {
43
            return;
×
44
        }
45
        $runtimeDefinition = $container->getDefinition('liip_imagine.templating.filter_runtime');
×
46
        if (null !== $runtimeDefinition->getArgument(1)) {
×
47
            // the asset version has been set explicitly
48
            return;
×
49
        }
50

51
        $versionStrategyDefinition = $container->findDefinition('assets._version__default');
×
52
        if (!is_a($versionStrategyDefinition->getClass(), StaticVersionStrategy::class, true)
×
53
            && !is_a($versionStrategyDefinition->getClass(), JsonManifestVersionStrategy::class, true)
×
54
        ) {
55
            $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');
×
56

57
            return;
×
58
        }
59
        $version = $versionStrategyDefinition->getArgument(0);
×
60
        $format = $versionStrategyDefinition->getArgument(1);
×
61
        $format = $container->resolveEnvPlaceholders($format);
×
62
        if ($format && !str_ends_with($format, '?%%s')) {
×
63
            $this->log($container, 'Can not handle assets versioning with custom format "'.$format.'". asset twig function can likely not be used with the imagine_filter');
×
64

65
            return;
×
66
        }
67

68
        $runtimeDefinition->setArgument(1, $version);
×
69

70
        if (is_a($versionStrategyDefinition->getClass(), JsonManifestVersionStrategy::class, true)) {
×
NEW
71
            $jsonManifestString = file_get_contents($version);
×
72

NEW
73
            if (!\is_string($jsonManifestString)) {
×
NEW
74
                $this->log($container, 'Can not handle assets versioning with "'.$versionStrategyDefinition->getClass().'". The manifest file at "'.$version.' " could not be read');
×
75

76
                return;
×
77
            }
78
            $jsonManifest = json_decode($jsonManifestString, true);
×
79
            if (!\is_array($jsonManifest)) {
×
80
                $this->log($container, 'Can not handle assets versioning with "'.$versionStrategyDefinition->getClass().'". The manifest file at "'.$version.' " does not contain valid JSON');
×
81

82
                return;
×
83
            }
84
            $runtimeDefinition->setArgument(1, null);
×
85
            $runtimeDefinition->setArgument(2, $jsonManifest);
×
86
        }
87
    }
88
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc