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
|
|
|
// if(!file_exists($file_path)) |
|
|
|
|
114
|
|
|
// Debugger::logMessage("Requested file does not exist: {$type}, {$file}"); |
|
|
|
|
115
|
|
|
|
116
|
|
|
self::instance()->add_included_file($file_path); |
117
|
|
|
|
118
|
|
|
switch($type) |
119
|
|
|
{ |
120
|
|
|
case 'images' : |
|
|
|
|
121
|
|
|
case 'scripts' : |
|
|
|
|
122
|
|
|
case 'styles' : |
|
|
|
|
123
|
|
|
echo file_get_contents($file_path); |
124
|
|
|
break; |
125
|
|
|
case 'view' : |
|
|
|
|
126
|
|
|
extract($data); |
127
|
|
|
include($file_path); |
128
|
|
|
break; |
129
|
|
|
default : |
|
|
|
|
130
|
|
|
include_once($file_path); |
131
|
|
|
break; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
private static function create_reflection_class($file) |
137
|
|
|
{ |
138
|
|
|
$class_name = self::instance()->get_class_name($file); |
139
|
|
|
return new ReflectionClass($class_name); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public static function loadInstance($type, $file) |
143
|
|
|
{ |
144
|
|
|
self::load($type, $file); |
145
|
|
|
|
146
|
|
|
$reflectionObject = self::create_reflection_class($file); |
147
|
|
|
|
148
|
|
|
if( |
149
|
|
|
$reflectionObject->hasMethod('instance') && |
150
|
|
|
$reflectionObject->getMethod('instance')->isStatic()) |
151
|
|
|
{ |
152
|
|
|
return $reflectionObject->getMethod('instance')->invoke(null); |
153
|
|
|
} |
154
|
|
|
trigger_error("Requested class cannot be instance'd: {$type}, {$file}"); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public static function loadNew($type, $file, $data = array()) |
158
|
|
|
{ |
159
|
|
|
self::load($type, $file); |
160
|
|
|
|
161
|
|
|
$reflectionObject = self::create_reflection_class($file); |
162
|
|
|
|
163
|
|
|
if($reflectionObject->hasMethod('__construct')) |
164
|
|
|
return $reflectionObject->newInstanceArgs($data); |
165
|
|
|
else |
166
|
|
|
return $reflectionObject->newInstance(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public static function getRoot() |
170
|
|
|
{ |
171
|
|
|
return self::instance()->get_root(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public static function isLive() |
175
|
|
|
{ |
176
|
|
|
return self::instance()->is_live; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public static function getRootURL($site = '') |
180
|
|
|
{ |
181
|
|
|
if (strlen($site) > 0) { |
182
|
|
|
if ($site == 'waterfalls' && self::instance()->is_live) { |
183
|
|
|
return 'http://www.waterfallsofthekeweenaw.com/'; |
184
|
|
|
} else { |
185
|
|
|
return 'http://' . (self::instance()->is_live ? '' : 'dev.') . $site . '.jacobemerick.com/'; |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
return '/'; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
} |
192
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.