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

liip / LiipImagineBundle / 9579701261

19 Jun 2024 09:35AM UTC coverage: 81.835% (-0.2%) from 82.018%
9579701261

push

github

web-flow
Merge pull request #1529 from liip/json_manifest_version_strategy_support

Add support for JsonManifestVersionStrategy

43 of 57 new or added lines in 2 files covered. (75.44%)

1 existing line in 1 file now uncovered.

2275 of 2780 relevant lines covered (81.83%)

103.27 hits per line

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

98.11
/Templating/LazyFilterRuntime.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\Templating;
13

14
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
15
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
16
use Twig\Extension\RuntimeExtensionInterface;
17

18
final class LazyFilterRuntime implements RuntimeExtensionInterface
19
{
20
    /**
21
     * @var CacheManager
22
     */
23
    private $cache;
24

25
    /**
26
     * Optional version to remove from the asset filename and re-append to the URL.
27
     *
28
     * @var string|null
29
     */
30
    private $assetVersion;
31

32
    /**
33
     * @var array|null
34
     */
35
    private $jsonManifest;
36

37
    /**
38
     * @var array|null
39
     */
40
    private $jsonManifestLookup;
41

42
    public function __construct(CacheManager $cache, ?string $assetVersion = null, ?array $jsonManifest = null)
43
    {
44
        $this->cache = $cache;
221✔
45
        $this->assetVersion = $assetVersion;
221✔
46
        $this->jsonManifest = $jsonManifest;
221✔
47
        $this->jsonManifestLookup = $jsonManifest ? array_flip($jsonManifest) : null;
221✔
48
    }
119✔
49

50
    /**
51
     * Gets the browser path for the image and filter to apply.
52
     */
53
    public function filter(string $path, string $filter, array $config = [], ?string $resolver = null, int $referenceType = UrlGeneratorInterface::ABSOLUTE_URL): string
54
    {
55
        $path = $this->cleanPath($path);
195✔
56
        $resolvedPath = $this->cache->getBrowserPath($path, $filter, $config, $resolver, $referenceType);
195✔
57

58
        return $this->appendAssetVersion($resolvedPath, $path);
195✔
59
    }
60

61
    /**
62
     * Gets the cache path for the image and filter to apply.
63
     *
64
     * This does not check whether the cached image exists or not.
65
     */
66
    public function filterCache(string $path, string $filter, array $config = [], ?string $resolver = null): string
67
    {
68
        $path = $this->cleanPath($path);
26✔
69
        if (\count($config)) {
26✔
70
            $path = $this->cache->getRuntimePath($path, $config);
13✔
71
        }
72
        $resolvedPath = $this->cache->resolve($path, $filter, $resolver);
26✔
73

74
        return $this->appendAssetVersion($resolvedPath, $path);
26✔
75
    }
76

77
    private function cleanPath(string $path): string
78
    {
79
        if (!$this->assetVersion && !$this->jsonManifest) {
221✔
80
            return $path;
91✔
81
        }
82

83
        if ($this->assetVersion) {
130✔
84
            $start = mb_strrpos($path, $this->assetVersion);
26✔
85
            if (mb_strlen($path) - mb_strlen($this->assetVersion) === $start) {
26✔
86
                return rtrim(mb_substr($path, 0, $start), '?');
13✔
87
            }
88
        }
89

90
        if ($this->jsonManifest) {
117✔
91
            if (\array_key_exists($path, $this->jsonManifestLookup)) {
104✔
92
                return $this->jsonManifestLookup[$path];
104✔
93
            }
94
        }
95

96
        return $path;
13✔
97
    }
98

99
    private function appendAssetVersion(string $resolvedPath, string $path): string
100
    {
101
        if (!$this->assetVersion && !$this->jsonManifest) {
221✔
102
            return $resolvedPath;
91✔
103
        }
104

105
        if ($this->assetVersion) {
130✔
106
            $separator = false !== mb_strpos($resolvedPath, '?') ? '&' : '?';
26✔
107

108
            return $resolvedPath.$separator.$this->assetVersion;
26✔
109
        }
110

111
        if (\array_key_exists($path, $this->jsonManifest)) {
104✔
112
            $prefixedSlash = 0 !== mb_strpos($path, '/') && 0 === mb_strpos($this->jsonManifest[$path], '/');
104✔
113
            $versionedPath = $prefixedSlash ? mb_substr($this->jsonManifest[$path], 1) : $this->jsonManifest[$path];
104✔
114

115
            $originalExt = pathinfo($path, PATHINFO_EXTENSION);
104✔
116
            $resolvedExt = pathinfo($resolvedPath, PATHINFO_EXTENSION);
104✔
117

118
            if ($originalExt !== $resolvedExt) {
104✔
119
                $path = str_replace('.'.$originalExt, '.'.$resolvedExt, $path);
52✔
120
                $versionedPath = str_replace('.'.$originalExt, '.'.$resolvedExt, $versionedPath);
52✔
121
            }
122

123
            $versioning = $this->captureVersion(pathinfo($path, PATHINFO_BASENAME), pathinfo($versionedPath, PATHINFO_BASENAME));
104✔
124
            $resolvedFilename = pathinfo($resolvedPath, PATHINFO_BASENAME);
104✔
125
            $resolvedDir = pathinfo($resolvedPath, PATHINFO_DIRNAME);
104✔
126
            $resolvedPath = $resolvedDir.'/'.$this->insertVersion($resolvedFilename, $versioning['version'], $versioning['position']);
104✔
127
        }
128

129
        return $resolvedPath;
104✔
130
    }
131

132
    /**
133
     * Capture the versioning string from the versioned filename
134
     */
135
    private function captureVersion(string $originalFilename, string $versionedFilename): array
136
    {
137
        $originalLength = mb_strlen($originalFilename);
104✔
138
        $versionedLength = mb_strlen($versionedFilename);
104✔
139

140
        for ($i = 0; $i < $originalLength && $i < $versionedLength; ++$i) {
104✔
141
            if ($originalFilename[$i] !== $versionedFilename[$i]) {
104✔
142
                break;
52✔
143
            }
144
        }
145

146
        $version = mb_substr($versionedFilename, $i, $versionedLength - $originalLength);
104✔
147

148
        return ['version' => $version, 'position' => $i];
104✔
149
    }
150

151
    /**
152
     * Insert the version string into our resolved filename
153
     */
154
    private function insertVersion(string $resolvedFilename, string $version, int $position): string
155
    {
156
        if ($position < 0 || $position > mb_strlen($resolvedFilename)) {
104✔
NEW
157
            return $resolvedFilename;
×
158
        }
159

160
        $firstPart = mb_substr($resolvedFilename, 0, $position);
104✔
161
        $secondPart = mb_substr($resolvedFilename, $position);
104✔
162

163
        return $firstPart.$version.$secondPart;
104✔
164
    }
165
}
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

© 2026 Coveralls, Inc