Add Horde_Autoloader.
authorMichael M Slusarz <slusarz@curecanti.org>
Sun, 9 Nov 2008 06:58:39 +0000 (23:58 -0700)
committerMichael M Slusarz <slusarz@curecanti.org>
Sun, 9 Nov 2008 06:58:39 +0000 (23:58 -0700)
framework/Autoloader/lib/Horde/Autoloader.php [new file with mode: 0644]
framework/Autoloader/package.xml [new file with mode: 0644]

diff --git a/framework/Autoloader/lib/Horde/Autoloader.php b/framework/Autoloader/lib/Horde/Autoloader.php
new file mode 100644 (file)
index 0000000..1d3dd17
--- /dev/null
@@ -0,0 +1,103 @@
+<?php
+/**
+ * Horde Autoloader.
+ *
+ * @category Horde
+ * @package  Horde_Autoloader
+ * @license  http://www.gnu.org/copyleft/lesser.html
+ */
+class Horde_Autoloader
+{
+    /**
+     * Patterns that match classes we can load.
+     * @var array
+     */
+    protected static $_classPatterns = array('/^Horde_/' => 'Horde/');
+
+    /**
+     * Autoload implementation automatically registered with
+     * spl_autoload_register.
+     *
+     * We ignore E_WARNINGS when trying to include files so that if our
+     * autoloader doesn't find a file, we pass on to the next autoloader (if
+     * any) or to the PHP class not found error. We don't want to suppress all
+     * errors, though, or else we'll end up silencing parse errors or
+     * redefined class name errors, making debugging especially difficult.
+     *
+     * @param string $class  Class name to load (or interface).
+     */
+    public static function loadClass($class)
+    {
+        foreach (self::$_classPatterns as $pattern => $replace) {
+            $file = $class;
+
+            if (!is_null($replace)) {
+                $file = preg_replace($pattern, $replace, $file);
+            }
+
+            if (!is_null($replace) || preg_match($pattern, $file)) {
+                $file = str_replace(array('::', '_'), '/', $file) . '.php';
+                $oldErrorReporting = error_reporting(E_ALL ^ E_WARNING);
+                $res = include_once $file;
+                error_reporting($oldErrorReporting);
+                if ($res) {
+                    return true;
+                }
+            }
+        }
+    }
+
+    /**
+     * Add a new path to the include_path we're loading from.
+     *
+     * @param string $path      The directory to add.
+     * @param boolean $prepend  Add to the beginning of the stack?
+     *
+     * @return string  The new include_path.
+     */
+    public static function addClassPath($path, $prepend = true)
+    {
+        $include_path = get_include_path();
+        if ($include_path == $path
+            || strpos($include_path, PATH_SEPARATOR . $path)
+            || strpos($include_path, $path . PATH_SEPARATOR) !== false) {
+            // The path is already present in our stack; don't re-add it.
+            return $include_path;
+        }
+
+        if ($prepend) {
+            $include_path = $path . PATH_SEPARATOR . $include_path;
+        } else {
+            $include_path .= PATH_SEPARATOR . $path;
+        }
+        set_include_path($include_path);
+
+        return $include_path;
+    }
+
+    /**
+     * Add a new class pattern.
+     *
+     * @param string $pattern  The class pattern to add.
+     * @param string $replace  The substitution pattern.
+     */
+    public static function addClassPattern($pattern, $replace = null)
+    {
+        self::$_classPatterns[$pattern] = $replace;
+    }
+
+}
+
+/* Register the autoloader in a way to play well with as many configurations
+ * as possible. */
+if (function_exists('spl_autoload_register')) {
+    spl_autoload_register(array('Horde_Autoloader', 'loadClass'));
+    if (function_exists('__autoload')) {
+        spl_autoload_register('__autoload');
+    }
+} elseif (!function_exists('__autoload')) {
+    function __autoload($class)
+    {
+        return Horde_Autoloader::loadClass($class);
+    }
+}
diff --git a/framework/Autoloader/package.xml b/framework/Autoloader/package.xml
new file mode 100644 (file)
index 0000000..2b7538e
--- /dev/null
@@ -0,0 +1,61 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<package packagerversion="1.4.9" version="2.0" xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0
+http://pear.php.net/dtd/tasks-1.0.xsd
+http://pear.php.net/dtd/package-2.0
+http://pear.php.net/dtd/package-2.0.xsd">
+ <name>Autoloader</name>
+ <channel>pear.horde.org</channel>
+ <summary>Horde Class Autoloader</summary>
+ <description>Autoload implementation and class loading manager for Horde
+ </description>
+ <lead>
+  <name>Chuck Hagenbuch</name>
+  <user>chuck</user>
+  <email>chuck@horde.org</email>
+  <active>yes</active>
+ </lead>
+ <lead>
+  <name>Jan Schneider</name>
+  <user>jan</user>
+  <email>jan@horde.org</email>
+  <active>yes</active>
+ </lead>
+ <date>2008-09-02</date>
+ <time>14:00:00</time>
+ <version>
+  <release>0.2.0</release>
+  <api>0.2.0</api>
+ </version>
+ <stability>
+  <release>beta</release>
+  <api>beta</api>
+ </stability>
+ <license uri="http://www.gnu.org/copyleft/lesser.html">LGPL</license>
+ <notes>Rename to Horde_Autoloader.
+ </notes>
+ <contents>
+  <dir name="/">
+   <dir name="lib">
+    <dir name="Horde">
+     <file name="Autoloader.php" role="php" />
+    </dir> <!-- /lib/Horde -->
+   </dir> <!-- /lib -->
+  </dir> <!-- / -->
+ </contents>
+ <dependencies>
+  <required>
+   <php>
+    <min>5.2</min>
+   </php>
+   <pearinstaller>
+    <min>1.5</min>
+   </pearinstaller>
+  </required>
+ </dependencies>
+ <phprelease>
+  <filelist>
+   <install name="lib/Horde/Autoloader.php" as="Horde/Autoloader.php" />
+  </filelist>
+ </phprelease>
+ <changelog/>
+</package>