<commit_before><?php
/**
 * This file is part of Berlioz framework.
 *
 * @license   https://opensource.org/licenses/MIT MIT License
 * @copyright 2017 <NAME>
 * @author    <NAME> <https://github.com/ElGigi>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code, to the root.
 */

namespace Berlioz\Core\Services\Template;


use Berlioz\Core\App;
use Berlioz\Core\App\AppAwareTrait;
use Berlioz\Core\ConfigInterface;

class DefaultEngine implements TemplateInterface
{
    use AppAwareTrait;
    /** @var \Twig_Loader_Filesystem Twig loader filesystem */
    private $twigLoader;
    /** @var \Twig_Environment Twig environment */
    private $twig;

    /**
     * DefaultEngine constructor
     *
     * @param \Berlioz\Core\App $app
     */
    public function __construct(App $app)
    {
        $this->setApp($app);

        // Init Twig
        $this->getTwig()->addExtension(new TwigExtension($this));
        $this->getTwig()->addGlobal('app', $app->getProfile());
        if ($this->getApp()->getConfig()->hasCacheEnabled()) {
            $this->getTwig()->setCache($this->getApp()->getConfig()->getDirectory(ConfigInterface::DIR_VAR_CACHE) . '/Twig');
        }
        if ($this->getApp()->getConfig()->hasDebugEnabled()) {
            $this->getTwig()->enableDebug();
        }
    }

    /**
     * Get Twig.
     *
     * @return \Twig_Environment
     */
    public function getTwig(): \Twig_Environment
    {
        if (is_null($this->twig)) {
            // Init Twig
            $this->twigLoader = new \Twig_Loader_Filesystem();
            $this->twig = new \Twig_Environment($this->twigLoader);
        }

        return $this->twig;
    }

    /**
     * Register a new path for template engine
     *
     * @param string      $path      Path
     * @param string|null $namespace Namespace
     *
     * @return void
     */
    public function registerPath(string $path, string $namespace = null): void
    {
        if (is_null($namespace)) {
            $namespace = \Twig_Loader_Filesystem::MAIN_NAMESPACE;
        }

        $this->twigLoader->addPath($path, $namespace);
    }

    /**
     * Render a template
     *
     * @param string $name      Template filename
     * @param array  $variables Variables
     *
     * @return string
     */
    public function render(string $name, array $variables = []): string
    {
        // Debug
        $this->getApp()->getService('logging')->debug(sprintf('%s / Rendering of template "%s"', __METHOD__, $name));

        $str = $this->twig->render($name, $variables);

        // Debug
        $this->getApp()->getService('logging')->debug(sprintf('%s / Rendering of template "%s" done', __METHOD__, $name));

        return $str;
    }

    /**
     * Has block in template ?
     *
     * @param string $tplName   Template filename
     * @param string $blockName Block name
     *
     * @return bool
     */
    public function hasBlock(string $tplName, string $blockName): bool
    {
        $template = $this->twig->load($tplName);

        return $template->hasBlock($blockName);
    }

    /**
     * Render a block in template
     *
     * @param string $tplName   Template filename
     * @param string $blockName Block name
     * @param array  $variables Variables
     *
     * @return string
     */
    public function renderBlock(string $tplName, string $blockName, array $variables = []): string
    {
        // Debug
        $this->getApp()->getService('logging')->debug(sprintf('%s / Rendering of block "%s" in template "%s"', __METHOD__, $blockName, $tplName));

        $template = $this->twig->load($tplName);
        $str = $template->renderBlock($blockName, $variables);

        // Debug
        $this->getApp()->getService('logging')->debug(sprintf('%s / Rendering of block "%s" in template "%s" done', __METHOD__, $blockName, $tplName));

        return $str;
    }
}<commit_msg>Fix Twig debug, extension not declared<commit_after><?php
/**
 * This file is part of Berlioz framework.
 *
 * @license   https://opensource.org/licenses/MIT MIT License
 * @copyright 2017 <NAME>
 * @author    <NAME> <https://github.com/ElGigi>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code, to the root.
 */

namespace Berlioz\Core\Services\Template;


use Berlioz\Core\App;
use Berlioz\Core\App\AppAwareTrait;
use Berlioz\Core\ConfigInterface;

class DefaultEngine implements TemplateInterface
{
    use AppAwareTrait;
    /** @var \Twig_Loader_Filesystem Twig loader filesystem */
    private $twigLoader;
    /** @var \Twig_Environment Twig environment */
    private $twig;

    /**
     * DefaultEngine constructor
     *
     * @param \Berlioz\Core\App $app
     */
    public function __construct(App $app)
    {
        $this->setApp($app);

        // Init Twig
        $this->getTwig()->addExtension(new TwigExtension($this));
        $this->getTwig()->addGlobal('app', $app->getProfile());
        if ($this->getApp()->getConfig()->hasCacheEnabled()) {
            $this->getTwig()->setCache($this->getApp()->getConfig()->getDirectory(ConfigInterface::DIR_VAR_CACHE) . '/Twig');
        }
        if ($this->getApp()->getConfig()->hasDebugEnabled()) {
            $this->getTwig()->enableDebug();
            $this->getTwig()->addExtension(new \Twig_Extension_Debug());
        }
    }

    /**
     * Get Twig.
     *
     * @return \Twig_Environment
     */
    public function getTwig(): \Twig_Environment
    {
        if (is_null($this->twig)) {
            // Init Twig
            $this->twigLoader = new \Twig_Loader_Filesystem();
            $this->twig = new \Twig_Environment($this->twigLoader);
        }

        return $this->twig;
    }

    /**
     * Register a new path for template engine
     *
     * @param string      $path      Path
     * @param string|null $namespace Namespace
     *
     * @return void
     */
    public function registerPath(string $path, string $namespace = null): void
    {
        if (is_null($namespace)) {
            $namespace = \Twig_Loader_Filesystem::MAIN_NAMESPACE;
        }

        $this->twigLoader->addPath($path, $namespace);
    }

    /**
     * Render a template
     *
     * @param string $name      Template filename
     * @param array  $variables Variables
     *
     * @return string
     */
    public function render(string $name, array $variables = []): string
    {
        // Debug
        $this->getApp()->getService('logging')->debug(sprintf('%s / Rendering of template "%s"', __METHOD__, $name));

        $str = $this->twig->render($name, $variables);

        // Debug
        $this->getApp()->getService('logging')->debug(sprintf('%s / Rendering of template "%s" done', __METHOD__, $name));

        return $str;
    }

    /**
     * Has block in template ?
     *
     * @param string $tplName   Template filename
     * @param string $blockName Block name
     *
     * @return bool
     */
    public function hasBlock(string $tplName, string $blockName): bool
    {
        $template = $this->twig->load($tplName);

        return $template->hasBlock($blockName);
    }

    /**
     * Render a block in template
     *
     * @param string $tplName   Template filename
     * @param string $blockName Block name
     * @param array  $variables Variables
     *
     * @return string
     */
    public function renderBlock(string $tplName, string $blockName, array $variables = []): string
    {
        // Debug
        $this->getApp()->getService('logging')->debug(sprintf('%s / Rendering of block "%s" in template "%s"', __METHOD__, $blockName, $tplName));

        $template = $this->twig->load($tplName);
        $str = $template->renderBlock($blockName, $variables);

        // Debug
        $this->getApp()->getService('logging')->debug(sprintf('%s / Rendering of block "%s" in template "%s" done', __METHOD__, $blockName, $tplName));

        return $str;
    }
}
{'fraction_non_alphanumeric': 0.14638403990024937, 'fraction_numerical': 0.000997506234413965, 'mean_word_length': 2.4558380008616973, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 103, 'https://': 4, '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': '2635728', 'n_tokens_mistral': 2383, 'n_tokens_neox': 2357, 'n_words': 553}