From 7030578bbe9e11c18b5becaf8b06e670e3c2e3cd Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Sun, 14 Jul 2013 01:32:34 -0400 Subject: [ticket/11698] Moving all autoloadable files to phpbb/ PHPBB3-11698 --- phpBB/phpbb/class_loader.php | 170 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 phpBB/phpbb/class_loader.php (limited to 'phpBB/phpbb/class_loader.php') diff --git a/phpBB/phpbb/class_loader.php b/phpBB/phpbb/class_loader.php new file mode 100644 index 0000000000..02a2d584dc --- /dev/null +++ b/phpBB/phpbb/class_loader.php @@ -0,0 +1,170 @@ +path. + * This map is stored in cache and looked up if the cache is available. + * + * @var array + */ + private $cached_paths = array(); + + /** + * Creates a new phpbb_class_loader, which loads files with the given + * file extension from the given path. + * + * @param string $prefix Required class name prefix for files to be loaded + * @param string $path Directory to load files from + * @param string $php_ext The file extension for PHP files + * @param phpbb_cache_driver_interface $cache An implementation of the phpBB cache interface. + */ + public function __construct($prefix, $path, $php_ext = 'php', phpbb_cache_driver_interface $cache = null) + { + $this->prefix = $prefix; + $this->path = $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 phpbb_cache_driver_interface $cache An implementation of the phpBB cache interface. + */ + public function set_cache(phpbb_cache_driver_interface $cache = null) + { + if ($cache) + { + $this->cached_paths = $cache->get('class_loader_' . $this->prefix); + + 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) + { + if (isset($this->cached_paths[$class])) + { + return $this->path . $this->cached_paths[$class] . '.' . $this->php_ext; + } + + if (!preg_match('/^' . $this->prefix . '[a-zA-Z0-9_]+$/', $class)) + { + return false; + } + + $parts = explode('_', substr($class, strlen($this->prefix))); + + $dirs = ''; + + for ($i = 0, $n = sizeof($parts); $i < $n && is_dir($this->path . $dirs . $parts[$i]); $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($this->path . $relative_path . '.' . $this->php_ext)) + { + return false; + } + + if ($this->cache) + { + $this->cached_paths[$class] = $relative_path; + $this->cache->put('class_loader_' . $this->prefix, $this->cached_paths); + } + + return $this->path . $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, strlen($this->prefix)) === $this->prefix) + { + $path = $this->resolve_path($class); + + if ($path) + { + require $path; + } + } + } +} -- cgit v1.2.1 From da2752e4004b296ae5acdd08b7c0a758d8f61e9d Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Sun, 14 Jul 2013 13:30:52 -0400 Subject: [ticket/11700] Modify all code to use the new interface names PHPBB3-11700 --- phpBB/phpbb/class_loader.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'phpBB/phpbb/class_loader.php') diff --git a/phpBB/phpbb/class_loader.php b/phpBB/phpbb/class_loader.php index 02a2d584dc..78467a044c 100644 --- a/phpBB/phpbb/class_loader.php +++ b/phpBB/phpbb/class_loader.php @@ -50,9 +50,9 @@ class phpbb_class_loader * @param string $prefix Required class name prefix for files to be loaded * @param string $path Directory to load files from * @param string $php_ext The file extension for PHP files - * @param phpbb_cache_driver_interface $cache An implementation of the phpBB cache interface. + * @param phpbb_cache_driver_driver_interface $cache An implementation of the phpBB cache interface. */ - public function __construct($prefix, $path, $php_ext = 'php', phpbb_cache_driver_interface $cache = null) + public function __construct($prefix, $path, $php_ext = 'php', phpbb_cache_driver_driver_interface $cache = null) { $this->prefix = $prefix; $this->path = $path; @@ -66,9 +66,9 @@ class phpbb_class_loader * the class loader will resolve paths by checking for the existance of every * directory in the class name every time. * - * @param phpbb_cache_driver_interface $cache An implementation of the phpBB cache interface. + * @param phpbb_cache_driver_driver_interface $cache An implementation of the phpBB cache interface. */ - public function set_cache(phpbb_cache_driver_interface $cache = null) + public function set_cache(phpbb_cache_driver_driver_interface $cache = null) { if ($cache) { -- cgit v1.2.1 From d4d3d311b926cf3d0df4b3c6ccc4731a0345ca18 Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Sun, 14 Jul 2013 13:54:56 -0400 Subject: [ticket/11700] Implement namespace loading in the class loader PHPBB3-11700 --- phpBB/phpbb/class_loader.php | 49 ++++++++++++++++---------------------------- 1 file changed, 18 insertions(+), 31 deletions(-) (limited to 'phpBB/phpbb/class_loader.php') diff --git a/phpBB/phpbb/class_loader.php b/phpBB/phpbb/class_loader.php index 78467a044c..bcd05e5853 100644 --- a/phpBB/phpbb/class_loader.php +++ b/phpBB/phpbb/class_loader.php @@ -7,6 +7,8 @@ * */ +namespace phpbb; + /** * @ignore */ @@ -28,9 +30,9 @@ if (!defined('IN_PHPBB')) * * @package phpBB3 */ -class phpbb_class_loader +class class_loader { - private $prefix; + private $namespace; private $path; private $php_ext; private $cache; @@ -44,17 +46,17 @@ class phpbb_class_loader private $cached_paths = array(); /** - * Creates a new phpbb_class_loader, which loads files with the given + * Creates a new \phpbb\class_loader, which loads files with the given * file extension from the given path. * - * @param string $prefix Required class name prefix for files to be loaded + * @param string $namespace Required namespace for files to be loaded * @param string $path Directory to load files from * @param string $php_ext The file extension for PHP files - * @param phpbb_cache_driver_driver_interface $cache An implementation of the phpBB cache interface. + * @param \phpbb\cache\driver\driver_interface $cache An implementation of the phpBB cache interface. */ - public function __construct($prefix, $path, $php_ext = 'php', phpbb_cache_driver_driver_interface $cache = null) + public function __construct($namespace, $path, $php_ext = 'php', \phpbb\cache\driver\driver_interface $cache = null) { - $this->prefix = $prefix; + $this->namespace = $namespace; $this->path = $path; $this->php_ext = $php_ext; @@ -66,13 +68,13 @@ class phpbb_class_loader * the class loader will resolve paths by checking for the existance of every * directory in the class name every time. * - * @param phpbb_cache_driver_driver_interface $cache An implementation of the phpBB cache interface. + * @param \phpbb\cache\driver\driver_interface $cache An implementation of the phpBB cache interface. */ - public function set_cache(phpbb_cache_driver_driver_interface $cache = null) + public function set_cache(\phpbb\cache\driver\driver_interface $cache = null) { if ($cache) { - $this->cached_paths = $cache->get('class_loader_' . $this->prefix); + $this->cached_paths = $cache->get('class_loader_' . str_replace('\\', '__', $this->namespace)); if ($this->cached_paths === false) { @@ -102,8 +104,8 @@ class phpbb_class_loader /** * 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 + * @param string $class The class name to resolve, must be in the + * namespace the loader was constructed with * @return string|bool A relative path to the file containing the * class or false if looking it up failed. */ @@ -114,27 +116,12 @@ class phpbb_class_loader return $this->path . $this->cached_paths[$class] . '.' . $this->php_ext; } - if (!preg_match('/^' . $this->prefix . '[a-zA-Z0-9_]+$/', $class)) + if (!preg_match('/^' . preg_quote($this->namespace, '/') . '[a-zA-Z0-9_\\\\]+$/', $class)) { return false; } - $parts = explode('_', substr($class, strlen($this->prefix))); - - $dirs = ''; - - for ($i = 0, $n = sizeof($parts); $i < $n && is_dir($this->path . $dirs . $parts[$i]); $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), '_'); + $relative_path = str_replace('\\', '/', substr($class, strlen($this->namespace))); if (!file_exists($this->path . $relative_path . '.' . $this->php_ext)) { @@ -144,7 +131,7 @@ class phpbb_class_loader if ($this->cache) { $this->cached_paths[$class] = $relative_path; - $this->cache->put('class_loader_' . $this->prefix, $this->cached_paths); + $this->cache->put('class_loader_' . str_replace('\\', '__', $this->namespace), $this->cached_paths); } return $this->path . $relative_path . '.' . $this->php_ext; @@ -157,7 +144,7 @@ class phpbb_class_loader */ public function load_class($class) { - if (substr($class, 0, strlen($this->prefix)) === $this->prefix) + if (substr($class, 0, strlen($this->namespace)) === $this->namespace) { $path = $this->resolve_path($class); -- cgit v1.2.1 From fe36375a36ec4f816eb07b41630b6c9fa7ff12c8 Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Thu, 19 Sep 2013 18:29:08 +0200 Subject: [ticket/11700] Fix extension loading with namespaces class loader now expects all classes to be prefixed with a backslash when resolving paths PHPBB3-11700 --- phpBB/phpbb/class_loader.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'phpBB/phpbb/class_loader.php') diff --git a/phpBB/phpbb/class_loader.php b/phpBB/phpbb/class_loader.php index bcd05e5853..769f28b4f1 100644 --- a/phpBB/phpbb/class_loader.php +++ b/phpBB/phpbb/class_loader.php @@ -55,7 +55,12 @@ class class_loader * @param \phpbb\cache\driver\driver_interface $cache An implementation of the phpBB cache interface. */ public function __construct($namespace, $path, $php_ext = 'php', \phpbb\cache\driver\driver_interface $cache = null) - { + { + if ($namespace[0] !== '\\') + { + $namespace = '\\' . $namespace; + } + $this->namespace = $namespace; $this->path = $path; $this->php_ext = $php_ext; @@ -105,7 +110,8 @@ class class_loader * Resolves a phpBB class name to a relative path which can be included. * * @param string $class The class name to resolve, must be in the - * namespace the loader was constructed with + * namespace the loader was constructed with. + * Has to begin with \ * @return string|bool A relative path to the file containing the * class or false if looking it up failed. */ @@ -144,6 +150,7 @@ class class_loader */ public function load_class($class) { + $class = '\\' . $class; if (substr($class, 0, strlen($this->namespace)) === $this->namespace) { $path = $this->resolve_path($class); -- cgit v1.2.1 From 7aa8f6461f1e85cf91931f56b95384e54fec07c2 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Wed, 30 Oct 2013 13:05:28 +0100 Subject: [task/code-sniffer] Remove the IN_PHPBB check side-effect from class files. PHPBB3-11980 --- phpBB/phpbb/class_loader.php | 8 -------- 1 file changed, 8 deletions(-) (limited to 'phpBB/phpbb/class_loader.php') diff --git a/phpBB/phpbb/class_loader.php b/phpBB/phpbb/class_loader.php index 769f28b4f1..5fe2c16aa4 100644 --- a/phpBB/phpbb/class_loader.php +++ b/phpBB/phpbb/class_loader.php @@ -9,14 +9,6 @@ namespace phpbb; -/** -* @ignore -*/ -if (!defined('IN_PHPBB')) -{ - exit; -} - /** * The class loader resolves class names to file system paths and loads them if * necessary. -- cgit v1.2.1 From ef1f99183796f8e246f96bca54ca439bf8ea1750 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Wed, 30 Oct 2013 13:37:29 +0100 Subject: [task/code-sniffer] Replace spaces with tabs. PHPBB3-11980 --- phpBB/phpbb/class_loader.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/phpbb/class_loader.php') diff --git a/phpBB/phpbb/class_loader.php b/phpBB/phpbb/class_loader.php index 5fe2c16aa4..37b62fff24 100644 --- a/phpBB/phpbb/class_loader.php +++ b/phpBB/phpbb/class_loader.php @@ -47,7 +47,7 @@ class class_loader * @param \phpbb\cache\driver\driver_interface $cache An implementation of the phpBB cache interface. */ public function __construct($namespace, $path, $php_ext = 'php', \phpbb\cache\driver\driver_interface $cache = null) - { + { if ($namespace[0] !== '\\') { $namespace = '\\' . $namespace; -- cgit v1.2.1 From bd0ba372078d10a093bd09b1b58839fa7b39daf0 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Fri, 28 Mar 2014 17:33:36 +0100 Subject: [ticket/12314] Workaround HHVM SPL autoloader sometimes using leading backslash PHPBB3-12314 --- phpBB/phpbb/class_loader.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'phpBB/phpbb/class_loader.php') diff --git a/phpBB/phpbb/class_loader.php b/phpBB/phpbb/class_loader.php index 37b62fff24..ee9767148b 100644 --- a/phpBB/phpbb/class_loader.php +++ b/phpBB/phpbb/class_loader.php @@ -142,7 +142,13 @@ class class_loader */ public function load_class($class) { - $class = '\\' . $class; + // In general $class is not supposed to contain a leading backslash, + // but sometimes it does. See tickets PHP-50731 and HHVM-1840. + if ($class[0] !== '\\') + { + $class = '\\' . $class; + } + if (substr($class, 0, strlen($this->namespace)) === $this->namespace) { $path = $this->resolve_path($class); -- cgit v1.2.1 From a759704b39fc1c1353f865a633759b1369589b67 Mon Sep 17 00:00:00 2001 From: Yuriy Rusko Date: Tue, 27 May 2014 20:18:06 +0200 Subject: [ticket/12594] Remove @package tags and update file headers PHPBB3-12594 --- phpBB/phpbb/class_loader.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'phpBB/phpbb/class_loader.php') diff --git a/phpBB/phpbb/class_loader.php b/phpBB/phpbb/class_loader.php index ee9767148b..cfdcc2af0b 100644 --- a/phpBB/phpbb/class_loader.php +++ b/phpBB/phpbb/class_loader.php @@ -1,9 +1,13 @@ +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. * */ @@ -19,8 +23,6 @@ namespace phpbb; * * 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 class_loader { -- cgit v1.2.1