root/trunk/common/mvblog_autoloader.php

Revision 776, 3.7 KB (checked in by michiel, 9 months ago)

update copyright year.

Closes #180

Line 
1<?php
2/**
3 * MvBlog -- An open source no-nosense blogtool
4 *
5 * Copyright (C) 2005-2008, Michiel van Baak
6 * Michiel van Baak <mvanbaak@users.sourceforge.net>
7 *
8 * See http://dev.mvblog.org for more information on MvBlog.
9 * That page also provides Bugtrackers, Filereleases etc.
10 *
11 * This program is free software, distributed under the terms of
12 * the GNU General Public License Version 2. See the LICENSE file
13 * at the top of the source tree.
14 *
15 * @package MvBlog
16 * @author Ferry Boender
17 * @version 2.0
18 * @copyright 2005-2008 Ferry Boender
19 */
20
21/**
22 * AutoLoader class handles auto loading of classes/objects.
23 *
24 * Example:
25 * <code>
26 * require_once("mvblog_AutoLoader.php");
27 *
28 * $mvblog_AutoLoader = new mvblog_AutoLoader();
29 *
30 * // Add the directory in which the current file resides to the autoloader.
31 * $pathInfo = pathinfo(__FILE__);
32 * $mvblog_AutoLoader->registerPath($pathInfo["dirname"]);
33 *
34 * // Register the AutoLoader object as the autoloader.
35 * function __autoload($className) {
36 *   global $mvblog_AutoLoader;
37 *   $mvblog_AutoLoader->autoload($className);
38 * }
39 * </code>
40 */
41class mvblog_AutoLoader {
42    const OPT_LOWERCASE = 1;
43
44    protected $paths = array();
45
46    /**
47     * Create a new mvblog_AutoLoader object. It will automatically parse
48     * your PHP ini include_path for paths in which is should look
49     * for objects. The default format it will look for is "CLASSNAME.php"
50     * (case-sensitive). The current directory is usually included in
51     * your include_path, so you won't have to add that. Add new
52     * paths using ->registerPath($path, $format).
53     *
54     * @param array $paths (optional) An array containing paths and formats. Each element in $paths is an associative array with "path", "format" and "options" keys. I.e. new mvblog_AutoLoader(array(array("path"=>"/usr/share/lib/app", "format" => "class_%s.php", "options" => mvblog_AutoLoader::OPT_LOWERCASE)));
55     */
56    public function __construct($paths = null) {
57        // Add paths in include_path ini setting of PHP.
58        $phpIncludePath = ini_get("include_path");
59        foreach (explode(':', $phpIncludePath) as $path) {
60            $this->registerPath($path, "%s.php");
61        }
62        // Add user-defined paths
63        if (isset($paths)) {
64            foreach($paths as $path) {
65                $realpath = realpath($path["path"]);
66                $this->registerPath($realpath, $path["format"]);
67            }
68        }
69    }
70
71    /**
72     * Add a new path to the list of paths where mvblog_AutoLoader will look for classes.
73     * @param string $path The path. This will be automatically expanded to an absolute pathname (i.e. ../../foo will become /var/www/foo/, depending on your current path).
74     * @param string $format (optional) The format of filenames to look for. %s will be expanded to the classname that needs to be loaded. Example: "class_%s.php".
75     * @return bool false if path is not a dir or not set.
76     */
77    public function registerPath($path, $format = "%s.php", $options = 0) {
78        $path = realpath($path);
79        if ($path === false || !is_dir($path))
80            return(false);
81
82        $pathInfo = array("path" => $path, "format" => $format, "options" => $options);
83        $this->paths[] = $pathInfo;
84
85        return(true);
86    }
87
88    /**
89     * The autoloader. It looks through all the paths and tries to find a file that contains the class to include.
90     * This method should be called from an __autoload() function in your initializing code.
91     *
92     * @param string className. The name of the class to load.
93     */
94    public function autoload($className) {
95        foreach($this->paths as $path) {
96            if ($path["options"] & mvblog_AutoLoader::OPT_LOWERCASE)
97                $finalClassName = strtolower($className);
98            else
99                $finalClassName = $className;
100
101            $filename = $path["path"] . '/' . sprintf($path["format"], $finalClassName);
102            if (file_exists($filename)) {
103                include_once($filename);
104                return(true);
105            }
106        }
107    }
108}
109?>
Note: See TracBrowser for help on using the browser.