SOURCES: autoload.php (NEW) - global autoload class: require 'autoload.php'...

glen glen at pld-linux.org
Tue Jan 13 13:48:39 CET 2009


Author: glen                         Date: Tue Jan 13 12:48:39 2009 GMT
Module: SOURCES                       Tag: HEAD
---- Log message:
- global autoload class: require 'autoload.php' to init

---- Files affected:
SOURCES:
   autoload.php (NONE -> 1.1)  (NEW)

---- Diffs:

================================================================
Index: SOURCES/autoload.php
diff -u /dev/null SOURCES/autoload.php:1.1
--- /dev/null	Tue Jan 13 13:48:39 2009
+++ SOURCES/autoload.php	Tue Jan 13 13:48:33 2009
@@ -0,0 +1,75 @@
+<?php
+/**
+ * Global Autoload class.
+ *
+ * @author Tarmo Lehtpuu <tarmo.lehtpuu at delfi.ee>
+ * @author Tanel Suurhans <tanel.suurhans at delfi.ee>
+ * @author Elan Ruusamäe <glen at delfi.ee>
+ *
+ * @package autoload
+ */
+class PHP_Autoload {
+
+	private static $excludes = array('.', '..', '.svn', 'CVS');
+	private static $classes;
+
+	public static function autoload($className) {
+		if (class_exists($className, false) || interface_exists($className, false)) {
+			return;
+		}
+
+		if (!is_array(self::$classes)) {
+			self::$classes = array();
+
+			// scan framework dir
+			foreach (explode(PATH_SEPARATOR, get_include_path()) as $path) {
+				self::scan($path);
+			}
+		}
+
+		// Zend framework
+		if (strpos($className, 'Zend') === 0){
+			require_once str_replace('_', '/', $className) . '.php';
+
+			// Smarty
+		} else if ($className === 'Smarty') {
+			require_once 'Smarty/Smarty.class.php';
+
+			// sql_pool
+		} else if ($className === 'sql_pool') {
+			require_once 'class.sql_pool.php';
+
+		} else {
+			// the rest
+			if (array_key_exists($className, self::$classes)) {
+				require_once self::$classes[$className];
+			}
+		}
+	}
+
+	private static function scan($path) {
+		$dh = opendir($path);
+		while (($file = readdir($dh)) !== false) {
+			if (array_search($file, self::$excludes) === false) {
+				if (is_dir($path . DIRECTORY_SEPARATOR . $file)) {
+					self::scan($path . DIRECTORY_SEPARATOR . $file);
+					continue;
+				}
+				$tokens = explode('.', $file);
+				if (count($tokens) != 2) {
+					continue;
+				}
+
+				$ext = $tokens[1];
+				$class = $tokens[0];
+
+				if ($ext == 'php') {
+					self::$classes[$class] = $path . DIRECTORY_SEPARATOR . $file;
+				}
+			}
+		}
+		closedir($dh);
+	}
+}
+
+spl_autoload_register(array('PHP_Autoload', 'autoload'));
================================================================


More information about the pld-cvs-commit mailing list