/
Loader.class.inc.php
194 lines (161 loc) · 4.33 KB
/
Loader.class.inc.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?
final class Loader
{
private $root;
private $is_live;
private $included_files = array();
private static $instance;
private function __construct()
{
$this->is_live = (substr($_SERVER['HTTP_HOST'], 0, 4) !== 'dev.');
return $this;
}
public static function instance()
{
if(!isset(self::$instance))
self::$instance = new Loader();
return self::$instance;
}
private function get_root()
{
if(!isset($this->root))
{
$current_directory = dirname(__FILE__);
$current_directory = substr($current_directory, 0, -7);
$this->root = $current_directory;
}
return $this->root;
}
private function get_delimiter()
{
return (true || $this->is_live) ? '/' : '\\';
}
private function check_delimiters($path)
{
return (true || $this->is_live) ? $path : str_replace('/', '\\', $path);
}
private static function get_class_name($path)
{
$path_array = explode('/', $path);
return array_pop($path_array);
}
private static function get_extension($type)
{
switch($type)
{
case 'collector' :
case 'controller' :
case 'model' :
case 'module' :
case 'router' :
case 'utility' :
$extension = '.class.inc.php';
break;
case 'view' :
$extension = '.tpl.php';
break;
}
return $extension;
}
public static function getImagePath($type, $file)
{
if($type == 'photo')
$type = 'photo/processed';
$path = self::instance()->get_root();
$path .= 'images';
$path .= self::instance()->get_delimiter();
$path .= $type;
$path .= self::instance()->get_delimiter();
$path .= self::instance()->check_delimiters($file);
return $path;
}
private static function get_path($type, $file)
{
$path = self::instance()->get_root();
$path .= $type;
$path .= self::instance()->get_delimiter();
$path .= self::instance()->check_delimiters($file);
$path .= self::get_extension($type);
return $path;
}
private function get_included_files()
{
return $this->included_files;
}
private function add_included_file($path)
{
$this->included_files[] = $path;
}
public static function load($type, $files, $data = array())
{
foreach((array) $files as $file)
{
$file_path = self::instance()->get_path($type, $file);
if(in_array($file_path, self::instance()->get_included_files()) && $type !== 'view')
continue;
if(!file_exists($file_path))
Debugger::logMessage("Requested file does not exist: {$type}, {$file}");
self::instance()->add_included_file($file_path);
switch($type)
{
case 'images' :
case 'scripts' :
case 'styles' :
echo file_get_contents($file_path);
break;
case 'view' :
extract($data);
include($file_path);
break;
default :
include_once($file_path);
break;
}
}
}
private static function create_reflection_class($file)
{
$class_name = self::instance()->get_class_name($file);
return new ReflectionClass($class_name);
}
public static function loadInstance($type, $file)
{
self::load($type, $file);
$reflectionObject = self::create_reflection_class($file);
if(
$reflectionObject->hasMethod('instance') &&
$reflectionObject->getMethod('instance')->isStatic())
{
return $reflectionObject->getMethod('instance')->invoke(null);
}
trigger_error("Requested class cannot be instance'd: {$type}, {$file}");
}
public static function loadNew($type, $file, $data = array())
{
self::load($type, $file);
$reflectionObject = self::create_reflection_class($file);
if($reflectionObject->hasMethod('__construct'))
return $reflectionObject->newInstanceArgs($data);
else
return $reflectionObject->newInstance();
}
public static function getRoot()
{
return self::instance()->get_root();
}
public static function isLive()
{
return self::instance()->is_live;
}
public static function getRootURL($site = '')
{
if (strlen($site) > 0) {
if ($site == 'waterfalls' && self::instance()->is_live) {
return 'http://www.waterfallsofthekeweenaw.com/';
} else {
return 'http://' . (self::instance()->is_live ? '' : 'dev.') . $site . '.jacobemerick.com/';
}
}
return '/';
}
}