<commit_before><?php
/*
 * Copyright (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace PHPoole\Renderer;

use Symfony\Component\Filesystem\Filesystem;

/**
 * Class Twig.
 */
class Twig implements RendererInterface
{
    /**
     * @var \Twig_Environment
     */
    protected $twig;
    /**
     * @var string
     */
    protected $templatesDir;
    /**
     * @var string
     */
    protected $rendered;
    /**
     * @var Filesystem
     */
    protected $fs;
    /**
     * @var bool
     */
    protected $twigStrict = false;
    /**
     * @var bool
     */
    protected $twigDebug = true;

    /**
     * {@inheritdoc}
     */
    public function __construct($templatesPath = '')
    {
        if (!empty($templatesPath)) {
            $this->templatesDir = $templatesPath;
        }

        $loaderFS = new \Twig_Loader_Filesystem($this->templatesDir);
        /*
        $loaderArray = new \Twig_Loader_Array(array(
            'redirect.html' => '<!DOCTYPE html>
<html>
<head lang="en">
    <link rel="canonical" href="{{ url(page.destination) }}"/>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <meta http-equiv="refresh" content="0;url={{ url(page.destination) }}" />
</head>
</html>',
        ));
        */
        //$loader = new \Twig_Loader_Chain(array($loaderArray, $loaderFS));
        //$this->twig = new \Twig_Environment($loader,
        $this->twig = new \Twig_Environment($loaderFS,
            [
                'autoescape'       => false,
                'strict_variables' => $this->twigStrict,
                'debug'            => $this->twigDebug,
            ]
        );
        $this->twig->addExtension(new \Twig_Extension_Debug());
        $this->twig->addExtension(new TwigExtensionSortArray());
        $this->twig->addExtension(new TwigExtensionUrlize());

        // excerpt filter
        $excerptFilter = new \Twig_SimpleFilter('excerpt', function ($string, $length = 450, $suffix = '…') {
            $str = trim(strip_tags($string));
            if (mb_strlen($str) > $length) {
                $string = mb_substr($string, 0, $length).$suffix;
            }

            return $string;
        });
        $this->twig->addFilter($excerptFilter);

        $this->fs = new Filesystem();
    }

    /**
     * {@inheritdoc}
     *
     * @throws \Twig_Error_Loader
     */
    public function addPath($path)
    {
        if (is_dir($path)) {
            /* @var $loader \Twig_Loader_Filesystem */
            $loader = $this->twig->getLoader();
            $loader->addPath($path);
            $this->twig->setLoader($loader);
        }
    }

    /**
     * {@inheritdoc}
     */
    public function addGlobal($name, $value)
    {
        $this->twig->addGlobal($name, $value);
    }

    /**
     * {@inheritdoc}
     */
    public function render($template, $variables)
    {
        $this->rendered = $this->twig->render($template, $variables);

        return $this;
    }

    /**
     * {@inheritdoc}
     */
    public function save($pathname)
    {
        if (!is_dir($dir = dirname($pathname))) {
            $this->fs->mkdir($dir);
        }
        if (false !== @file_put_contents($pathname, $this->rendered)) {
            return true;
        }
    }
}
<commit_msg>Redirect logic through internal template<commit_after><?php
/*
 * Copyright (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace PHPoole\Renderer;

use Symfony\Component\Filesystem\Filesystem;

/**
 * Class Twig.
 */
class Twig implements RendererInterface
{
    /**
     * @var \Twig_Environment
     */
    protected $twig;
    /**
     * @var string
     */
    protected $templatesDir;
    /**
     * @var string
     */
    protected $rendered;
    /**
     * @var Filesystem
     */
    protected $fs;
    /**
     * @var bool
     */
    protected $twigStrict = false;
    /**
     * @var bool
     */
    protected $twigDebug = true;

    /**
     * {@inheritdoc}
     */
    public function __construct($templatesPath = '')
    {
        if (!empty($templatesPath)) {
            $this->templatesDir = $templatesPath;
        }

        $loaderFS = new \Twig_Loader_Filesystem($this->templatesDir);
        $loaderArray = new \Twig_Loader_Array(array(
            'redirect.html' => '<!DOCTYPE html>
<html>
<head lang="en">
    <link rel="canonical" href="{{ url(page.destination) }}"/>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <meta http-equiv="refresh" content="0;url={{ url(page.destination) }}" />
</head>
</html>',
        ));
        $loader = new \Twig_Loader_Chain(array($loaderArray, $loaderFS));
        $this->twig = new \Twig_Environment($loader,
        //$this->twig = new \Twig_Environment($loaderFS,
            [
                'autoescape'       => false,
                'strict_variables' => $this->twigStrict,
                'debug'            => $this->twigDebug,
            ]
        );
        $this->twig->addExtension(new \Twig_Extension_Debug());
        $this->twig->addExtension(new TwigExtensionSortArray());
        $this->twig->addExtension(new TwigExtensionUrlize());

        // excerpt filter
        $excerptFilter = new \Twig_SimpleFilter('excerpt', function ($string, $length = 450, $suffix = '…') {
            $str = trim(strip_tags($string));
            if (mb_strlen($str) > $length) {
                $string = mb_substr($string, 0, $length).$suffix;
            }

            return $string;
        });
        $this->twig->addFilter($excerptFilter);

        $this->fs = new Filesystem();
    }

    /**
     * {@inheritdoc}
     *
     * @throws \Twig_Error_Loader
     */
    public function addPath($path)
    {
        if (is_dir($path)) {
            /* @var $loader \Twig_Loader_Filesystem */
            $loader = $this->twig->getLoader();
            $loader->addPath($path);
            $this->twig->setLoader($loader);
        }
    }

    /**
     * {@inheritdoc}
     */
    public function addGlobal($name, $value)
    {
        $this->twig->addGlobal($name, $value);
    }

    /**
     * {@inheritdoc}
     */
    public function render($template, $variables)
    {
        $this->rendered = $this->twig->render($template, $variables);

        return $this;
    }

    /**
     * {@inheritdoc}
     */
    public function save($pathname)
    {
        if (!is_dir($dir = dirname($pathname))) {
            $this->fs->mkdir($dir);
        }
        if (false !== @file_put_contents($pathname, $this->rendered)) {
            return true;
        }
    }
}

{'fraction_non_alphanumeric': 0.16126645953543423, 'fraction_numerical': 0.001775410563692854, 'mean_word_length': 1.9845474613686533, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 91, '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': '6472911', 'n_tokens_mistral': 2098, 'n_tokens_neox': 2045, 'n_words': 380}