Source for file mvblog_autoloader.php

Documentation is available at mvblog_autoloader.php

  1. <?php
  2. /**
  3.  * MvBlog -- An open source no-nosense blogtool
  4.  *
  5.  * Copyright (C) 2005-2006, 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-2006 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.  */
  41.     const OPT_LOWERCASE 1
  42.  
  43.     protected $paths = array();
  44.  
  45.     /**
  46.      * Create a new mvblog_AutoLoader object. It will automatically parse
  47.      * your PHP ini include_path for paths in which is should look
  48.      * for objects. The default format it will look for is "CLASSNAME.php"
  49.      * (case-sensitive). The current directory is usually included in
  50.      * your include_path, so you won't have to add that. Add new
  51.      * paths using ->registerPath($path, $format).
  52.      *
  53.      * @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)));
  54.      */
  55.     public function __construct($paths null{
  56.         // Add paths in include_path ini setting of PHP.
  57.         $phpIncludePath ini_get("include_path");
  58.         foreach (explode(':'$phpIncludePathas $path{
  59.             $this->registerPath($path"%s.php");
  60.         }
  61.         // Add user-defined paths
  62.         if (isset($paths)) {
  63.             foreach($paths as $path{
  64.                 $realpath realpath($path["path"]);
  65.                 $this->registerPath($realpath$path["format"]);
  66.             }
  67.         }
  68.     }
  69.  
  70.     /**
  71.      * Add a new path to the list of paths where mvblog_AutoLoader will look for classes.
  72.      * @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).
  73.      * @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".
  74.      * @return bool false if path is not a dir or not set.
  75.      */
  76.     public function registerPath($path$format "%s.php"$options 0{
  77.         $path realpath($path);
  78.         if ($path === false || !is_dir($path)) 
  79.             return(false);
  80.  
  81.         $pathInfo array("path" => $path"format" => $format"options" => $options);
  82.         $this->paths[$pathInfo;
  83.  
  84.         return(true);
  85.     }
  86.  
  87.     /**
  88.      * The autoloader. It looks through all the paths and tries to find a file that contains the class to include.
  89.      * This method should be called from an __autoload() function in your initializing code.
  90.      * 
  91.      * @param string className. The name of the class to load.
  92.      */
  93.     public function autoload($className{
  94.         foreach($this->paths as $path{
  95.             if ($path["options"mvblog_AutoLoader::OPT_LOWERCASE)
  96.                 $finalClassName strtolower($className);
  97.             else
  98.                 $finalClassName $className;
  99.  
  100.             $filename $path["path"'/' sprintf($path["format"]$finalClassName);
  101.             if (file_exists($filename)) {
  102.                 include_once($filename);
  103.                 return(true);
  104.             }
  105.         }
  106.     }
  107. }
  108. ?>

Documentation generated on Fri, 28 Dec 2007 13:17:38 +0100 by phpDocumentor 1.4.1