-
Notifications
You must be signed in to change notification settings - Fork 0
/
FrontController.php
127 lines (111 loc) · 3.46 KB
/
FrontController.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
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
<?php
class FrontController
{
protected static $_instance;
protected $paths = array();
protected $layoutName;
protected $scope;
protected function __construct()
{
$root = realpath(dirname(__FILE__) . '/../');
$this->paths['root'] = $root;
$this->setLayoutName('layout');
$this->loadViewHelpers();
}
public static function getInstance()
{
if (is_null(self::$_instance)){
self::$_instance = new FrontController();
}
return self::$_instance;
}
public function renderPage($viewName=false)
{
Registry::clear();
$view = $this->loadView($viewName);
Registry::getNamespaceInstance('layout')->setVar('view', $view);
$layout = $this->loadLayout();
echo $layout;
}
protected function getPath($name, $filename=false)
{
if (isset($this->paths[$name])){
$path = $this->paths[$name];
} else {
$path = $this->paths['root'] . DIRECTORY_SEPARATOR . "$name";
}
return $filename ? $path . DIRECTORY_SEPARATOR . $filename : $path;
}
protected function loadView($viewName=false)
{
$this->setScope('views');
$uri = preg_replace('/\.html$/', '', trim($viewName ? $viewName : @$_SERVER['REDIRECT_URL'], '/'));
if (isset($_REQUEST['viewmenu']) || !$view = $this->loadPartial($uri, '.php')){
$anchors = array_map(function($name){
return '<a href="/' . $name . '">' . $name . '</a>';
}, $this->getViewNames());
$view = '<ul class="view-menu"><li>' . implode('</li><li>', $anchors) . '</li></ul>';
}
return $view;
}
protected function loadLayout()
{
$this->setScope('layout');
$layout = $this->getLayoutName();
return $this->loadPartial($layout, '.php');
}
protected function loadPartial($partialName, $ext='.phtml')
{
$file = $this->getPath($this->getScope(), "$partialName$ext");
if (file_exists($file) && is_file($file)) {
ob_start();
include $file;
return ob_get_clean();
}
return false;
}
public function setLayoutName($slug)
{
$this->layoutName = $slug;
}
public function getLayoutName()
{
return $this->layoutName;
}
protected function loadViewHelpers()
{
foreach(glob($this->getPath('views\\helpers', '*.php')) as $file) {
require_once $file;
}
}
public function getViewNames()
{
$views = array();
foreach(glob($this->getPath('views\\*.php')) as $file) {
$views[] = preg_replace('/.php$/', '', basename($file));
}
return $views;
}
public function setScope($scope)
{
$this->scope = $scope;
}
public function getScope()
{
return $this->scope;
}
public function __get($var)
{
return Registry::getNamespaceInstance($var);
}
public function __call($method, $args)
{
$class = '\\ViewHelper\\' . ucfirst($method);
if(class_exists($class)){
$helper = call_user_func(array($class, 'getInstance'));
return call_user_func_array(array($helper, 'invoke'), $args);
}
throw new Exception('There is no view helper called ' . $method);
}
}
?>