~gecka/gecka-framework/nautile

« back to all changes in this revision

Viewing changes to Gecka/Loader.php

  • Committer: Laurent Dinclaux
  • Date: 2008-01-29 23:27:10 UTC
  • Revision ID: laurent@gecka.com-20080129232710-qdypezge2o58relw
First beta release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
require_once('Exception.php');
 
4
 
 
5
class Gecka_Loader {
 
6
                
 
7
        
 
8
        static public function loadClass ( $class_name, $class_path=null, $real_name=null ) {   
 
9
                
 
10
                // class exists
 
11
                if ( class_exists ( $class_name, false ) ) {
 
12
                        return true ;
 
13
                }
 
14
                
 
15
                // autodiscover the path from the class name
 
16
                $path = $class_path ? $class_path . DIRECTORY_SEPARATOR : '';
 
17
                $path .= str_replace ( '_', DIRECTORY_SEPARATOR, $class_name ) . '.php' ;
 
18
                
 
19
                // include file
 
20
                $included = self::loadFile ( $path, true ) ;
 
21
                
 
22
                
 
23
                
 
24
                if(!$included){
 
25
                        throw new Gecka_Exception ( "Could not include \"" . ( $real_name ? $real_name : $class_name) ."\" " ) ;
 
26
                }
 
27
                
 
28
                
 
29
                                        
 
30
                // check class exists
 
31
                if (!class_exists ( $real_name ? $real_name : $class_name, false )) {
 
32
                        throw new Gecka_Exception ( "File \"" . ( $real_name ? $real_name : $class_name) ."\" was included " . "but class \"$class_name\" was not found within." ) ;
 
33
                }
 
34
                
 
35
                return true;
 
36
        }
 
37
        
 
38
        static public function loadFile ($filepath, $dirs=null, $once=false) {
 
39
                
 
40
                // security check
 
41
                if (preg_match ( '/[^a-z0-9\-_.\/:\\' . DIRECTORY_SEPARATOR . ']/i', $filepath )) {
 
42
                        throw new Gecka_Exception ( 'Security check: Illegal character in filename' ) ;
 
43
                }
 
44
 
 
45
                
 
46
                if (self::isReadable($filepath)) {
 
47
                        
 
48
                return self::includeFile($filepath, $once);
 
49
        }
 
50
        
 
51
        throw new Gecka_Exception("File \"$filepath\" was not found");
 
52
                
 
53
        }
 
54
        
 
55
        static public function includeFile ( $filepath , $once = false ) {              
 
56
                
 
57
                if ($once) {
 
58
                        $status = include_once ($filepath) ;
 
59
                } else {
 
60
                        $status = include ($filepath) ;
 
61
                }
 
62
                
 
63
                return $status;
 
64
        }
 
65
        
 
66
        static public function isReadable($filename) {
 
67
        
 
68
                if (@is_readable($filename)) {
 
69
            return true;
 
70
        }
 
71
 
 
72
        $path = get_include_path();
 
73
        $dirs = explode(PATH_SEPARATOR, $path);
 
74
 
 
75
        foreach ($dirs as $dir) {
 
76
            // No need to check against current dir -- already checked
 
77
            if ('.' == $dir) {
 
78
                continue;
 
79
            }
 
80
 
 
81
            if (@is_readable($dir . DIRECTORY_SEPARATOR . $filename)) {
 
82
                return true;
 
83
            }
 
84
        }
 
85
 
 
86
        return false;
 
87
    }
 
88
    
 
89
        static public function registerAutoload() {
 
90
                spl_autoload_register(array('Gecka_Loader', 'autoload'));
 
91
        }
 
92
    
 
93
    static public function autoload( $className ) {
 
94
                self::loadClass($className);
 
95
        }
 
96
        
 
97
        static public function setIncludePath ($path) {
 
98
                
 
99
                set_include_path($path);
 
100
                
 
101
        }
 
102
        
 
103
        static public function addIncludePath ($path) {
 
104
                
 
105
                set_include_path(get_include_path() . PATH_SEPARATOR . $path);
 
106
                
 
107
        }
 
108
 
 
109
        static public function is_in_base_dir($filePath, $baseDir) {
 
110
 
 
111
                $_filePath = realpath($filePath);
 
112
                if(!$_filePath) trigger_error('Le fichier ' . $filePath . ' n\'existe pas. ');
 
113
 
 
114
                if( strpos($_filePath, $baseDir) === 0 ) return true;
 
115
                else return false;
 
116
 
 
117
        }
 
118
 
 
119
}
 
120
 
 
121
?>
 
 
b'\\ No newline at end of file'