-
Notifications
You must be signed in to change notification settings - Fork 2
/
AutoLoader.php
executable file
·197 lines (173 loc) · 7.24 KB
/
AutoLoader.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
195
196
<?php
/**
* This file sets up the application.
* Sets up constants for the main file locations.
* @package PHP-Wax
*/
/**
* Defines application level constants
*/
if(!defined("APP_DIR")) define('APP_DIR', WAX_ROOT . "app/");
if(!defined("MODEL_DIR")) define('MODEL_DIR' , WAX_ROOT.'app/model/');
if(!defined("CONTROLLER_DIR")) define('CONTROLLER_DIR', WAX_ROOT.'app/controller/');
if(!defined("FORMS_DIR")) define('FORMS_DIR', WAX_ROOT.'app/forms/');
if(!defined("CONFIG_DIR")) define('CONFIG_DIR' , WAX_ROOT.'app/config/');
if(!defined("VIEW_DIR")) define('VIEW_DIR', WAX_ROOT.'app/view/');
if(!defined("APP_LIB_DIR")) define('APP_LIB_DIR', WAX_ROOT.'app/lib/');
if(!defined("CACHE_DIR")) define('CACHE_DIR', WAX_ROOT.'tmp/cache/');
if(!defined("LOG_DIR")) define('LOG_DIR', WAX_ROOT.'tmp/log/');
if(!defined("SESSION_DIR")) define('SESSION_DIR', WAX_ROOT.'tmp/session/');
if(!defined("PUBLIC_DIR")) define('PUBLIC_DIR', WAX_ROOT.'public/');
if(!defined("SCRIPT_DIR")) define('SCRIPT_DIR', PUBLIC_DIR.'javascripts/');
if(!defined("STYLE_DIR")) define('STYLE_DIR', PUBLIC_DIR.'stylesheets/');
if(!defined("PLUGIN_DIR")) define('PLUGIN_DIR', WAX_ROOT . 'plugins/');
if(!defined("FRAMEWORK_DIR")) define("FRAMEWORK_DIR", dirname(__FILE__));
function __autoload($class_name) {
AutoLoader::include_from_registry($class_name);
}
function throw_wxexception($e) {
$exc = new WXException($e->getMessage(), "Application Error");
}
function throw_wxerror($code, $error) {
$exc = new WXException($error, "Application Error $code");
}
/**
* A simple static class to Preload php files and commence the application.
* It manages a registry of PHP files and includes them according to hierarchy.
* All file inclusion is done 'just in time' meaning that file load overhead is avoided.
* @package PHP-Wax
* @static
*/
class AutoLoader
{
/**
* @access public
* @param string $dir The directory to include
*/
static $plugin_array=array();
/**
* The registry allows classes to be registered in a central location.
* A responsibility chain then decides upon include order.
* Format $registry = array("responsibility"=>array("ClassName", "path/to/file"))
*/
static public $registry = array();
static public $registry_chain = array("user", "application", "plugin", "framework");
static public function register($responsibility, $class, $path) {
self::$registry[$responsibility][$class]=$path;
}
static public function include_from_registry($class_name) {
foreach(self::$registry_chain as $responsibility) {
if(isset(self::$registry[$responsibility]) && array_key_exists($class_name, self::$registry[$responsibility])) {
if(require_once(self::$registry[$responsibility][$class_name]) ) { return true; }
}
}
throw new WXDependencyException("Class Name - {$class_name} cannot be found in the registry.", "Missing Dependency");
}
static public function include_plugin($plugin) {
self::recursive_register(PLUGIN_DIR.$plugin."/lib", "plugin");
$setup = PLUGIN_DIR.$plugin."/setup.php";
if(is_readable($setup)) include_once($setup);
}
static public function plugin_installed($plugin) {
return is_readable(PLUGIN_DIR.$plugin);
}
static public function autoregister_plugins() {
if(defined('AUTOREGISTER_PLUGINS')) return false;
$plugins = scandir(PLUGIN_DIR);
sort($plugins);
foreach($plugins as $plugin) {
if(is_dir(PLUGIN_DIR.$plugin)) self::include_plugin($plugin);
}
}
static public function detect_assets() {
self::register("framework", "File", FRAMEWORK_DIR."/utilities/File.php");
if(!isset($_GET["route"])) return false;
$temp_route = $_GET["route"];
$_temp_route= preg_replace("/[^a-zA-Z0-9_\-\.]/", "", $temp_route);
while(strpos($temp_route, "..")) $temp_route= str_replace("..", ".", $temp_route);
$asset_paths = explode("/", $_GET["route"]);
if($asset_paths[0] =="images" || $asset_paths[0] =="javascripts" || $asset_paths[0] =="stylesheets") {
$plugins = scandir(PLUGIN_DIR);
$type = array_shift($asset_paths);
rsort($plugins);
foreach($plugins as $plugin) {
$path = PLUGIN_DIR.$plugin."/resources/public/".$type."/".implode("/", $asset_paths);
if($type=="images") File::display_image($path);
if($type=="javascripts") File::display_asset($path, "text/javascript");
if($type=="stylesheets") File::display_asset($path, "text/css");
}
}
}
static public function recursive_register($directory, $type, $force = false) {
if(!is_dir($directory)) { return false; }
$dir = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($directory), true);
foreach ( $dir as $file ) {
if(substr($file->getFilename(),0,1) != "." && strrchr($file->getFilename(), ".")==".php") {
if($force){
require_once($file->getPathName());
}else{
$classname = basename($file->getFilename(), ".php");
self::register($type, $classname, $file->getPathName());
}
}
}
}
static public function detect_test_mode() {
if(isset($_SERVER['HTTP_USER_AGENT']) && $_SERVER['HTTP_USER_AGENT'] == "simpletest" ) {
define('ENV', 'test');
}
}
static public function detect_environments() {
if(!is_array(WXConfiguration::get("environments"))) return false;
if($_SERVER['HOSTNAME']) $addr = gethostbyname($_SERVER['HOSTNAME']);
elseif($_SERVER['SERVER_ADDR']) $addr = $_SERVER['SERVER_ADDR'];
if($envs= WXConfiguration::get("environments")) {
foreach($envs as $env=>$range) {
$range = "/".str_replace(".", "\.", $range)."/";
if(preg_match($range, $addr) && !defined($env) ) {
define('ENV', $env);
}
}
}
}
static public function register_helpers() {
foreach(get_declared_classes() as $class) {
if(is_subclass_of($class, "WXHelpers") || $class=="WXHelpers" || $class == "Inflections") {
foreach(get_class_methods($class) as $method) {
if(substr($method,0,1)!="_" && !function_exists($method)) {
WXGenerator::new_helper_wrapper($class, $method);
}
}
}
}
}
static public function initialise() {
self::detect_assets();
self::detect_test_mode();
self::recursive_register(APP_LIB_DIR, "user");
self::recursive_register(MODEL_DIR, "application");
self::recursive_register(CONTROLLER_DIR, "application");
self::recursive_register(FORMS_DIR, "application");
self::recursive_register(FRAMEWORK_DIR, "framework");
self::autoregister_plugins();
self::include_from_registry('Inflections'); // Bit of a hack -- forces the inflector functions to load
self::include_from_registry('WXHelpers'); // Bit of a hack -- forces the helper functions to load
self::register_helpers();
set_exception_handler('throw_wxexception');
set_error_handler('throw_wxerror', 247 );
}
/**
* Includes the necessary files and instantiates the application.
* @access public
*/
static public function run_application($environment="development", $full_app=true) {
//if(!defined('ENV')) define('ENV', $environment);
$app=new WXApplication($full_app);
}
/**** DEPRECIATED FUNCTIONS BELOW THIS POINT, WILL BE REMOVED IN COMING RELEASES ****/
static public function include_dir($directory, $force = false) {
return self::recursive_register($directory, "framework", $force);
}
}
Autoloader::initialise();