-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbootstrap.php
101 lines (79 loc) · 2.66 KB
/
bootstrap.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
use \Slim\App as Slim;
use \App\System\Session;
use \App\System\App;
// Set internal encoding to what should be the default
mb_internal_encoding("UTF-8");
/**
* Config array for Slim framework
* Define the basic constants that the application needs
*/
$config = require dirname(__FILE__) . '/conf.php';
define('APP_HTDOCS_PATH', APP_ROOT . 'htdocs/');
define('APP_TEMPLATES_PATH', APP_ROOT . 'templates/');
require APP_ROOT . 'vendor/autoload.php';
$app = new Slim(['settings' => $config]);
App::$slim = $app;
$container = $app->getContainer();
require APP_ROOT. 'i18n.php';
/**
* Add resources to the app. These resources will be needed at any point throughout the execution.
*/
$container['session'] = function () use ($app) {
return new Session($app);
};
$container['view'] = function ($container) {
$cache = '../tmp/cache';
if ($container->get("settings")["mode"] == "development") {
$cache = false;
}
$view = new \Slim\Views\Twig(APP_TEMPLATES_PATH, [
'debug' => true,
'cache' => $cache
]);
$view->addExtension(new Twig_Extensions_Extension_I18n());
$view->addExtension(new \Slim\Views\TwigExtension(
$container['router'],
$container['request']->getUri()
));
if ($container->get("settings")["mode"] == "development") {
$view->addExtension(new Twig_Extension_Debug());
}
return $view;
};
$container['db'] = function ($container) {
$capsule = new \Illuminate\Database\Capsule\Manager;
$capsule->addConnection($container['settings']['mysql']);
$capsule->setAsGlobal();
$capsule->bootEloquent();
// for query debugging/printing
//$capsule::connection()->enableQueryLog();
//p(\Illuminate\Database\Capsule\Manager::getQueryLog());exit;
return $capsule;
};
/*
* Add some global vars before rendering each template
*/
$app->add(function ($request, $response, $next) use ($app) {
$view = App::container()->get("view");
$view->getEnvironment()->addGlobal('isAjax', $app->isAjax);
//$response = $next($request, $response);return $response;
try {
$response = $next($request, $response);
} catch (\Exception $e)
{
if ($e->getCode() == 11 && !$app->isAjax) { // Authentication failed
App::redirect("/login");
}
if ($app->isAjax) {
$response = new \stdClass();
$response->error = $e->getCode();
$response->message = $e->getMessage();
\App\System\Utils::jsonResponse($response);
} else if (App::settings()["mode"] == "development") {
throw $e;
}
}
return $response;
});
App::container()->get("db")->getConnection();