-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoader.php
More file actions
254 lines (225 loc) · 5.55 KB
/
Loader.php
File metadata and controls
254 lines (225 loc) · 5.55 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php if ( ! defined('DENY_ACCESS')) exit('403: No direct file access allowed');
/**
* A Bright CMS
*
* Open source, lightweight, web application framework and content management
* system in PHP.
*
* @package A Bright CMS
* @author Gabriel Liwerant
*/
/**
* Loader Class
*
* We use this with spl_autoload_register to load our class files.
*
* @subpackage core
* @author Gabriel Liwerant
*/
class Loader
{
/**
* Error codes for Loader
*/
const FILE_PATH_COULD_NOT_BE_OPENED = 1001;
/**
* Stored paths for use in our autoloader method.
*
* @var array $path
*/
public static $path = array();
/**
* The constructor stores the valid paths in our class for later.
*
* @param string array $path_arr Paths to store for autoloading later
*/
public function __construct($path_arr)
{
self::_setInitialPaths($path_arr);
}
/**
* Setter for inial paths
*
* @param array $path_arr
*/
private function _setInitialPaths($path_arr)
{
self::$path = $path_arr;
}
/**
* Validate that our file path is accurate.
*
* @deprecated Currently unused, slated for removal unless we need some
* fancy validation here aside from the obvious.
*
* @param string $file_path
*
* @return boolean
*/
private static function _isFilePathValid($file_path)
{
if (file_exists($file_path))
{
return true;
}
else
{
return false;
}
}
/**
* Open a file path and look through it for appropriate subdirectories.
*
* @param string $path
*
* @return array Return null if no appropriate directories were found
*/
private static function _getSubdirectoryArrayFromFilePath($path)
{
$handle = opendir($path);
if ($handle)
{
// Must use strict comparison to false or we have silent issues
while (($directory = readdir($handle)) !== false)
{
$has_period = strstr($directory, '.');
$is_license = strstr($directory, 'LICENSE');
// We don't want anything with a period, as it's either a single
// file or the period-only directories.
if ( ! $has_period AND ! $is_license)
{
$directory_arr[] = $directory;
}
}
closedir($handle);
return isset($directory_arr) ? $directory_arr : null;
}
else
{
throw ApplicationFactory::makeException('Loader Exception', self::FILE_PATH_COULD_NOT_BE_OPENED);
//throw new Exception('Loader Exception', self::FILE_PATH_COULD_NOT_BE_OPENED);
}
}
/**
* Search through all subdirectories in a given path recursively until no
* more are found and then attempt to load the class from the final leaf.
*
* @param string $path
* @param string $class_name
*/
private static function _loadFromSubdirectory($path, $class_name)
{
$sub_dir_arr = self::_getSubdirectoryArrayFromFilePath($path);
if ( ! empty($sub_dir_arr))
{
foreach ($sub_dir_arr as $sub_dir)
{
self::_loadFromSubdirectory($path . '/' . $sub_dir, $class_name);
self::_load(array($path, $sub_dir), $class_name);
}
}
}
/**
* From an array of directories listed outermost to innermost, build the
* appropriate file path array for our class name.
*
* We use a file path array so that we can try multiple path variations to
* accommodate alternate file names or case issues.
*
* @param array $directory_arr
* @param string $file_name
*
* @return array
*/
private static function _buildFilePathArray($directory_arr, $file_name)
{
$path = null;
foreach ($directory_arr as $directory)
{
$path .= $directory . '/';
}
$possible_path_arr = array(
$path . $file_name . '.php',
$path . $file_name . '.inc.php'
);
if (PHP_VERSION < 5.3)
{
$file_name = self::convertFirstCharacterToLowerCase($file_name);
array_push(
$possible_path_arr,
$path . $file_name . '.php',
$path . $file_name . '.inc.php'
);
}
return $possible_path_arr;
}
/**
* Load a file after we've built the proper path and checked whether or not
* it exists.
*
* @param array $directory_arr
* @param string $file_name
*/
private static function _load($directory_arr, $file_name)
{
$file_path_arr = self::_buildFilePathArray($directory_arr, $file_name);
foreach ($file_path_arr as $file_path)
{
if (file_exists($file_path))
{
require $file_path;
break;
}
}
}
/**
* Set additional paths for our autoloader to call upon. Useful for
* assigning conditional paths after construction, like development paths.
*
* @param string $path
*
* return object Loader
*/
public function setAdditionalPath($path_arr)
{
foreach ($path_arr as $key => $path)
{
self::$path[$key] = $path;
}
return $this;
}
/**
* The method to find the proper paths for autoloading our classes.
*
* We run through the list of paths given in the class property, check
* subdirectories of those, and attempt to build the full class path to load.
*
* @param string $class_name
*/
public static function autoload($class_name)
{
foreach (self::$path as $main_dir)
{
if ($main_dir['search_sub_dir'])
{
self::_loadFromSubdirectory($main_dir['path'], $class_name);
}
self::_load(array($main_dir['path']), $class_name);
}
}
/**
* PHP 5.2 safe way to convert first character in a string to lower case.
*
* @param string $string
*
* @return string
*/
public static function _convertFirstCharacterToLowerCase($string)
{
$lower_first_char = strtolower(substr($string, 0, 1));
$all_but_first_char = substr($string, 1);
return $lower_first_char . $all_but_first_char;
}
}
// End of Loader Class
/* EOF system/core/Loader.php */