1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
final class Loader |
4
|
|
|
{ |
5
|
|
|
|
6
|
|
|
private $root; |
7
|
|
|
private $is_live; |
8
|
|
|
private $included_files = array(); |
9
|
|
|
|
10
|
|
|
private static $instance; |
11
|
|
|
|
12
|
|
|
private function __construct() |
13
|
|
|
{ |
14
|
|
|
$this->is_live = (isset($_SERVER['HTTP_HOST']) && substr($_SERVER['HTTP_HOST'], 0, 4) !== 'dev.'); |
15
|
|
|
return $this; |
|
|
|
|
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public static function instance() |
19
|
|
|
{ |
20
|
|
|
if(!isset(self::$instance)) |
21
|
|
|
self::$instance = new Loader(); |
22
|
|
|
return self::$instance; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
private function get_root() |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
if(!isset($this->root)) |
28
|
|
|
{ |
29
|
|
|
$current_directory = dirname(__FILE__); |
30
|
|
|
$current_directory = substr($current_directory, 0, -7); |
31
|
|
|
$this->root = $current_directory; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return $this->root; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
private function get_delimiter() |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
return (true || $this->is_live) ? '/' : '\\'; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
private function check_delimiters($path) |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
return (true || $this->is_live) ? $path : str_replace('/', '\\', $path); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
private static function get_class_name($path) |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
$path_array = explode('/', $path); |
50
|
|
|
return array_pop($path_array); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
private static function get_extension($type) |
54
|
|
|
{ |
55
|
|
|
switch($type) |
56
|
|
|
{ |
57
|
|
|
case 'collector' : |
58
|
|
|
case 'controller' : |
59
|
|
|
case 'model' : |
60
|
|
|
case 'module' : |
61
|
|
|
case 'router' : |
62
|
|
|
case 'utility' : |
63
|
|
|
$extension = '.class.inc.php'; |
64
|
|
|
break; |
65
|
|
|
case 'view' : |
66
|
|
|
$extension = '.tpl.php'; |
67
|
|
|
break; |
68
|
|
|
} |
69
|
|
|
return $extension; |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
View Code Duplication |
public static function getImagePath($type, $file) |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
$path = self::instance()->get_root(); |
75
|
|
|
$path .= 'public'; |
76
|
|
|
$path .= self::instance()->get_delimiter(); |
77
|
|
|
$path .= $type; |
78
|
|
|
$path .= self::instance()->get_delimiter(); |
79
|
|
|
$path .= self::instance()->check_delimiters($file); |
80
|
|
|
|
81
|
|
|
return $path; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
View Code Duplication |
private static function get_path($type, $file) |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
$path = self::instance()->get_root(); |
87
|
|
|
$path .= $type; |
88
|
|
|
$path .= self::instance()->get_delimiter(); |
89
|
|
|
$path .= self::instance()->check_delimiters($file); |
90
|
|
|
$path .= self::get_extension($type); |
91
|
|
|
|
92
|
|
|
return $path; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
private function get_included_files() |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
return $this->included_files; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
private function add_included_file($path) |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
$this->included_files[] = $path; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public static function load($type, $files, $data = array()) |
106
|
|
|
{ |
107
|
|
|
foreach((array) $files as $file) |
108
|
|
|
{ |
109
|
|
|
$file_path = self::instance()->get_path($type, $file); |
110
|
|
|
if(in_array($file_path, self::instance()->get_included_files()) && $type !== 'view') |
111
|
|
|
continue; |
112
|
|
|
|
113
|
|
|
self::instance()->add_included_file($file_path); |
114
|
|
|
|
115
|
|
|
switch($type) |
116
|
|
|
{ |
117
|
|
|
case 'images' : |
118
|
|
|
case 'scripts' : |
119
|
|
|
case 'styles' : |
120
|
|
|
echo file_get_contents($file_path); |
121
|
|
|
break; |
122
|
|
|
case 'view' : |
123
|
|
|
extract($data); |
124
|
|
|
include($file_path); |
125
|
|
|
break; |
126
|
|
|
default : |
127
|
|
|
include_once($file_path); |
128
|
|
|
break; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
private static function create_reflection_class($file) |
134
|
|
|
{ |
135
|
|
|
$class_name = self::instance()->get_class_name($file); |
136
|
|
|
return new ReflectionClass($class_name); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public static function loadInstance($type, $file) |
140
|
|
|
{ |
141
|
|
|
self::load($type, $file); |
142
|
|
|
|
143
|
|
|
$reflectionObject = self::create_reflection_class($file); |
144
|
|
|
|
145
|
|
|
if( |
146
|
|
|
$reflectionObject->hasMethod('instance') && |
147
|
|
|
$reflectionObject->getMethod('instance')->isStatic()) |
148
|
|
|
{ |
149
|
|
|
return $reflectionObject->getMethod('instance')->invoke(null); |
150
|
|
|
} |
151
|
|
|
trigger_error("Requested class cannot be instance'd: {$type}, {$file}"); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public static function loadNew($type, $file, $data = array()) |
155
|
|
|
{ |
156
|
|
|
self::load($type, $file); |
157
|
|
|
|
158
|
|
|
$reflectionObject = self::create_reflection_class($file); |
159
|
|
|
|
160
|
|
|
if($reflectionObject->hasMethod('__construct')) |
161
|
|
|
return $reflectionObject->newInstanceArgs($data); |
162
|
|
|
else |
163
|
|
|
return $reflectionObject->newInstance(); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public static function getRoot() |
167
|
|
|
{ |
168
|
|
|
return self::instance()->get_root(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public static function isLive() |
172
|
|
|
{ |
173
|
|
|
return self::instance()->is_live; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public static function getRootURL($site = '') |
177
|
|
|
{ |
178
|
|
|
if (strlen($site) > 0) { |
179
|
|
|
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http'; |
180
|
|
|
if ($site == 'waterfalls' && self::instance()->is_live) { |
181
|
|
|
return "{$protocol}://www.waterfallsofthekeweenaw.com/"; |
182
|
|
|
} else { |
183
|
|
|
return $protocol . '://' . (self::instance()->is_live ? '' : 'dev.') . $site . '.jacobemerick.com/'; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
return '/'; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
} |
190
|
|
|
|