<commit_before><?php
/**
 * Twig Template class
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */

namespace Skeleton\Template\Twig;

class Twig {
	/**
	 * Local Twig instance
	 *
	 * @var \Twig_Environment $twig
	 */
	private $twig = null;

	/**
	 * Variables
	 *
	 * @access private
	 * @var array $variables
	 */
	private $variables = [];

	/**
	 * Variables to add to the environment
	 *
	 * @access private
	 * @var array $environment
	 */
	private $environment = [];

	/**
	 * Translation
	 *
	 * @access private
	 * @var Translation $translation
	 */
	private $translation = null;

	/**
	 * Is i18n available
	 *
	 * @access private
	 * @var bool $i18n_available
	 */
	private $i18n_available = false;

	/**
	 * Constructor
	 *
	 * @param string $name
	 * @param Language $language
	 */
	public function __construct() {
		\Twig_Autoloader::register();
		$chain_loader = new \Twig_Loader_Chain([
			new \Twig_Loader_Filesystem(),
			new \Twig_Loader_String()
		]);

		$this->twig = new \Twig_Environment(
			$chain_loader,
			[
				'cache' => Config::$cache_directory,
				'auto_reload' => true,
				'debug' => Config::$debug
			]
		);

		if (class_exists('\\Skeleton\\I18n\\Translation')) {
			$this->i18n_available = true;
			$this->twig->addExtension(new \Skeleton\I18n\Template\Twig\Extension\Tigron());
		}

		if (Config::$debug === true) {
			$this->twig->addExtension(new \Twig_Extension_Debug());
		}

		$this->twig->addExtension(new \Skeleton\Template\Twig\Extension\Common());
		$this->twig->addExtension(new \Twig_Extension_StringLoader());
		$this->twig->addExtension(new \Twig_Extensions_Extension_Text());

		$this->twig->getExtension('core')->setNumberFormat(2, '.', '');
	}

	/**
	 * Set Template dir
	 *
	 * @access public
	 * @param string $directory
	 */
	public function set_template_directory($directory) {
		$loader = new \Twig_Loader_Filesystem($directory);
		$this->twig->setLoader($loader);
	}

	/**
	 * Add an environment variable
	 *
	 * @param string $key
	 * @param mixed $value
	 */
	public function add_environment($key, $value) {
		$this->environment[$key] = $value;
	}

	/**
	 * Assign variables to the template
	 *
	 * @param string $key
	 * @param mixed $value
	 */
	public function assign($key, $value) {
		$this->variables[$key] = $value;
	}

	/**
	 * Set translation
	 *
	 * @access public
	 * @param Translation $translation
	 */
	public function set_translation(\Skeleton\I18n\Translation $translation) {
		if ($this->i18n_available === true) {
			$this->translation = $translation;
		} else {
			throw new \Exception('Translation is not available, class not found');
		}
	}

	/**
	 * Render
	 *
	 * @access public
	 * @param string $template
	 * @return string $html
	 */
	public function render($template) {
		$environment = [
			'post' => $_POST,
			'get' => $_GET,
			'cookie' => $_COOKIE,
			'server' => $_SERVER,
		];

		$environment = array_merge($environment, $this->environment);

		if ($this->i18n_available === true and $this->translation !== null) {
			$environment['translation'] = $this->translation;
			$environment['language'] = $this->translation->language;
		}

		if (isset($_SESSION)) {
			$environment['session'] = $_SESSION;
		}

		$this->twig->addGlobal('env', $environment);
		return $this->twig->render($template, $this->variables);
	}
}<commit_msg>add template directories with namespaces<commit_after><?php
/**
 * Twig Template class
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */

namespace Skeleton\Template\Twig;

class Twig {
	/**
	 * Local Twig instance
	 *
	 * @var \Twig_Environment $twig
	 */
	private $twig = null;

	/**
	 * Variables
	 *
	 * @access private
	 * @var array $variables
	 */
	private $variables = [];

	/**
	 * Variables to add to the environment
	 *
	 * @access private
	 * @var array $environment
	 */
	private $environment = [];

	/**
	 * Translation
	 *
	 * @access private
	 * @var Translation $translation
	 */
	private $translation = null;

	/**
	 * Is i18n available
	 *
	 * @access private
	 * @var bool $i18n_available
	 */
	private $i18n_available = false;

	/**
	 * filesystem_loader
	 *
	 * @access private
	 * @var Twig_Loader_Filesystem $filesystem
	 */
	private $filesystem_loader = null;

	/**
	 * Constructor
	 *
	 * @param string $name
	 * @param Language $language
	 */
	public function __construct() {
		\Twig_Autoloader::register();
		$chain_loader = new \Twig_Loader_Chain([
			new \Twig_Loader_Filesystem(),
			new \Twig_Loader_String()
		]);

		$this->twig = new \Twig_Environment(
			$chain_loader,
			[
				'cache' => Config::$cache_directory,
				'auto_reload' => true,
				'debug' => Config::$debug
			]
		);

		if (class_exists('\\Skeleton\\I18n\\Translation')) {
			$this->i18n_available = true;
			$this->twig->addExtension(new \Skeleton\I18n\Template\Twig\Extension\Tigron());
		}

		if (Config::$debug === true) {
			$this->twig->addExtension(new \Twig_Extension_Debug());
		}

		$this->twig->addExtension(new \Skeleton\Template\Twig\Extension\Common());
		$this->twig->addExtension(new \Twig_Extension_StringLoader());
		$this->twig->addExtension(new \Twig_Extensions_Extension_Text());

		$this->twig->getExtension('core')->setNumberFormat(2, '.', '');
	}

	/**
	 * Set Template dir
	 *
	 * @access public
	 * @param string $directory
	 */
	public function add_template_directory($directory, $namespace = null) {
		if ($this->filesystem_loader === null) {
			$this->filesystem_loader = new \Twig_Loader_Filesystem($directory);
		}

		if ($namespace === null) {
			$this->filesystem_loader->addPath($directory);
		} else {
			$this->filesystem_loader->addPath($directory, $namespace);
		}
	}

	/**
	 * Add an environment variable
	 *
	 * @param string $key
	 * @param mixed $value
	 */
	public function add_environment($key, $value) {
		$this->environment[$key] = $value;
	}

	/**
	 * Assign variables to the template
	 *
	 * @param string $key
	 * @param mixed $value
	 */
	public function assign($key, $value) {
		$this->variables[$key] = $value;
	}

	/**
	 * Set translation
	 *
	 * @access public
	 * @param Translation $translation
	 */
	public function set_translation(\Skeleton\I18n\Translation $translation) {
		if ($this->i18n_available === true) {
			$this->translation = $translation;
		} else {
			throw new \Exception('Translation is not available, class not found');
		}
	}

	/**
	 * Render
	 *
	 * @access public
	 * @param string $template
	 * @return string $html
	 */
	public function render($template) {
		$environment = [
			'post' => $_POST,
			'get' => $_GET,
			'cookie' => $_COOKIE,
			'server' => $_SERVER,
		];

		$environment = array_merge($environment, $this->environment);

		if ($this->i18n_available === true and $this->translation !== null) {
			$environment['translation'] = $this->translation;
			$environment['language'] = $this->translation->language;
		}

		if (isset($_SESSION)) {
			$environment['session'] = $_SESSION;
		}
		$this->twig->setLoader($this->filesystem_loader);
		$this->twig->addGlobal('env', $environment);
		return $this->twig->render($template, $this->variables);
	}
}
{'fraction_non_alphanumeric': 0.16908962597035992, 'fraction_numerical': 0.005363443895553988, 'mean_word_length': 3.5569131832797427, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 100, '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': '911480', 'n_tokens_mistral': 2614, 'n_tokens_neox': 2276, 'n_words': 479}