<?php
// DIC configuration
use Symfony\Component\Yaml\Yaml;
$container = $app->getContainer();

$container['flash'] = function($c){
    return new \Slim\Flash\Messages();
};

$container['renderer'] = function ($c) {
    $settings = $c->get('settings')['renderer'];

    $view = new Slim\Views\Twig($settings['template_path'],[
        'cache' => $settings['cache_path'],
        

    ]);

    $view->addExtension(new Knlv\Slim\Views\TwigMessages(
      $c->get('flash')
    ));
    $noticias = Yaml::parseFile(__DIR__."/../data/noticias.yml");
    $sponsors = Yaml::parseFile(__DIR__."/../data/sponsors.yml");
    $view->getEnvironment()->addGlobal('noticias',$noticias);
    $view->getEnvironment()->addGlobal('sponsors',$sponsors);

    return $view;
};

$container['db'] = function ($c) {
    $dbconf = $c->get('settings')['db'];
    try{
        $pdo = new PDO('sqlite:'.$dbconf['path']);
        $pdo->setAttribute( \PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION );
        $pdo->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC);
        $db = new DB($pdo);
        if(!file_exists($dbconf['load_schema'])){
          $db->loadSchema($dbconf['schema']);
          touch($dbconf['load_schema']);
        }

    }
    catch(\PDOException $e){
        echo $e->getMessage();
    }
    return $db;
};
// monolog
$container['logger'] = function ($c) {
    $settings = $c->get('settings')['logger'];
    $logger = new Monolog\Logger($settings['name']);
    $logger->pushProcessor(new Monolog\Processor\UidProcessor());
    $logger->pushHandler(new Monolog\Handler\StreamHandler($settings['path'], $settings['level']));
    return $logger;
};

/*$container['mailer'] =function ($c){

};*/
