-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathApp.php
120 lines (94 loc) · 2.55 KB
/
App.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
<?php
/**
* Description of App
*
* @author kortekim twitter
*/
class App {
static $confdir = __DIR__;
static $conffile = 'config.json.php';
public $env = 'localhost';
public $url = null;
public $server_url = null;
public $root = null;
public $server_root = null;
public $pointer = null;
public $htmlbuffer = '';
public function __construct($includes = array(), $confdir = false) {
if($confdir)
$this::$confdir = $confdir;
else
$this::$confdir .= '/';
$this->checkIfLive();
$url = $_SERVER['PHP_SELF'];
if(strpos($url, '.php')) {
$url = explode('.php', $url);
array_pop($url);
$url = implode('.php', $url);
$explode = explode('/', $url);
$end = end($explode);
$url = str_replace($end, '', $url);
}
$config = array(
'url' => $url,
'server_url' => $url,
'root' => __DIR__.'/',
'server_root' => __DIR__.'/'
);
$parsed = $this->parseConfig();
if(isset($parsed->{$this->env}))
$config = array_merge($config, (array) $parsed->{$this->env});
foreach($config as $key => $value)
$this->{$key} = $value;
if($includes)
$this->includer($includes);
}
public function checkIfLive() {
if(!in_array($_SERVER['REMOTE_ADDR'], array('127.0.0.1', '::1')))
$this->env = 'live';
}
public function parseConfig() {
$file = $this::$confdir.$this::$conffile;
$config = array();
if(file_exists($file))
$config = json_decode($this->get($file));
return $config;
}
public function get($path, $variablesx = array()) {
$path = $this->pointer.$path;
if(!strpos($path, '.php'))
$path = $path.'.php';
if(!file_exists($path)) {
$path = $this->pointer.'../404.php';
}
ob_start();
extract($variablesx);
$app = $this;
include $path;
$output = ob_get_clean();
$backtrace = debug_backtrace();
if($backtrace[0]['file'] == $app->server_root.'index.php')
$this->htmlbuffer .= "<!-- Start ".$path." -->\n".$output."\n<!-- End ".$path." -->\n";
return $output;
}
public function includer($includes = array(), $directory = false) {
foreach ($includes as $dir) {
$this->autoload($directory . $dir);
}
}
public function autoload($file){
if(is_dir($file)) {
foreach(glob($file.'/*') as $file) {
$this->autoload($file);
}
}else{
if(!strpos($file, '.php'))
$file = $file.'.php';
if(is_file($file)) {
$app = $this;
include_once $file;
}
}
}
}
?>