/
autoload.php
77 lines (68 loc) · 2.25 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
<?php
/**
* Ethna_* クラス群のオートロード
*/
spl_autoload_register(function($className){
//Ethnaクラス
if ($className === 'Ethna') {
include_once __DIR__ . '/src/Ethna.php';
}
//Ethna_*クラス
//単純に_区切りをディレクトリ区切りにマッピングする
if (strpos($className, 'Ethna_') === 0) {
$separated = explode('_', $className);
array_shift($separated); // remove first element
//読み込み失敗しても死ぬ必要はないのでrequireではなくincludeする
//see http://qiita.com/Hiraku/items/72251c709503e554c280
include_once __DIR__ . '/src/' . join('/', $separated) . '.php';
}
});
spl_autoload_register(function($class_name){
$file = sprintf("%s.%s", $class_name, 'php');
if (file_exists_ex($file)) {
include_once $file;
return ;
}
if (preg_match('/^(\w+?)_(.*)/', $class_name, $match)) {
// try ethna app style
// App_Foo_Bar_Baz -> Foo/Bar/App_Foo_Bar_Baz.php
$tmp = explode("_", $match[2]);
$tmp[count($tmp)-1] = $class_name;
$file = sprintf('%s.%s',
implode(DIRECTORY_SEPARATOR, $tmp),
'php');
if (file_exists_ex($file)) {
include_once $file;
return ;
}
// try ethna app & pear mixed style
// App_Foo_Bar_Baz -> Foo/Bar/Baz.php
$file = sprintf('%s.%s',
str_replace('_', DIRECTORY_SEPARATOR, $match[2]),
'php');
if (file_exists_ex($file)) {
include_once $file;
return ;
}
// try ethna master style
// Ethna_Foo_Bar -> src/Ethna/Foo/Bar.php
$tmp = explode('_', $match[2]);
array_unshift($tmp, 'Ethna', 'class');
$file = sprintf('%s.%s',
implode(DIRECTORY_SEPARATOR, $tmp),
'php');
if (file_exists_ex($file)) {
include_once $file;
return ;
}
// try pear style
// Foo_Bar_Baz -> Foo/Bar/Baz.php
$file = sprintf('%s.%s',
str_replace('_', DIRECTORY_SEPARATOR, $class_name),
'php');
if (file_exists_ex($file)) {
include_once $file;
return ;
}
}
});