-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.php
More file actions
81 lines (58 loc) · 2.13 KB
/
app.php
File metadata and controls
81 lines (58 loc) · 2.13 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
<?php
require __DIR__."/vendor/autoload.php";
use twiger\Router as Router;
use twiger\Twiger as Twiger;
//Including all Controllers
foreach (glob(__DIR__.'/src/control/*.php') as $filename){
require $filename;
}
$routes = Spyc::YAMLLoad('app/config/routing.yml');
$constants = Spyc::YAMLLoad('app/config/constants.yml');
$config = Spyc::YAMLLoad('app/config/config.yml');
$requestUri = $_SERVER['REQUEST_URI'];
$router = new Router($routes);
$route = $router->handle($requestUri);
if (!is_null($route) && !isset($route['params']))
$route['params'] = array();
if (!is_null($route) && !isset($route['route_params']))
$route['route_params'] = array();
$constants['route'] = $route;
$assets = new Twig_SimpleFunction('assets', function ($path) {
$config = Spyc::YAMLLoad('app/config/config.yml');
if(isset($config['assets']))
return '/'.$config['assets'].'/'.$path;
else
return '/'.$path;
});
$path = new Twig_SimpleFunction('path', function ($route, $args = null) {
$routes = Spyc::YAMLLoad('app/config/routing.yml');
if( isset($routes[$route]) )
$route = $routes[$route];
else
throw new Twig_Error_Path('No route found for '.$route);
$path = $route['pattern'];
if (!empty($args))
foreach ($args as $key => $value)
$path = preg_replace('/{'.$key.'}/', $value, $path);
return $path;
});
if (!is_null($route)){
if (isset($route['controller'])) {
$controlling = explode('::',$route['controller']);
$controller = $controlling[0];
$controller = 'control\\'.$controller;
$controller = new $controller($config, array_merge($constants, $route['params']) );
$controller->addFunctions(array($assets, $path));
$method = $controlling[1];
echo call_user_func_array(array($controller, $method), $route['route_params']);
}elseif (isset($route['template'])) {
$twiger = new Twiger( array_merge($constants, $route['params']) );
$twiger->addFunctions(array($assets, $path));
echo $twiger->render($route['template'].'.html.twig', $route['route_params'] );
}
}else{
$twiger = new Twiger();
$twiger->addFunctions(array($assets, $path));
echo $twiger->render('404.html.twig', array('route' => $requestUri));
http_response_code(404);
}