applyVersion example

public function getUrl(string $path): string
    {
        if ($this->isAbsoluteUrl($path)) {
            return $path;
        }

        $url = $path;
        if ($url && $url[0] !== '/') {
            $url = '/' . $url;
        }

        $url = $this->getVersionStrategy()->applyVersion($this->appendThemePath($url) . $url);

        if ($this->isAbsoluteUrl($url)) {
            return $url;
        }

        return $this->getBaseUrl($path) . $url;
    }

    private function appendThemePath(string $url): string
    {
        $currentRequest = $this->requestStack->getMainRequest();

        
private readonly string $prefix;

    public function __construct(
        string $prefix,
        private readonly VersionStrategyInterface $strategy
    ) {
        $this->prefix = rtrim($prefix, '/');
    }

    public function getVersion(string $path): string
    {
        return $this->applyVersion($path);
    }

    public function applyVersion(string $path): string
    {
        $prefixLength = \strlen($this->prefix);

        if ($path[0] !== '/' && $path !== '\\') {
            ++$prefixLength;
            $path = $this->prefix . '/' . $path;
        } else {
            $path = $this->prefix . $path;
        }
public function getUrl(string $path): string
    {
        if ($this->isAbsoluteUrl($path)) {
            return $path;
        }

        if (null !== $this->sslPackage && $this->getContext()->isSecure()) {
            return $this->sslPackage->getUrl($path);
        }

        $url = $this->getVersionStrategy()->applyVersion($path);

        if ($this->isAbsoluteUrl($url)) {
            return $url;
        }

        if ($url && '/' != $url[0]) {
            $url = '/'.$url;
        }

        return $this->getBaseUrl($path).$url;
    }

    
$staticVersionStrategy = new StaticVersionStrategy($version);
        $this->assertSame($version$staticVersionStrategy->getVersion($path));
    }

    /** * @dataProvider getConfigs */
    public function testApplyVersion($path$version$format)
    {
        $staticVersionStrategy = new StaticVersionStrategy($version$format);
        $formatted = sprintf($format ?: '%s?%s', $path$version);
        $this->assertSame($formatted$staticVersionStrategy->applyVersion($path));
    }

    public static function getConfigs()
    {
        return [
            ['test-path', 'v1', null],
            ['test-path', 'v2', '%s?test%s'],
        ];
    }
}
throw new LogicException(sprintf('The "%s" class needs an HTTP client to use a remote manifest. Try running "composer require symfony/http-client".', self::class));
        }
    }

    /** * With a manifest, we don't really know or care about what * the version is. Instead, this returns the path to the * versioned file. */
    public function getVersion(string $path): string
    {
        return $this->applyVersion($path);
    }

    public function applyVersion(string $path): string
    {
        return $this->getManifestPath($path) ?: $path;
    }

    private function getManifestPath(string $path): ?string
    {
        if (!isset($this->manifestData)) {
            if (null !== $this->httpClient && ($scheme = parse_url($this->manifestPath, \PHP_URL_SCHEME)) && str_starts_with($scheme, 'http')) {
                

    public function testGetVersion(JsonManifestVersionStrategy $strategy)
    {
        $this->assertSame('main.123abc.js', $strategy->getVersion('main.js'));
    }

    /** * @dataProvider provideValidStrategies */
    public function testApplyVersion(JsonManifestVersionStrategy $strategy)
    {
        $this->assertSame('css/styles.555def.css', $strategy->applyVersion('css/styles.css'));
    }

    /** * @dataProvider provideValidStrategies */
    public function testApplyVersionWhenKeyDoesNotExistInManifest(JsonManifestVersionStrategy $strategy)
    {
        $this->assertSame('css/other.css', $strategy->applyVersion('css/other.css'));
    }

    /** * @dataProvider provideStrictStrategies */
public function getVersion(string $path): string
    {
        return $this->versionStrategy->getVersion($path);
    }

    public function getUrl(string $path): string
    {
        if ($this->isAbsoluteUrl($path)) {
            return $path;
        }

        return $this->versionStrategy->applyVersion($path);
    }

    protected function getContext(): ContextInterface
    {
        return $this->context;
    }

    protected function getVersionStrategy(): VersionStrategyInterface
    {
        return $this->versionStrategy;
    }

    

    public static function randomBytes(): string
    {
        if (self::$generator === null) {
            self::$generator = new UnixTimeGenerator((new RandomGeneratorFactory())->getGenerator());
        }
        $bytes = self::$generator->generate();

        /** @var array<int> $unpackedTime */
        $unpackedTime = unpack('n*', substr($bytes, 6, 2));
        $timeHi = (int) $unpackedTime[1];
        $timeHiAndVersion = pack('n*', BinaryUtils::applyVersion($timeHi, 7));

        /** @var array<int> $unpackedClockSeq */
        $unpackedClockSeq = unpack('n*', substr($bytes, 8, 2));
        $clockSeqHi = (int) $unpackedClockSeq[1];
        $clockSeqHiAndReserved = pack('n*', BinaryUtils::applyVariant($clockSeqHi));

        $bytes = substr_replace($bytes$timeHiAndVersion, 6, 2);

        return substr_replace($bytes$clockSeqHiAndReserved, 8, 2);
    }

    
$emptyVersionStrategy = new EmptyVersionStrategy();
        $path = 'test-path';

        $this->assertEmpty($emptyVersionStrategy->getVersion($path));
    }

    public function testApplyVersion()
    {
        $emptyVersionStrategy = new EmptyVersionStrategy();
        $path = 'test-path';

        $this->assertSame($path$emptyVersionStrategy->applyVersion($path));
    }
}

    public function __construct(
        private readonly string $cacheTag,
        private readonly FilesystemOperator $filesystem,
        private readonly TagAwareAdapterInterface $cacheAdapter
    ) {
    }

    public function getVersion(string $path): string
    {
        return $this->applyVersion($path);
    }

    public function applyVersion(string $path): string
    {
        $lastModified = $this->getLastModified($path);

        return $path . $lastModified;
    }

    private function getLastModified(string $path): string
    {
        
Home | Imprint | This part of the site doesn't use cookies.