-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass.cb_autoload.php
114 lines (104 loc) · 3.48 KB
/
class.cb_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
<?php
/* This file is part of cbutil.
* Copyright © 2011-2012 stiftung kulturserver.de ggmbh <[email protected]>
*
* cbutil 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 3 of the License, or
* (at your option) any later version.
*
* cbutil 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 cbutil. If not, see <http://www.gnu.org/licenses/>.
*/
// we need the case converter for resolving file names to be included. So we
// have to include it the messy way here.
require_once dirname(__FILE__).'/class.cb_case_converter.php';
class CbAutoload {
private static $registered = false;
/**
* add our autoloader to the queue (there may already be a Zend or Propel autoloader)
*/
public static function register()
{
if (!self::$registered) {
spl_autoload_register(array('CbAutoload', 'autoload'));
self::$registered = true;
}
self::addIncludePath(dirname(__FILE__));
self::addIncludePath('.');
}
public static function addIncludePath($path) {
set_include_path($path.PATH_SEPARATOR.get_include_path());
}
/**
* just a more natural name for autoload.
*
* And easier usage as well. You may load as many classes as you like.
*
* usage:
* <pre>
* Cb::import('CbSomeClass');
* Cb::import('CbSomeClass', 'CbSomeOtherClass');
* </pre>
*
* @param string class_name
* @param ...
*/
public static function import()
{
$a = func_get_args();
foreach ($a as $c) self::autoload($c);
}
/**
* This method returns the filename where a given class can be found.
*/
public static function fileNameFromClass($class_name)
{
$m = array();
if (preg_match('/(\w+)(Interface|Test|Suite)$/', $class_name, $m)) {
return lcfirst($m[2]).'.'.CbCaseConverter::snakeify($m[1]).'.php';
} else {
return 'class.'.CbCaseConverter::snakeify($class_name).'.php';
}
}
/**
* Returns classname of class which is defined in given file.
*
* @param string filename
* @return string classname
*/
public static function classFromFileName($filename)
{
$m = array();
if (preg_match('/^(\w+)\.(\w+)\.php$/', basename($filename), $m)) {
$class = CbCaseConverter::camelize($m[2]);
if ($m[1] !== 'class') $class .= ucfirst($m[1]);
return $class;
} else {
throw InvalidArgumentException("$filename is not a valid filename");
}
}
public static function handleAutoloadNotFoundWarning($a, $b) {}
/**
* This is more like to be used in __autoload() function.
*
* imports file where given class is defined.
*
* @param string a class's name
* @return boolean true, if require_once() is successful, nothing is returned
* otherwise because a fatal error would occurr anyway
*/
public static function autoload($class_name)
{
set_error_handler(array('CbAutoload', 'handleAutoloadNotFoundWarning'),
E_WARNING);
$found = include_once(self::fileNameFromClass($class_name));
restore_error_handler();
return $found;
}
}