<?php

namespace ProudNerds\Core;

use Timber\Menu;
use Timber\Site;
use Twig\Environment;
use Twig\Extension;

class Timber {
    /**
     * Timber constructor.
     */
    public function __construct() {
        $this->init();
    }

    /**
     * Setup Twig/Timber templating engine.
     */
    private function init() {
        new \Timber\Timber();

        // Declare the location where the Twig template files are
        \Timber\Timber::$dirname = array('views');

        // We don't like Twig's auto escape feature
        \Timber\Timber::$autoescape = false;

        add_filter('timber/context', array($this, 'addToContext'));
        add_filter('timber/twig', array($this, 'addToTwig'));
    }

    /**
     * Add default functionality to Timber context.
     *
     * @param array $context context['this'] being the Twig's {{ this }}.
     */
    public function addToContext(array $context): array {
        $context['menu'] = new Menu();
        $context['site'] = new Site();

        return $context;
    }

    /**
     * Add String Loader extension.
     *
     * @param Environment $twig Twig environment to add extensions to.
     */
    public function addToTwig(Environment $twig): Environment {
        $twig->addExtension(new Extension\StringLoaderExtension());

        return $twig;
    }
}