<commit_before><?php

/**
 * Twigger
 *
 * PHP version 5
 *
 * @category  Library
 * @package   PyritePHP
 * @author    <NAME> <<EMAIL>>
 * @copyright 2016 <NAME>
 * @license   https://opensource.org/licenses/MIT  MIT
 * @link      https://github.com/vphantom/pyrite-php
 */

/**
 * Twigger class
 *
 * @category  Library
 * @package   PyritePHP
 * @author    <NAME> <<EMAIL>>
 * @copyright 2016 <NAME>
 * @license   https://opensource.org/licenses/MIT  MIT
 * @link      https://github.com/vphantom/pyrite-php
 */
class Twigger
{
    private static $_twig;
    private static $_title = '';
    private static $_status = 200;
    private static $_template;
    private static $_safeBody = '';
    private static $_lang = 'en';  // Could be '', paranoid precaution

    /**
     * Initialize wrapper around Twig templating and display headers
     *
     * @return null
     */
    public static function startup()
    {
        global $PPHP;
        self::$_lang = $PPHP['config']['global']['default_lang'];
        $tplBase = __DIR__ . '/../templates';
        $twigLoader = new \Twig_Loader_Filesystem();

        // Don't choke if language from URL is bogus
        try {
            $twigLoader->addPath($tplBase . '/' . self::$_lang);
        } catch (Exception $e) {
        };

        // Be nice, don't even choke if templates aren't sorted by language
        if (self::$_lang !== $PPHP['config']['global']['default_lang']) {
            try {
                $twigLoader->addPath($tplBase . '/' . $PPHP['config']['global']['default_lang']);
            } catch (Exception $e) {
            };
        };

        $twigLoader->addPath($tplBase);
        $twig = new \Twig_Environment(
            $twigLoader,
            array(
                // 'cache' => __DIR__ . '/var/twig_cache',
                'autoescape' => true,
            )
        );
        $twig->addFunction(
            new \Twig_SimpleFunction(
                'grab', function () {
                    return array_pop(call_user_func_array('trigger', func_get_args()));
                }
            )
        );
        $twig->addFunction(
            new \Twig_SimpleFunction(
                'pass', function () {
                    return array_pop(call_user_func_array('trigger', func_get_args())) !== false;
                }
            )
        );
        $twig->addFunction(
            new \Twig_SimpleFunction(
                'filter', function () {
                    return call_user_func_array('filter', func_get_args());
                }
            )
        );
        $twig->addFunction(
            new \Twig_SimpleFunction(
                'title', function () {
                    return call_user_func_array('self::title', func_get_args());
                }
            )
        );

        // Load utilities globally
        $twig->addGlobal('lib', $twig->loadTemplate('lib'));

        self::$_twig = $twig;

        try {
            self::$_template = $twig->loadTemplate('layout.html');

            if (self::$_status !== 200) {
                http_response_code(self::$_status);
            };
            echo self::$_template->renderBlock(
                'head',
                array(
                    'session' => $_SESSION,
                    'req' => grab('request'),
                    'post' => $_POST
                )
            );
        } catch (Exception $e) {
            echo $e->getMessage();
        };
        flush();
        ob_start();
    }

    /**
     * Clean up content capture and display main template
     *
     * @return null
     */
    public static function shutdown()
    {
        $body = ob_get_contents();
        ob_end_clean();
        try {
            echo self::$_template->renderBlock(
                'body',
                array(
                    'title' => self::$_title,
                    'body' => self::$_safeBody,
                    'stdout' => $body,
                    'session' => $_SESSION,
                    'req' => grab('request'),
                    'post' => $_POST
                )
            );
        } catch (Exception $e) {
            echo $e->getMessage();
        };
    }

    /**
     * Set current language
     *
     * @param string $code Two-letter language code<commit_msg>Don't echo render, display (Twig)<commit_after><?php

/**
 * Twigger
 *
 * PHP version 5
 *
 * @category  Library
 * @package   PyritePHP
 * @author    <NAME> <<EMAIL>>
 * @copyright 2016 <NAME>
 * @license   https://opensource.org/licenses/MIT  MIT
 * @link      https://github.com/vphantom/pyrite-php
 */

/**
 * Twigger class
 *
 * @category  Library
 * @package   PyritePHP
 * @author    <NAME> <<EMAIL>>
 * @copyright 2016 <NAME>
 * @license   https://opensource.org/licenses/MIT  MIT
 * @link      https://github.com/vphantom/pyrite-php
 */
class Twigger
{
    private static $_twig;
    private static $_title = '';
    private static $_status = 200;
    private static $_template;
    private static $_safeBody = '';
    private static $_lang = 'en';  // Could be '', paranoid precaution

    /**
     * Initialize wrapper around Twig templating and display headers
     *
     * @return null
     */
    public static function startup()
    {
        global $PPHP;
        self::$_lang = $PPHP['config']['global']['default_lang'];
        $tplBase = __DIR__ . '/../templates';
        $twigLoader = new \Twig_Loader_Filesystem();

        // Don't choke if language from URL is bogus
        try {
            $twigLoader->addPath($tplBase . '/' . self::$_lang);
        } catch (Exception $e) {
        };

        // Be nice, don't even choke if templates aren't sorted by language
        if (self::$_lang !== $PPHP['config']['global']['default_lang']) {
            try {
                $twigLoader->addPath($tplBase . '/' . $PPHP['config']['global']['default_lang']);
            } catch (Exception $e) {
            };
        };

        $twigLoader->addPath($tplBase);
        $twig = new \Twig_Environment(
            $twigLoader,
            array(
                // 'cache' => __DIR__ . '/var/twig_cache',
                'autoescape' => true,
            )
        );
        $twig->addFunction(
            new \Twig_SimpleFunction(
                'grab', function () {
                    return array_pop(call_user_func_array('trigger', func_get_args()));
                }
            )
        );
        $twig->addFunction(
            new \Twig_SimpleFunction(
                'pass', function () {
                    return array_pop(call_user_func_array('trigger', func_get_args())) !== false;
                }
            )
        );
        $twig->addFunction(
            new \Twig_SimpleFunction(
                'filter', function () {
                    return call_user_func_array('filter', func_get_args());
                }
            )
        );
        $twig->addFunction(
            new \Twig_SimpleFunction(
                'title', function () {
                    return call_user_func_array('self::title', func_get_args());
                }
            )
        );

        // Load utilities globally
        $twig->addGlobal('lib', $twig->loadTemplate('lib'));

        self::$_twig = $twig;

        try {
            self::$_template = $twig->loadTemplate('layout.html');

            if (self::$_status !== 200) {
                http_response_code(self::$_status);
            };
            self::$_template->displayBlock(
                'head',
                array(
                    'session' => $_SESSION,
                    'req' => grab('request'),
                    'post' => $_POST
                )
            );
        } catch (Exception $e) {
            echo $e->getMessage();
        };
        flush();
        ob_start();
    }

    /**
     * Clean up content capture and display main template
     *
     * @return null
     */
    public static function shutdown()
    {
        $body = ob_get_contents();
        ob_end_clean();
        try {
            self::$_template->displayBlock(
                'body',
                array(
                    'title' => self::$_title,
                    'body' => self::$_safeBody,
                    'stdout' => $body,
                    'session' => $_SESSION,
                    'req' => grab('request'),
                    'post' => $_POST
                )
            );
        } catch (Exception $e) {
            echo $e->getMessage();
        };
    }

    /**
     * Set current language
     *
     * @param string $code Two-letter language code
{'fraction_non_alphanumeric': 0.13943202979515829, 'fraction_numerical': 0.0034916201117318434, 'mean_word_length': 1.4671260407694515, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 69, 'https://': 8, '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': '1513155', 'n_tokens_mistral': 2443, 'n_tokens_neox': 2318, 'n_words': 489}