<commit_before><?php
namespace MicroFW\Templating;

use MicroFW\Core\Exceptions\TemplateDoesNotExistException;
use MicroFW\Templating\Context;

class Template
{
    /** @var string */
    private $templatePath;

    /** @var string */
    private $fullTemplatePath;

    /** @var MicroFW\Tempalate\Context */
    private $context;

    /** @var MicroFW\Core\IConfigurator */
    private static $configurator;

    /**
     * @param $templatePath string
     * @param $context array
     */
    public function __construct($templatePath, $context = [])
    {
        $templateDirs = self::$configurator['TEMPLATE_DIRS'];
        $this->templatePath = $templatePath;
        $this->findExistingTemplate($templateDirs);
        $this->context = new Context($context);
    }

    /**
     * @param $configurator MicroFW\Core\IConfigurator
     * @return void
     */
    public static function init($configurator)
    {
        self::$configurator = $configurator;
    }

    /**
     * Render template and return it.
     * @return string
     */
    public function render()
    {
        ob_start();
        $context = $this->context;
        include($this->fullTemplatePath);
        $content = ob_get_clean();

        return $content;
    }

    private function findExistingTemplate($templateDirs)
    {
        foreach ($templateDirs as $dir) {
            $fullTemplatePath = $dir . '/' . $this->templatePath;
            if (file_exists($fullTemplatePath)) {
                $this->fullTemplatePath = $fullTemplatePath;
                return;
            }
        }

        throw new TemplateDoesNotExistException(<commit_msg>Context passed to Template via render method<commit_after><?php
namespace MicroFW\Templating;

use MicroFW\Core\Exceptions\TemplateDoesNotExistException;
use MicroFW\Templating\Context;

class Template
{
    /** @var string */
    private $templatePath;

    /** @var string */
    private $fullTemplatePath;

    /** @var MicroFW\Tempalate\Context */
    private $context;

    /** @var MicroFW\Core\IConfigurator */
    private static $configurator;

    /**
     * @param $templatePath string
     */
    public function __construct($templatePath)
    {
        $templateDirs = self::$configurator['TEMPLATE_DIRS'];
        $this->templatePath = $templatePath;
        $this->findExistingTemplate($templateDirs);
    }

    /**
     * @param $configurator MicroFW\Core\IConfigurator
     * @return void
     */
    public static function init($configurator)
    {
        self::$configurator = $configurator;
    }

    /**
     * Render template and return it.
     * @param $context array
     * @return string
     */
    public function render($context = [])
    {
        ob_start();
        $context = new Context($context);
        include($this->fullTemplatePath);
        $content = ob_get_clean();

        return $content;
    }

    private function findExistingTemplate($templateDirs)
    {
        foreach ($templateDirs as $dir) {
            $fullTemplatePath = $dir . '/' . $this->templatePath;
            if (file_exists($fullTemplatePath)) {
                $this->fullTemplatePath = $fullTemplatePath;
                return;
            }
        }

        throw new TemplateDoesNotExistException(
{'fraction_non_alphanumeric': 0.11878453038674033, 'fraction_numerical': 0.0, 'mean_word_length': 2.3632610939112486, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, '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': '6250892', 'n_tokens_mistral': 934, 'n_tokens_neox': 918, 'n_words': 184}