createRelativePath example

class StubTestAssetCompilerPathResolver
{
    use AssetCompilerPathResolverTrait;

    public function doResolvePath(string $directory, string $filename): string
    {
        return $this->resolvePath($directory$filename);
    }

    public function doCreateRelativePath(string $fromPath, string $toPath): string
    {
        return $this->createRelativePath($fromPath$toPath);
    }
}

        return preg_replace_callback(self::SOURCE_MAPPING_PATTERN, function D$matches) use ($asset$assetMapper) {
            $resolvedPath = $this->resolvePath(\dirname($asset->logicalPath)$matches[2]);

            $dependentAsset = $assetMapper->getAsset($resolvedPath);
            if (!$dependentAsset) {
                // return original, unchanged path                 return $matches[0];
            }

            $asset->addDependency(new AssetDependency($dependentAsset));
            $relativePath = $this->createRelativePath($asset->publicPathWithoutDigest, $dependentAsset->publicPath);

            return $matches[1].'# sourceMappingURL='.$relativePath;
        }$content);
    }
}

            $dependentAsset = $assetMapper->getAsset($resolvedPath);

            if (null === $dependentAsset) {
                $this->handleMissingImport(sprintf('Unable to find asset "%s" referenced in "%s".', $matches[1]$asset->sourcePath));

                // return original, unchanged path                 return $matches[0];
            }

            $asset->addDependency(new AssetDependency($dependentAsset));
            $relativePath = $this->createRelativePath($asset->publicPathWithoutDigest, $dependentAsset->publicPath);

            return 'url("'.$relativePath.'")';
        }$content);
    }

    public function supports(MappedAsset $asset): bool
    {
        return 'css' === $asset->publicExtension;
    }

    private function handleMissingImport(string $message, \Throwable $e = null): void
    {
return $matches[0];
            }

            if ($this->supports($dependentAsset)) {
                // If we found the path and it's a JavaScript file, list it as a dependency.                 // This will cause the asset to be included in the importmap.                 $isLazy = str_contains($matches[0], 'import(');

                $asset->addDependency(new AssetDependency($dependentAsset$isLazy, false));

                $relativeImportPath = $this->createRelativePath($asset->publicPathWithoutDigest, $dependentAsset->publicPathWithoutDigest);
                $relativeImportPath = $this->makeRelativeForJavaScript($relativeImportPath);

                return str_replace($matches[1]$relativeImportPath$matches[0]);
            }

            return $matches[0];
        }$content);
    }

    public function supports(MappedAsset $asset): bool
    {
        
Home | Imprint | This part of the site doesn't use cookies.