aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/includes/class_loader.php
diff options
context:
space:
mode:
authorNils Adermann <naderman@naderman.de>2010-06-25 14:24:04 +0200
committerNils Adermann <naderman@naderman.de>2010-07-05 15:40:46 +0200
commit22c3041e128bd2d10d37672bdf5c7c1eb4112b12 (patch)
tree07f1ace82305fecee57cd6260c49278df3e637d4 /phpBB/includes/class_loader.php
parent55a22363fa4e0cc49a55478ccd238b078003fd78 (diff)
downloadforums-22c3041e128bd2d10d37672bdf5c7c1eb4112b12.tar
forums-22c3041e128bd2d10d37672bdf5c7c1eb4112b12.tar.gz
forums-22c3041e128bd2d10d37672bdf5c7c1eb4112b12.tar.bz2
forums-22c3041e128bd2d10d37672bdf5c7c1eb4112b12.tar.xz
forums-22c3041e128bd2d10d37672bdf5c7c1eb4112b12.zip
[feature/auto-loading] Added a phpBB autoloader with caching support.
phpBB class name lookups follow these rules: - All classes are prefixed with phpbb_ - All classes reside in includes/ or a subdirectory thereof - Directories must not contain underscores - The class name is separated into parts by underscores, the parts are checked from first to last, until one is found which is not a directory, all remaining parts make up the file name. If no parts are left, the last directory name is used. Examples: directory structure: includes/ class_name.php dir/ class_name.php dir.php subdir/ class_name.php lookups: phpbb_class_name -> includes/class_name.php phpbb_dir_class_name -> includes/dir/class_name.php phpbb_dir -> includes/dir/dir.php phpbb_dir_subdir_class_name -> includes/dir/subdir/class_name.php Optionally the class can be supplied with a cache instance, either in the constructor or via set_cache() at a later time. This allows for the lookups to be cached, so the directories do not have to be traveresed on every request. This makes it necessary for the cache and its dependency to continue to be loaded the old way - without autoloading. The code will not be changed to use autoloading, but it will rather only be used for new classes where applicable. PHPBB3-9682
Diffstat (limited to 'phpBB/includes/class_loader.php')
-rw-r--r--phpBB/includes/class_loader.php162
1 files changed, 162 insertions, 0 deletions
diff --git a/phpBB/includes/class_loader.php b/phpBB/includes/class_loader.php
new file mode 100644
index 0000000000..c70351b437
--- /dev/null
+++ b/phpBB/includes/class_loader.php
@@ -0,0 +1,162 @@
+<?php
+/**
+*
+* @package phpBB3
+* @version $Id$
+* @copyright (c) 2005 phpBB Group
+* @license http://opensource.org/licenses/gpl-license.php GNU Public License
+*
+*/
+
+/**
+* @ignore
+*/
+if (!defined('IN_PHPBB'))
+{
+ exit;
+}
+
+/**
+* The class loader resolves class names to file system paths and loads them if
+* necessary.
+*
+* Classes have to be of the form phpbb_(dir_)*(classpart_)*, so directory names
+* must never contain underscores. Example: phpbb_dir_subdir_class_name is a
+* valid class name, while phpbb_dir_sub_dir_class_name is not.
+*
+* If every part of the class name is a directory, the last directory name is
+* also used as the filename, e.g. phpbb_dir would resolve to dir/dir.php.
+*
+* @package phpBB3
+*/
+class phpbb_class_loader
+{
+ private $phpbb_root_path;
+ private $php_ext;
+ private $cache;
+ private $cached_paths = array();
+
+ /**
+ * Creates a new phpbb_class_loader, which loads files with the given
+ * file extension from the given phpbb root path.
+ *
+ * @param string $phpbb_root_path phpBB's root directory containing includes/
+ * @param string $php_ext The file extension for PHP files
+ */
+ public function __construct($phpbb_root_path, $php_ext = '.php', $cache = null)
+ {
+ $this->phpbb_root_path = $phpbb_root_path;
+ $this->php_ext = $php_ext;
+
+ $this->set_cache($cache);
+ }
+
+ /**
+ * Provide the class loader with a cache to store paths. If set to null, the
+ * the class loader will resolve paths by checking for the existance of every
+ * directory in the class name every time.
+ *
+ * @param acm $cache An implementation of the phpBB cache interface.
+ */
+ public function set_cache($cache = null)
+ {
+ if ($cache)
+ {
+ $this->cached_paths = $cache->get('class_loader');
+
+ if ($this->cached_paths === false)
+ {
+ $this->cached_paths = array();
+ }
+ }
+
+ $this->cache = $cache;
+ }
+
+ /**
+ * Registers the class loader as an autoloader using SPL.
+ */
+ public function register()
+ {
+ spl_autoload_register(array($this, 'load_class'));
+ }
+
+ /**
+ * Removes the class loader from the SPL autoloader stack.
+ */
+ public function unregister()
+ {
+ spl_autoload_unregister(array($this, 'load_class'));
+ }
+
+ /**
+ * Resolves a phpBB class name to a relative path which can be included.
+ *
+ * @param string $class The class name to resolve, must have a phpbb_
+ * prefix
+ * @return string|bool A relative path to the file containing the
+ * class or false if looking it up failed.
+ */
+ public function resolve_path($class)
+ {
+ $path_prefix = $this->phpbb_root_path . 'includes/';
+
+ if (isset($this->cached_paths[$class]))
+ {
+ return $path_prefix . $this->cached_paths[$class] . $this->php_ext;
+ }
+
+ if (!preg_match('/phpbb_[a-zA-Z0-9_]+/', $class))
+ {
+ return false;
+ }
+
+ $parts = explode('_', substr($class, 6));
+
+ $dirs = '';
+
+ for ($i = 0; is_dir($path_prefix . $dirs . $parts[$i]) && $i < sizeof($parts); $i++)
+ {
+ $dirs .= $parts[$i] . '/';
+ }
+
+ // no file name left => use last dir name as file name
+ if ($i == sizeof($parts))
+ {
+ $parts[] = $parts[$i - 1];
+ }
+
+ $relative_path = $dirs . implode(array_slice($parts, $i, sizeof($parts) - $i), '_');
+
+ if (!file_exists($path_prefix . $relative_path . $this->php_ext))
+ {
+ return false;
+ }
+
+ if ($this->cache)
+ {
+ $this->cached_paths[$class] = $relative_path;
+ $this->cache->put('class_loader', $this->cached_paths);
+ }
+
+ return $path_prefix . $relative_path . $this->php_ext;
+ }
+
+ /**
+ * Resolves a class name to a path and then includes it.
+ *
+ * @param string $class The class name which is being loaded.
+ */
+ public function load_class($class)
+ {
+ if (substr($class, 0, 6) === 'phpbb_')
+ {
+ $path = $this->resolve_path($class);
+
+ if ($path)
+ {
+ require $path;
+ }
+ }
+ }
+}