bootstrap.php 4.38 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
    $events('on', 'app.autoload', function ($autoloader) {
17 18
        $autoloader->addPsr4('GrEduLabs\\Application\\', __DIR__ . '/src');
    });
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
19

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

24 25 26 27 28
        // setup RedbeanPHP
        define('REDBEAN_MODEL_PREFIX', '');
        RedBeanPHP\R::setup(
            $container['settings']['db']['dsn'],
            $container['settings']['db']['user'],
29 30
            $container['settings']['db']['pass'],
            isset($container['settings']['db']['freeze']) ? $container['settings']['db']['freeze'] : true
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
        );

        $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) {
53 54 55 56
            $settings                     = $c['settings'];
            $nav                          = $settings['navigation'];
            $nav['main']['forum']['href'] = $settings['forum_url'];

57
            return new GrEduLabs\Application\Twig\Extension\Navigation(
58
                $nav,
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
                $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
85 86 87 88 89 90 91
        $container[GrEduLabs\Application\Middleware\AddCsrfToView::class] = function ($c) {
            return new GrEduLabs\Application\Middleware\AddCsrfToView(
                $c->get('view'),
                $c->get('csrf')
            );
        };

92 93 94 95 96 97
        $container['assets'] = function ($c) {
            $settings = $c['settings']['assets'];

            return new Knlv\Middleware\AssetsManager($settings);
        };

kanellov's avatar
kanellov committed
98
        $container[GrEduLabs\Application\Action\Index::class] = function ($c) {
99 100
            return new GrEduLabs\Application\Action\Index($c['view']);
        };
kanellov's avatar
kanellov committed
101 102 103
        $container[GrEduLabs\Application\Action\About::class] = function ($c) {
            return new GrEduLabs\Application\Action\About($c['view']);
        };
104 105
    });

106
    $events('on', 'app.bootstrap', function ($app, $container) {
107 108 109 110 111 112

        $app->add(function ($req, $res, $next) use ($container) {
            $settings           = $container->get('settings');
            $forumUrl           = $settings['forum_url'];
            $view               = $container->get('view');
            $view['_forum_url'] = $forumUrl;
113
            $view['_egkyklios_file'] = $settings['egkyklios_file'];
114 115 116 117

            return $next($req, $res);
        });

kanellov's avatar
kanellov committed
118 119
        $app->get('/', GrEduLabs\Application\Action\Index::class)->setName('index');
        $app->get('/about', GrEduLabs\Application\Action\About::class)->setName('about');
120
    });
121 122 123 124 125

    // low priority means add middleware last and for slim means run middleware first
    $events('on', 'app.bootstrap', function ($app, $container) {
        $app->add('assets');
    }, -1000);
126
};