flexloader-autoload

Gesponsorde koppelingen

PHP script bestanden

  1. flexloader-autoload

« Lees de omschrijving en reacties

Een voorbeeld autload functie, ook kan je natuurlijk zelf een functie maken die in SPL te hangen valt

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
175
176
177
178
179
180
181
<?php
/**
 * Automagic PHP autoload function
 *
 * @param string $class_name
 * @return bool
 */

function __autoload($class_name)
{

    static $flexloader = null;
    
    if(!isset($flexloader)) {
        $cachepath = './cache/flexloader.cache.php';
        $classespath = realpath('./classes');
        // Cachefile path, directories to scan, extensions, ignore hidden files, follow symlinks
        $flexloader = new FlexLoader($cachepath, array($classespath));
    }

    return $flexloader->loadClass($class_name);
}
[
/code]


[
code]
[
code]<?php
/**
 * FlexLoader
 *
 * @author Christiaan Baartse <[email protected]>
 * @version 0.0.2
 * @since 28/02/2008
 */

class FlexLoader {
    
    /**
     * Directories to scan
     *
     * @var array
     */

    protected $directories;
    
    /**
     * Cache filename
     *
     * @var string
     */

    protected $cache_file;
    
    /**
     * Class to file mapping
     *
     * @var array
     */

    protected $class2file;
    
    /**
     * Extensions to scan
     *
     * @var array
     */

    protected $extensions;
    
    /**
     * Enable scanning of hidden files
     *
     * @var bool
     */

    protected $hidden;
    
    /**
     * Enable following of symlinks
     *
     * @var bool
     */

    protected $symlinks;
        
    /**
     * Construct
     *
     * @param string $cache_file
     * @param array $directories
     * @param array $extensions
     * @param bool $hidden
     * @param bool $symlinks
     */

    public function __construct($cache_file = 'flexloader.cache.php', $directories = array('classes'), $extensions = array('.php'), $hidden = false, $symlinks = true)
    {

        $this->cache_file = $cache_file;
        $this->directories = $directories;
        $this->extensions = $extensions;
        $this->hidden = $hidden;
        $this->symlinks = $symlinks;
        
        if( ! file_exists($this->cache_file) && ! $this->createCache()) {
            throw new Exception("FlexLoader: Couldn't create cache '".$this->cache_file."'");
        }
else{
            if(include($this->cache_file)) {
                $this->class2file = @$flexloader_classes;
            }
else{
                throw new Exception("FlexLoader: Couldn't load cache from '".$this->cache_file."'");
            }
        }     
    }

    
    /**
     * Load file containing the given class
     *
     * @param string $class_name
     * @return boolean
     */

    public function loadClass($class)
    {

        $class = strtolower($class);
        
        if(isset($this->class2file[$class]) && include($this->class2file[$class])) {
            return TRUE;
        }

        else{ //Comment out this whole else on production sites
            $this->class2file = array();
            $this->createCache();
            if(isset($this->class2file[$class]) && include($this->class2file[$class])) {
                return TRUE;
            }
        }
// So untill here

        throw new Exception(__CLASS__." couldn't ".__METHOD__." ".$class);
    }

    
    /**
     * Create the cachefile
     *
     * @return bool
     */

    public function createCache()
    {

        foreach($this->directories as $directory) {
            $this->parseDirectory($directory.'/');
        }

        
        $cache = "<?php\n\$flexloader_classes = ";
        $cache.= var_export($this->class2file, true);
        $cache.= ';';        
        file_put_contents($this->cache_file, $cache);
        
        return TRUE;
    }

    
    /**
     * Parse Directory for files containing classes
     *
     * @param string $directory Pathname of directory
     * @return bool
     */

    protected function parseDirectory($directory)
    {

        if( ! is_dir($directory)) return FALSE;
        if( ! $dhandle = opendir($directory)) return FALSE;
        while(($file = readdir($dhandle)) !== false) {
            if($file[0] == '.' && ! $this->hidden) continue;

            $file_path = $directory.$file;
            if(is_dir($file_path)){
                if($file == '.' || $file == '..') continue;
                $this->parseDirectory($file_path.'/');            
            }
elseif($this->symlinks && is_link($file_path)){
                $this->parseDirectory($file_path.'/');
            }
elseif(is_file($file_path)){
                if(empty($this->extensions) || in_array(substr($file, strrpos($file, '.')), $this->extensions)) {
                    preg_match_all('~^\s*(?:abstract\s+|final\s+)?(?:class|interface)\s+(\w+)~mi', file_get_contents($file_path), $result);
                    if( ! empty($result[1])){
                        foreach($result[1] as $class_name) {
                            $this->class2file[strtolower($class_name)] = $file_path;
                        }
                    }
                }
            }
        }
                
        return TRUE;
    }
    
}
[
/code]

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.