<?php
// DIC configuration

$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(
      new Slim\Flash\Messages() 
    ));
    return $view;
};

$container['db'] = function ($c) {
    $db = $c->get('settings')['db'];
    try{
        $pdo = new PDO('sqlite:'.$db['path']);
        $pdo->setAttribute( \PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION );
        $pdo->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC);
    }
    catch(\PDOException $e){
        echo $e->getMessage();
    }

    return $pdo;
};
// 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;
};
