bootstrap.php 3.36 KB
Newer Older
1 2
<?php
/**
3
 * gredu_labs.
4 5
 * 
 * @link https://github.com/eellak/gredu_labs for the canonical source repository
6
 *
7 8 9 10 11 12 13
 * @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/)
 * @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
 */

return function (Slim\App $app) {

    $container = $app->getContainer();
14
    $events    = $container['events'];
15

16 17 18
    $events('on', 'app.autoload', function ($stop, $autoloader) {
        $autoloader->addPsr4('GrEduLabs\\Application\\', __DIR__ . '/src');
    });
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
19

20
    $events('on', 'app.services', function ($stop, $container) {
21 22 23
        session_name('GrEduLabs');
        session_start();

24 25 26 27 28 29 30
        // setup RedbeanPHP
        define('REDBEAN_MODEL_PREFIX', '');
        RedBeanPHP\R::setup(
            $container['settings']['db']['dsn'],
            $container['settings']['db']['user'],
            $container['settings']['db']['pass']
        );
kanellov's avatar
kanellov committed
31
        RedBeanPHP\R::freeze();
32
//        RedBeanPHP\R::debug( true );
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

        $container['view'] = function ($c) {
            $settings = $c['settings'];
            $view     = new Slim\Views\Twig(
                $settings['view']['template_path'],
                $settings['view']['twig']
            );
            $view->addExtension(new Slim\Views\TwigExtension(
                $c['router'],
                $c['request']->getUri()
            ));

            $view->addExtension($c[GrEduLabs\Application\Twig\Extension\Navigation::class]);
            $view->addExtension(new Knlv\Slim\Views\TwigMessages(
                $c['flash']
            ));

            return $view;
        };

        $container[GrEduLabs\Application\Twig\Extension\Navigation::class] = function ($c) {
            return new GrEduLabs\Application\Twig\Extension\Navigation(
                $c['settings']['navigation'],
                $c['router'],
                $c['request']
            );
        };

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

        $container['logger'] = function ($c) {
            $settings = $c['settings'];
            $logger   = new Monolog\Logger($settings['logger']['name']);
            $logger->pushProcessor(new Monolog\Processor\UidProcessor());
            $logger->pushHandler(new Monolog\Handler\RotatingFileHandler(
                $settings['logger']['path'],
                $settings['logger']['max_files'],
                Monolog\Logger::INFO
            ));

            return $logger;
        };

        $container['csrf'] = function ($c) {
            return new \Slim\Csrf\Guard();
        };

Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
82 83 84 85 86 87 88
        $container[GrEduLabs\Application\Middleware\AddCsrfToView::class] = function ($c) {
            return new GrEduLabs\Application\Middleware\AddCsrfToView(
                $c->get('view'),
                $c->get('csrf')
            );
        };

89 90 91 92 93 94
        $container['GrEduLabs\\Application\\Action\\Index'] = function ($c) {
            return new GrEduLabs\Application\Action\Index($c['view']);
        };
    });

    $events('on', 'app.bootstrap', function ($stop, $app, $container) {
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
95 96 97 98 99
        foreach ($container['router']->getRoutes() as $route) {
            if ('user.login' === $route->getName()) {
                $route->add('csrf');
                break;
            }
100
        }
101

102 103
        $app->get('/', 'GrEduLabs\\Application\\Action\\Index')->setName('index');
    });
104
};