-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMicroMuffin.php
More file actions
163 lines (138 loc) · 4.3 KB
/
MicroMuffin.php
File metadata and controls
163 lines (138 loc) · 4.3 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
/**
* Created by JetBrains PhpStorm.
* User: Mathieu
* Date: 14/07/13
* Time: 15:18
* To change this template use File | Settings | File Templates.
*/
namespace Lib;
use Lib\Router\Route;
use Lib\Router\Router;
class MicroMuffin
{
const ENV_DEV = 0;
const ENV_PROD = 1;
/** @var MicroMuffin */
private static $instance = null;
/** @var Route */
private $route;
/** @var Controller */
private $controller;
/** @var string */
private $action;
private function init()
{
require_once('autoloader.php');
require_once('../config/config.php');
require_once('config.php');
/*
* WARNING ! Do not call Autoloader::register before the three includes before
*/
Autoloader::register();
require_once('../app/routes.php');
require_once('../' . VENDORS_DIR . 'Twig/Autoloader.php');
\Twig_Autoloader::register();
}
private function route()
{
//Route determination
$url = Tools::getParam("url", null);
Log::write('MicroMuffin::route : url called is ' . $url);
$route = Router::get(!is_null($url) ? $url : "");
if (!is_null($route))
$this->route = $route;
else
{
header("HTTP/1.0 404 Not Found");
$e = new \Error("Page not found", "The page you are looking for doesn't exist.");
$e->display();
}
}
private function checkRoute()
{
$className = $this->route->getController() . 'Controller';
if (class_exists($className))
{
$this->controller = new $className();
if (method_exists($this->controller, $this->route->getAction()))
$this->action = $this->route->getAction();
else
{
//Undefined action
$e = new \Error('Undefined action', 'Action ' . $this->route->getAction() . ' doesn\'t exist on ' . $this->route->getController() . ' controller.');
$e->display();
}
}
else
{
//Undefined controller
$e = new \Error('Undefined controller', $this->route->getController() . ' doesn\'t exist.');
$e->display();
}
}
private function execute()
{
if (method_exists($this->controller, 'before_filter'))
$this->controller->before_filter($this->route->getParameters());
$action = $this->action;
$this->controller->$action($this->route->getParameters());
//View displaying
$render = $this->controller->getRender();
if ($render != "false")
{
Internationalization::init();
$twig_options = array('cache' => false, 'autoescape' => false, 'strict_variables' => true);
$loader = new \Twig_Loader_Filesystem('../' . VIEW_DIR . strtolower($this->route->getController()));
$twig = new \Twig_Environment($loader, $twig_options);
$twig->addFilter("tr", new \Twig_Filter_Function("\\Lib\\Internationalization::translate"));
$twig->addFilter("url", new \Twig_Filter_Function("\\Lib\\Tools::sanitizeForUrl"));
$page = $twig->render(($render == "true" ? $this->action : $render) . ".html.twig", $this->controller->getVariables());
//Base layout execution and displaying
$layout = $this->controller->getRenderLayout();
if ($layout != "false")
{
$loader = new \Twig_Loader_Filesystem('../' . VIEW_DIR . 'base');
$twig = new \Twig_Environment($loader, $twig_options);
$twig->addFilter("tr", new \Twig_Filter_Function("\\Lib\\Internationalization::translate"));
$twig->addFilter("url", new \Twig_Filter_Function("\\Lib\\Tools::sanitizeForUrl"));
$base = new \BaseController();
$base->$layout();
$params = $base->getVariables();
$params = array_merge($params, $this->controller->getLayoutVariables());
$params['_page'] = $page;
echo $twig->render($layout . ".html.twig", $params);
}
else
echo $page;
}
}
private function __construct()
{
$this->controller = null;
$this->route = null;
$this->action = null;
}
/**
* @return Route
*/
public function getRoute()
{
return $this->route;
}
/**
* @return MicroMuffin
*/
public static function getInstance()
{
return self::$instance;
}
public static function run()
{
self::$instance = new MicroMuffin();
self::$instance->init();
self::$instance->route();
self::$instance->checkRoute();
self::$instance->execute();
}
}