<?php

namespace Wga\Core\XClass;

use TYPO3\CMS\Core\Resource\RelativeCssPathFixer as CoreRelativeCssPathFixer;

/**
 * This fixes import paths in CSS files if their location changes,
 * e.g. when inlining or compressing css
 *
 * @internal This class is not part of the TYPO3 API.
 */
class RelativeCssPathFixer extends CoreRelativeCssPathFixer
{
    /**
     * Fixes the relative paths inside of url() references in CSS files
     *
     * @param string $contents Data to process
     * @param string $newDir directory referenced from current location
     * @return string Processed data
     */
    public function fixRelativeUrlPaths(string $contents, string $newDir): string
    {
        // Replace "@import" paths
        if (stripos($contents, '@import') !== false) {
            $regex = '/@import\\s*(["\']?(?!\\/)([^"\']+)["\']?)/i';
            $contents = $this->findAndReplaceUrlPathsByRegex($contents, $regex, $newDir, '"|"');
        }
        return $contents;
    }
}
