<commit_before><?php

namespace Avalanche\Bundle\ImagineBundle\Imagine;

use Avalanche\Bundle\ImagineBundle\Imagine\Filter\FilterManager;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Templating\Helper\AssetsHelper;

class CachePathResolver
{
    /** @var FilterManager */
    private $manager;
    /** @var ParamResolver */
    private $params;
    /** @var RouterInterface */
    private $router;
    /** @var string */
    private $sourceRoot;
    /** @var RequestContext */
    private $context;
    /** @var AssetsHelper */
    private $assets;
    /** @var string */
    private $defaultFrontController;

    /**
     * Constructs cache path resolver with a given web root and cache prefix
     *
     * @param FilterManager    $manager
     * @param ParamResolver    $params
     * @param RouterInterface  $router
     * @param string           $sourceRoot
     * @param RequestContext   $context
     * @param AssetsHelper     $assets
     */
    public function __construct(
        FilterManager $manager,
        ParamResolver $params,
        RouterInterface $router,
        $sourceRoot,
        RequestContext $context = null,
        /*AssetsHelper*/ $assets = null,
        $defaultFrontController
    ) {
        $this->manager    = $manager;
        $this->params     = $params;
        $this->router     = $router;
        $this->context    = $context;
        $this->assets     = $assets;
        $this->sourceRoot = $sourceRoot;
        // In some cases this may be required to force cached image generation.
        // I.e. front controller is not handling any of assets URI.
        $this->defaultFrontController = $defaultFrontController;
    }

    /**
     * Get real path to file for given filter
     *
     * @param string $path
     * @param string $filter
     *
     * @return string|null
     */
    public function getRealPath($path, $filter)
    {
        $fullPath = $this->manager->getOption($filter, 'source_root', $this->sourceRoot) . $path;

        return $this->normalizeFilePath($fullPath);
    }

    /**
     * Get cached path to file for given filter
     *
     * @param string $path
     * @param string $filter
     * @param bool   $evaluate
     *
     * @return null|string
     */
    public function getCachedPath($path, $filter, $evaluate = false)
    {
        return empty($path) ? null : $this->findCachedFile($this->findCachedUri($path, $filter, false), $evaluate);
    }

    public function getCachedUri($path, $filter, $absolute = false)
    {
        return empty($path) ? null : $this->findCachedUri($path, $filter, $absolute);
    }

    /**
     * Gets filtered path for rendering in the browser
     *
     * @param string  $path
     * @param string  $filter
     * @param boolean $absolute
     *
     * @return string|null
     */
    public function getBrowserPath($path, $filter, $absolute = false)
    {
        if (empty($path)) {
            return null;
        }

        $uri    = $this->findCachedUri($path, $filter, $absolute);
        $cached = $this->findCachedFile($uri, true);

        if ($cached) {
            if (!$realPath = $this->getRealPath($path, $filter)) {
                unlink($cached);

                return null;
            }

            if (filemtime($realPath) > filemtime($cached)) {
                unlink($cached);
                $cached = null;
            }
        }

        !$cached && ($uri = $this->findCachedUri($path, $filter, $absolute, true));

        return $uri;
    }

    /** @internal */
    private function findCachedUri($path, $filter, $absolute, $generate = false)
    {
        if ($this->skip($path, $filter)) {
            return $this->assets->getUrl($path, null, false);
        }

        $assetsHost = !$generate;

        $path = ltrim($path, '/');
        $name = '_imagine_' . $filter . $this->params->getRouteSuffix($assetsHost);
        $uri  = $this->params->generateUrl($name, ['path' => $path], $absolute);

        $prefix  = preg_quote($this->params->getCachePrefix($assetsHost), '#');
        if ($assetsHost) {
            $pattern = sprintf('#^((?:[a-z]+:)?//.*?)?(?:/\w+[.]php)?(/%s.*?)$#i', $prefix);
            if (preg_match($pattern, $uri, $m)) {
                $uri = $m[1] . $m[2];
            }
        } elseif (!empty($this->defaultFrontController)) {
            $pattern = sprintf('#^((?:[a-z]+:)?//.*?)?(/\w+[.]php)?(/%s.*?)$#i', $prefix);
            if (preg_match($pattern, $uri, $m)) {
                empty($m[2]) && ($uri = $m[1] . '/' . $this->defaultFrontController . $m[3]);
            }
        }<commit_msg>Stop decorating assets that are skipped<commit_after><?php

namespace Avalanche\Bundle\ImagineBundle\Imagine;

use Avalanche\Bundle\ImagineBundle\Imagine\Filter\FilterManager;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Templating\Helper\AssetsHelper;

class CachePathResolver
{
    /** @var FilterManager */
    private $manager;
    /** @var ParamResolver */
    private $params;
    /** @var RouterInterface */
    private $router;
    /** @var string */
    private $sourceRoot;
    /** @var RequestContext */
    private $context;
    /** @var AssetsHelper */
    private $assets;
    /** @var string */
    private $defaultFrontController;

    /**
     * Constructs cache path resolver with a given web root and cache prefix
     *
     * @param FilterManager    $manager
     * @param ParamResolver    $params
     * @param RouterInterface  $router
     * @param string           $sourceRoot
     * @param RequestContext   $context
     * @param AssetsHelper     $assets
     */
    public function __construct(
        FilterManager $manager,
        ParamResolver $params,
        RouterInterface $router,
        $sourceRoot,
        RequestContext $context = null,
        /*AssetsHelper*/ $assets = null,
        $defaultFrontController
    ) {
        $this->manager    = $manager;
        $this->params     = $params;
        $this->router     = $router;
        $this->context    = $context;
        $this->assets     = $assets;
        $this->sourceRoot = $sourceRoot;
        // In some cases this may be required to force cached image generation.
        // I.e. front controller is not handling any of assets URI.
        $this->defaultFrontController = $defaultFrontController;
    }

    /**
     * Get real path to file for given filter
     *
     * @param string $path
     * @param string $filter
     *
     * @return string|null
     */
    public function getRealPath($path, $filter)
    {
        $fullPath = $this->manager->getOption($filter, 'source_root', $this->sourceRoot) . $path;

        return $this->normalizeFilePath($fullPath);
    }

    /**
     * Get cached path to file for given filter
     *
     * @param string $path
     * @param string $filter
     * @param bool   $evaluate
     *
     * @return null|string
     */
    public function getCachedPath($path, $filter, $evaluate = false)
    {
        return empty($path) ? null : $this->findCachedFile($this->findCachedUri($path, $filter, false), $evaluate);
    }

    public function getCachedUri($path, $filter, $absolute = false)
    {
        return empty($path) ? null : $this->findCachedUri($path, $filter, $absolute);
    }

    /**
     * Gets filtered path for rendering in the browser
     *
     * @param string  $path
     * @param string  $filter
     * @param boolean $absolute
     *
     * @return string|null
     */
    public function getBrowserPath($path, $filter, $absolute = false)
    {
        if (empty($path)) {
            return null;
        }

        $uri    = $this->findCachedUri($path, $filter, $absolute);
        $cached = $this->findCachedFile($uri, true);

        if ($cached) {
            if (!$realPath = $this->getRealPath($path, $filter)) {
                unlink($cached);

                return null;
            }

            if (filemtime($realPath) > filemtime($cached)) {
                unlink($cached);
                $cached = null;
            }
        }

        !$cached && ($uri = $this->findCachedUri($path, $filter, $absolute, true));

        return $uri;
    }

    /** @internal */
    private function findCachedUri($path, $filter, $absolute, $generate = false)
    {
        if ($this->skip($path, $filter)) {
            return $path;
        }

        $assetsHost = !$generate;

        $path = ltrim($path, '/');
        $name = '_imagine_' . $filter . $this->params->getRouteSuffix($assetsHost);
        $uri  = $this->params->generateUrl($name, ['path' => $path], $absolute);

        $prefix  = preg_quote($this->params->getCachePrefix($assetsHost), '#');
        if ($assetsHost) {
            $pattern = sprintf('#^((?:[a-z]+:)?//.*?)?(?:/\w+[.]php)?(/%s.*?)$#i', $prefix);
            if (preg_match($pattern, $uri, $m)) {
                $uri = $m[1] . $m[2];
            }
        } elseif (!empty($this->defaultFrontController)) {
            $pattern = sprintf('#^((?:[a-z]+:)?//.*?)?(/\w+[.]php)?(/%s.*?)$#i', $prefix);
            if (preg_match($pattern, $uri, $m)) {
                empty($m[2]) && ($uri = $m[1] . '/' . $this->defaultFrontController . $m[3]);
            }
        }
{'fraction_non_alphanumeric': 0.15329173838209983, 'fraction_numerical': 0.0010757314974182443, 'mean_word_length': 2.17087312414734, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1050646', 'n_tokens_mistral': 2759, 'n_tokens_neox': 2714, 'n_words': 623}