<commit_before><?php

/*
 * This file is part of the Sylius package.
 *
 * (c) <NAME>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Sylius\Bundle\ThemeBundle\Twig;

use Symfony\Component\Config\FileLocatorInterface;
use Symfony\Component\Templating\TemplateNameParserInterface;
use Symfony\Component\Templating\TemplateReferenceInterface;

/**
 * @author <NAME> <<EMAIL>>
 */
final class ThemeFilesystemLoader implements \Twig_LoaderInterface, \Twig_ExistsLoaderInterface
{
    /**
     * @var \Twig_LoaderInterface
     */
    private $decoratedLoader;

    /**
     * @var FileLocatorInterface
     */
    private $templateLocator;

    /**
     * @var TemplateNameParserInterface
     */
    private $templateNameParser;

    /**
     * @var array
     */
    private $cache = [];

    /**
     * @param \Twig_LoaderInterface $decoratedLoader
     * @param FileLocatorInterface $templateLocator
     * @param TemplateNameParserInterface $templateNameParser
     */
    public function __construct(
        \Twig_LoaderInterface $decoratedLoader,
        FileLocatorInterface $templateLocator,
        TemplateNameParserInterface $templateNameParser
    ) {
        $this->decoratedLoader = $decoratedLoader;
        $this->templateLocator = $templateLocator;
        $this->templateNameParser = $templateNameParser;
    }

    /**
     * {@inheritdoc}
     */
    public function getSource($name)
    {
        try {
            return file_get_contents($this->findTemplate($name));
        } catch (\Exception $exception) {
            return $this->decoratedLoader->getSource($name);
        }
    }

    /**
     * {@inheritdoc}
     */
    public function getCacheKey($name)
    {
        try {
            return $this->findTemplate($name);
        } catch (\Exception $exception) {
            return $this->decoratedLoader->getCacheKey($name);
        }
    }

    /**
     * {@inheritdoc}
     */
    public function isFresh($name, $time)
    {
        try {
            return filemtime($this->findTemplate($name)) <= $time;
        } catch (\Exception $exception) {
            return $this->decoratedLoader->isFresh($name, $time);
        }
    }

    /**
     * {@inheritdoc}
     */
    public function exists($name)
    {
        return true;
    }

    /**
     * @param TemplateReferenceInterface|string $template
     *
     * @return string
     */
    private function findTemplate($template)
    {
        $logicalName = (string) $template;

        if (isset($this->cache[$logicalName])) {
            return $this->cache[$logicalName];
        }

        $template = $this->templateNameParser->parse($template);
        $file = $this->templateLocator->locate($template);

        return $this->cache[$logicalName] = $file;
    }
}
<commit_msg>[Theme] fix bc break in templates loading, add real template existence check<commit_after><?php

/*
 * This file is part of the Sylius package.
 *
 * (c) <NAME>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Sylius\Bundle\ThemeBundle\Twig;

use Symfony\Component\Config\FileLocatorInterface;
use Symfony\Component\Templating\TemplateNameParserInterface;
use Symfony\Component\Templating\TemplateReferenceInterface;

/**
 * @author <NAME> <<EMAIL>>
 */
final class ThemeFilesystemLoader implements \Twig_LoaderInterface, \Twig_ExistsLoaderInterface
{
    /**
     * @var \Twig_LoaderInterface
     */
    private $decoratedLoader;

    /**
     * @var FileLocatorInterface
     */
    private $templateLocator;

    /**
     * @var TemplateNameParserInterface
     */
    private $templateNameParser;

    /**
     * @var array
     */
    private $cache = [];

    /**
     * @param \Twig_LoaderInterface $decoratedLoader
     * @param FileLocatorInterface $templateLocator
     * @param TemplateNameParserInterface $templateNameParser
     */
    public function __construct(
        \Twig_LoaderInterface $decoratedLoader,
        FileLocatorInterface $templateLocator,
        TemplateNameParserInterface $templateNameParser
    ) {
        $this->decoratedLoader = $decoratedLoader;
        $this->templateLocator = $templateLocator;
        $this->templateNameParser = $templateNameParser;
    }

    /**
     * {@inheritdoc}
     */
    public function getSource($name)
    {
        try {
            return file_get_contents($this->findTemplate($name));
        } catch (\Exception $exception) {
            return $this->decoratedLoader->getSource($name);
        }
    }

    /**
     * {@inheritdoc}
     */
    public function getCacheKey($name)
    {
        try {
            return $this->findTemplate($name);
        } catch (\Exception $exception) {
            return $this->decoratedLoader->getCacheKey($name);
        }
    }

    /**
     * {@inheritdoc}
     */
    public function isFresh($name, $time)
    {
        try {
            return filemtime($this->findTemplate($name)) <= $time;
        } catch (\Exception $exception) {
            return $this->decoratedLoader->isFresh($name, $time);
        }
    }

    /**
     * {@inheritdoc}
     */
    public function exists($name)
    {
        try {
            return stat($this->findTemplate($name)) !== false;
        } catch (\Exception $exception) {
            return false;
        }
    }

    /**
     * @param TemplateReferenceInterface|string $template
     *
     * @return string
     */
    private function findTemplate($template)
    {
        $logicalName = (string) $template;

        if (isset($this->cache[$logicalName])) {
            return $this->cache[$logicalName];
        }

        $template = $this->templateNameParser->parse($template);
        $file = $this->templateLocator->locate($template);

        return $this->cache[$logicalName] = $file;
    }
}

{'fraction_non_alphanumeric': 0.12257629404822121, 'fraction_numerical': 0.0, 'mean_word_length': 2.4131185270425775, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 50, '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': '2402272', 'n_tokens_mistral': 1663, 'n_tokens_neox': 1635, 'n_words': 326}