<commit_before><?php declare(strict_types=1);

namespace Symplify\Statie\Twig\Renderable;

use Nette\Utils\Strings;
use Symplify\Statie\Configuration\Configuration;
use Symplify\Statie\Contract\Renderable\FileDecoratorInterface;
use Symplify\Statie\Generator\Configuration\GeneratorElement;
use Symplify\Statie\Renderable\File\AbstractFile;
use Symplify\Statie\Twig\TwigRenderer;

final class TwigFileDecorator implements FileDecoratorInterface
{
    /**
     * @var Configuration
     */
    private $configuration;

    /**
     * @var TwigRenderer
     */
    private $twigRenderer;

    public function __construct(Configuration $configuration, TwigRenderer $twigRenderer)
    {
        $this->configuration = $configuration;
        $this->twigRenderer = $twigRenderer;
    }

    /**
     * @param AbstractFile[] $files
     * @return AbstractFile[]
     */
    public function decorateFiles(array $files): array
    {
        foreach ($files as $file) {
            if (! in_array($file->getExtension(), ['twig', 'md'], true)) {
                continue;
            }

            $parameters = $file->getConfiguration() + $this->configuration->getOptions() + [
                'file' => $file,
            ];

            if ($file->getLayout()) {
                $this->prependLayoutToFileContent($file, $file->getLayout());
            }

            $content = $this->twigRenderer->renderFileWithParameters($file, $parameters);

            $file->changeContent($content);
        }

        return $files;
    }

    /**
     * @param AbstractFile[] $files
     * @return AbstractFile[]
     */
    public function decorateFilesWithGeneratorElement(array $files, GeneratorElement $generatorElement): array
    {
        foreach ($files as $file) {
            if (! in_array($file->getExtension(), ['twig', 'md'], true)) {
                continue;
            }

            $this->decorateFileWithGeneratorElements($file, $generatorElement);
        }

        return $files;
    }

    /**
     * Higher priorities are executed first.
     */
    public function getPriority(): int
    {
        return 700;
    }

    private function decorateFileWithGeneratorElements(AbstractFile $file, GeneratorElement $generatorElement): void
    {
        // prepare parameters
        $parameters = $file->getConfiguration() + $this->configuration->getOptions() + [
            $generatorElement->getVariable() => $file,
            'layout' => $generatorElement->getLayout(),
        ];

        // add layout
        $this->prependLayoutToFileContent($file, $generatorElement->getLayout());

        $htmlContent = $this->twigRenderer->renderFileWithParameters($file, $parameters);

        // trim "{% extends %s %}" left over
        $htmlContent = Strings::replace($htmlContent, '#{% extends "[a-z]+" %}#');

        $file->changeContent($htmlContent);
    }

    /**
     * @inspiration https://github.com/sculpin/sculpin/blob/3264c087e31da2d49c9ec825fec38cae4d583d50/src/Sculpin/Bundle/TwigBundle/TwigFormatter.php#L113
     */
    private function prependLayoutToFileContent(AbstractFile $file, string $layout): void
    {
        $content = $file->getContent();

        // wrap to block
        if (! Strings::match($content, '#{% block content %}#')) {
            $content = '{% block content %}' . $content . '{% endblock %}';
        }<commit_msg>improve readability<commit_after><?php declare(strict_types=1);

namespace Symplify\Statie\Twig\Renderable;

use Nette\Utils\Strings;
use Symplify\Statie\Configuration\Configuration;
use Symplify\Statie\Contract\Renderable\FileDecoratorInterface;
use Symplify\Statie\Generator\Configuration\GeneratorElement;
use Symplify\Statie\Renderable\File\AbstractFile;
use Symplify\Statie\Twig\TwigRenderer;

final class TwigFileDecorator implements FileDecoratorInterface
{
    /**
     * @var Configuration
     */
    private $configuration;

    /**
     * @var TwigRenderer
     */
    private $twigRenderer;

    public function __construct(Configuration $configuration, TwigRenderer $twigRenderer)
    {
        $this->configuration = $configuration;
        $this->twigRenderer = $twigRenderer;
    }

    /**
     * @param AbstractFile[] $files
     * @return AbstractFile[]
     */
    public function decorateFiles(array $files): array
    {
        foreach ($files as $file) {
            if (! in_array($file->getExtension(), ['twig', 'md'], true)) {
                continue;
            }

            $parameters = $file->getConfiguration() + $this->configuration->getOptions() + [
                'file' => $file,
            ];

            if ($file->getLayout()) {
                $this->prependLayoutToFileContent($file, $file->getLayout());
            }

            $content = $this->twigRenderer->renderFileWithParameters($file, $parameters);

            $file->changeContent($content);
        }

        return $files;
    }

    /**
     * @param AbstractFile[] $files
     * @return AbstractFile[]
     */
    public function decorateFilesWithGeneratorElement(array $files, GeneratorElement $generatorElement): array
    {
        foreach ($files as $file) {
            if (! in_array($file->getExtension(), ['twig', 'md'], true)) {
                continue;
            }

            // prepare parameters
            $parameters = $file->getConfiguration() + $this->configuration->getOptions() + [
                $generatorElement->getVariable() => $file,
                'layout' => $generatorElement->getLayout(),
            ];

            // add layout
            $this->prependLayoutToFileContent($file, $generatorElement->getLayout());

            $htmlContent = $this->twigRenderer->renderFileWithParameters($file, $parameters);

            // trim "{% extends %s %}" left over
            $htmlContent = Strings::replace($htmlContent, '#{% extends "[a-z]+" %}#');

            $file->changeContent($htmlContent);
        }

        return $files;
    }

    /**
     * Higher priorities are executed first.
     */
    public function getPriority(): int
    {
        return 700;
    }

    /**
     * @inspiration https://github.com/sculpin/sculpin/blob/3264c087e31da2d49c9ec825fec38cae4d583d50/src/Sculpin/Bundle/TwigBundle/TwigFormatter.php#L113
     */
    private function prependLayoutToFileContent(AbstractFile $file, string $layout): void
    {
        $content = $file->getContent();

        // wrap to block
        if (! Strings::match($content, '#{% block content %}#')) {
            $content = '{% block content %}' . $content . '{% endblock %}';
        }
{'fraction_non_alphanumeric': 0.13701338199513383, 'fraction_numerical': 0.009428223844282239, 'mean_word_length': 2.503995737879595, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 58, 'https://': 2, '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': '5711933', 'n_tokens_mistral': 1890, 'n_tokens_neox': 1783, 'n_words': 326}