This repository has been archived by the owner on Dec 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
autoload.php
135 lines (121 loc) · 3.18 KB
/
autoload.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
<?php
/**
* require default define
*/
require_once ("define.php");
/*
** In object-oriented applications one of the biggest annoyances is having to write a long list of needed includes
** at the beginning of each script.
** an __autoload() function automatically called in case you are trying to use a class/interface which hasn't been defined yet.
** By calling this function the scripting engine is given a last chance to load the class before PHP fails with an error.
*/
class autoload
{
static $require = array();
static $core_prefix = array('lib', 'ilib', 'cls', 'database', 'mvc', 'addons');
static $autoload = false;
/**
* [load description]
* @param [type] $name [description]
* @return [type] [description]
*/
static function load($name)
{
if(isset(self::$require[$name]))
{
return;
}
$split_name = preg_split("[\\\]", $name);
if(count($split_name) > 1)
{
$file_addr = self::iget_file_name($split_name);
if($file_addr !== false)
{
self::$require[$name] = 1;
$file_addr = stream_resolve_include_path($file_addr);
include_once($file_addr);
}
else
{
$name = preg_replace("/[\\\]/", DIRECTORY_SEPARATOR, $name).'.php';
$file_addr = stream_resolve_include_path($name);
if($file_addr)
{
self::$require[$name] = 1;
include_once($file_addr);
}
else
{
return false;
}
}
}
}
/**
* [get_file_name description]
* @param [type] $split_name [description]
* @return [type] [description]
*/
static function get_file_name($split_name)
{
list($prefix, $sub_path, $exec_file) = self::ifile_splice($split_name);
$prefix_file = null;
if (preg_grep("/^$prefix$/", self::$core_prefix))
{
$file_addr = self::icheck_file($prefix, $sub_path, $exec_file);
return $file_addr;
}
$prefix_file = \lib\router::get_repository();
$prefix_file = preg_replace("#\/[^\/]+\/?$#", '', $prefix_file);
$file_addr = $prefix_file. '/'. $prefix.'/'. $sub_path. $exec_file;
if(!file_exists($file_addr))
{
$file_addr = false;
}
return $file_addr;
}
/**
* [check_file description]
* @param [type] $prefix [description]
* @param [type] $sub_path [description]
* @param [type] $exec_file [description]
* @return [type] [description]
*/
static function check_file($prefix, $sub_path, $exec_file)
{
if(!defined($prefix))
{
return false;
}
$prefix_file = constant($prefix);
$file_addr = $prefix_file .$sub_path .$exec_file;
if(stream_resolve_include_path($file_addr))
{
return $file_addr;
}
return false;
}
/**
* [file_splice description]
* @param [type] $split_name [description]
* @return [type] [description]
*/
static function file_splice($split_name)
{
$prefix = $split_name[0];
array_shift($split_name);
$exec_file = end($split_name);
array_pop($split_name);
$sub_path = (count($split_name) > 0) ? join($split_name, "/") .'/' : '';
return array($prefix, $sub_path, $exec_file .".php");
}
static function __callStatic($_name, $_args)
{
$name = preg_replace("/^i/", "", $_name);
return autoload::{$name}(...$_args);
}
}
spl_autoload_register("\autoload::load");
// RUN DASH!
new \lib\dash;
?>