-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAutoLoader.php
More file actions
65 lines (58 loc) · 1.88 KB
/
AutoLoader.php
File metadata and controls
65 lines (58 loc) · 1.88 KB
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
<?php
/**
* Created by JetBrains PhpStorm.
* User: Seikai
* Date: 12/07/06
* Time: 16:44
* To change this template use File | Settings | File Templates.
*/
class AutoLoader
{
public static function addIncludePath( array $dirs )
{
$currentPath = ini_get( 'include_path' );
if ( $currentPath ) {
$currentPath = explode( PATH_SEPARATOR, $currentPath );
$dirs = array_merge( $currentPath, $dirs );
$dirs = array_unique( $dirs );
}
ini_set( 'include_path', implode( PATH_SEPARATOR, $dirs ) );
}
public static function registerAutoLoad()
{
spl_autoload_register( array( 'AutoLoader', 'loadClass' ) );
}
public static function loadClass($className, $dir = null)
{
if ( class_exists( $className, false ) || interface_exists( $className, false ) ) {
return;
}
if ( (null !== $dir) && !is_string( $dir ) ) {
throw new Exception( '$dir must be a string' );
}
$file = $className . '.php';
if ( !empty( $dir ) ) {
$file = $dir . DIRECTORY_SEPARATOR . $file;
}
self::checkFileName( $file );
include_once $file;
if ( !class_exists( $className, false ) && !interface_exists( $className, false ) ) {
throw new Exception( $className . ' not Exist in load file' );
}
}
/**
* ファイル文字列が安全か
*
* @param string $filename
* @throws Exception
*/
private static function checkFileName($filename)
{
if ( !is_string( $filename ) ) {
throw new Exception( '$filename must be a string' );
}
if ( preg_match( '/[^a-z0-9\\/\\\\_.-]/i', $filename ) ) {
throw new Exception( 'Load Error: Illegal Filename' );
}
}
}