/
class.Loader.php
174 lines (157 loc) 路 5.33 KB
/
class.Loader.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
<?php
/**
* LICENSE:
*
* This file is part of Isosceles (http://ginatrapani.github.io/isosceles/).
*
* Isosceles is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
* later version.
*
* Isosceles is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with Isosceles. If not, see
* <http://www.gnu.org/licenses/>.
*
* @license http://www.gnu.org/licenses/gpl.html
*/
class Loader {
/**
* Lookup path for classes and interfaces.
*
* @var array
*/
private static $lookup_path;
/**
* Some classes have a special filename that doesn't follow the convention.
* The value will be assigned inside setLookupPath method.
*/
private static $special_classes;
/**
* Register current script to use lazy loading.
* @param array $additional_paths Array of strings; additional lookup path for classes
* @return bool true
*/
public static function register($paths = null) {
self::setLookupPath($paths);
return spl_autoload_register(array(__CLASS__, 'load' ));
}
/**
* Unregister the loader script.
*/
public static function unregister() {
self::$lookup_path = null;
self::$special_classes = null;
return spl_autoload_unregister( array(__CLASS__, 'load') );
}
/**
* Set lookup paths
* @param array $additional_paths Array of strings, additional lookup path for classes
*/
private static function setLookupPath($additional_paths = null ) {
// check required named constants
if ( !defined('ISOSCELES_PATH') ) {
define('ISOSCELES_PATH', str_replace("\\",'/', dirname(dirname(__FILE__))) .'/');
}
// set default lookup path for classes
self::$lookup_path = array(
ISOSCELES_PATH . 'libs/',
ISOSCELES_PATH . 'libs/model/',
ISOSCELES_PATH . 'libs/dao/',
ISOSCELES_PATH . 'libs/controller/',
ISOSCELES_PATH . 'libs/exceptions/'
);
// set default lookup path for special classes
self::$special_classes = array(
'Smarty' => ISOSCELES_PATH . 'libs/extlibs/Smarty-3.1.21/libs/Smarty.class.php'
);
if ( isset($additional_paths) && is_array($additional_paths) ) {
foreach ($additional_paths as $path) {
self::$lookup_path[] = $path;
}
}
}
/**
* Define application path constant ISOSCELES_PATH
*/
public static function definePathConstants() {
if (!defined('ISOSCELES_PATH') ) {
define('ISOSCELES_PATH', str_replace("\\",'/', dirname(dirname(__FILE__))) .'/');
}
}
/**
* Add another lookup path
* @param str $path
*/
public static function addPath($path) {
if (!isset(self::$lookup_path)) {
self::register();
}
self::$lookup_path[] = $path;
}
/**
* Get lookup path
* @return array of lookup paths
*/
public static function getLookupPath() {
return self::$lookup_path;
}
/**
* Get special classes files
* @return array of special classes path files
* @access public
*/
public static function getSpecialClasses() {
return self::$special_classes;
}
/**
* Add special class information for loading.
*
* @param str $class_name
* @param str $path full qualified path
*/
public static function addSpecialClass($class_name, $path) {
self::definePathConstants();
self::$special_classes[$class_name] = $path;
require_once($path);
}
/**
* The method registered to run on _autoload. When a class gets instantiated this method will be called to look up
* the class file if the class is not present. The second instantiation of the same class wouldn't call this method.
*
* @param str $class Class name
* @return bool true
*/
public static function load($class) {
// if class already in scope
if ( class_exists($class, false) ) {
return;
}
// if $class is a standard Isosceles object or interface
foreach ( self::$lookup_path as $path ) {
$file_name = $path . 'class.' . $class . '.php';
if ( file_exists( $file_name )) {
require_once $file_name;
return;
}
$file_name = $path . 'interface.' . $class . '.php';
if ( file_exists( $file_name )) {
require_once $file_name;
return;
}
$file_name = $path . $class . '.php';
if ( file_exists( $file_name )) {
require_once $file_name;
return;
}
}
// if $class is special class filename
if ( array_key_exists($class, self::$special_classes) ) {
require_once self::$special_classes[$class];
return;
}
//error_log("Class ".$class." not found");
}
}