<?php
final class Twig{
  public $data = array();
  private $registry;

  public function __construct($pregistry) {
  $this->registry = $pregistry;
    
  }

  public function fetch($filename){
    require_once DIR_SYSTEM . 'library/template/vendor/autoload.php';
    require_once DIR_SYSTEM . 'library/template/vendor/twig/twig/src/Loader/FilesystemLoader.php';
    require_once DIR_SYSTEM . 'library/template/vendor/twig/twig/src/Environment.php';
    require_once DIR_SYSTEM . 'library/template/vendor/twig/twig/src/Extension/DebugExtension.php';

    $loader = new Twig\Loader\FilesystemLoader(DIR_TEMPLATE);
    $twig = new Twig\Environment($loader, array(
        'autoescape' => false,
        'debug'      => true,
        'cache'      => (defined('AMBIENTE_TESTE')) ? false : DIR_CACHE, // Se for ambiente de testes, trabalha com cache desabilitado...
    ));
    
    $twig->addExtension(new Twig\Extension\DebugExtension());
    
    $twig->addGlobal('config', $this->registry->get('config')->getData());

    $output  =  $twig->render($filename, $this->data);

    return $output;
  }

  public function fetchString($filename, $string, $array){
    require_once DIR_SYSTEM . 'library/template/vendor/autoload.php';
    require_once DIR_SYSTEM . 'library/template/vendor/twig/twig/src/Environment.php';
    require_once DIR_SYSTEM . 'library/template/vendor/twig/twig/src/Loader/ArrayLoader.php';

    $loader = new Twig\Loader\ArrayLoader(array(
        $filename => $string,
    ));
    $twig = new Twig\Environment($loader, array(
        'autoescape' => false,
        //'cache'      => DIR_CACHE,
    ));

    $output =  $twig->render($filename, $array);

    return $output;
  }

  public function fetchStr($string, $data){
    require_once DIR_SYSTEM . 'library/template/vendor/autoload.php';
    require_once DIR_SYSTEM . 'library/template/vendor/twig/twig/src/Environment.php';
    require_once DIR_SYSTEM . 'library/template/vendor/twig/twig/src/Loader/ArrayLoader.php';

    $loader = new Twig\Loader\ArrayLoader(array(
      'index' => $string,
    ));

    $twig = new Twig\Environment($loader, array(
        //'autoescape' => true,
        //'cache'      => DIR_CACHE,
    ));
    
	  return $twig->render('index', $data);
  }




}
