diff options
| author | Cullen Walsh <ckwalsh@phpbb.com> | 2011-04-17 19:29:41 -0700 |
|---|---|---|
| committer | Cullen Walsh <ckwalsh@cullenwalsh.com> | 2012-03-18 22:19:16 +0000 |
| commit | 1bd3d40121960c203d0dabb4b1a04c16c564b6f1 (patch) | |
| tree | 30262400ec8bacd8b671a83757aa6821d1ecd895 | |
| parent | 4c699e0d0acc0aafab37e36206a92b1919282dac (diff) | |
| download | forums-1bd3d40121960c203d0dabb4b1a04c16c564b6f1.tar forums-1bd3d40121960c203d0dabb4b1a04c16c564b6f1.tar.gz forums-1bd3d40121960c203d0dabb4b1a04c16c564b6f1.tar.bz2 forums-1bd3d40121960c203d0dabb4b1a04c16c564b6f1.tar.xz forums-1bd3d40121960c203d0dabb4b1a04c16c564b6f1.zip | |
[feature/avatars] Refactor avatars to use manager
Manager now stores singletons of each driver to speed loading.
PHPBB3-10018
| -rw-r--r-- | phpBB/includes/avatar/driver.php (renamed from phpBB/includes/avatars/avatar_base.php) | 34 | ||||
| -rw-r--r-- | phpBB/includes/avatar/manager.php | 91 | ||||
| -rw-r--r-- | phpBB/includes/functions_display.php | 51 |
3 files changed, 131 insertions, 45 deletions
diff --git a/phpBB/includes/avatars/avatar_base.php b/phpBB/includes/avatar/driver.php index c84a6e8a7f..777b225e84 100644 --- a/phpBB/includes/avatars/avatar_base.php +++ b/phpBB/includes/avatar/driver.php @@ -1,7 +1,7 @@ <?php /** * -* @package avatars +* @package avatar * @copyright (c) 2005, 2009 phpBB Group * @license http://opensource.org/licenses/gpl-license.php GNU Public License * @@ -16,16 +16,28 @@ if (!defined('IN_PHPBB')) } /** -* Base class for avatar modules +* Base class for avatar drivers * @package avatars */ -class phpbb_avatar_base +abstract class phpbb_avatar_driver { /** - * User data this avatar may use to generate a url or html - * @type array + * Current board configuration + * @type phpbb_config + */ + protected $config; + + /** + * Current $phpbb_root_path + * @type string + */ + protected $phpbb_root_path; + + /** + * Current $phpEx + * @type string */ - private $user_row; + protected $php_ext; /** * This flag should be set to true if the avatar requires a nonstandard image @@ -39,9 +51,11 @@ class phpbb_avatar_base * * @param $user_row User data to base the avatar url/html on */ - public function __construct(&$user_row) + public function __construct(phpbb_config $config, $phpbb_root_path, $php_ext) { - $this->user_row = $user_row; + $this->config = $config; + $this->phpbb_root_path = $phpbb_root_path; + $this->php_ext = $php_ext; } /** @@ -51,7 +65,7 @@ class phpbb_avatar_base * should be ignored * @return array Avatar data */ - public function get_data($ignore_config = false) + public function get_data($user_row, $ignore_config = false) { return array( 'src' => '', @@ -68,7 +82,7 @@ class phpbb_avatar_base * should be ignored * @return string HTML */ - public function get_custom_html($ignore_config = false) + public function get_custom_html($user_row, $ignore_config = false) { return ''; } diff --git a/phpBB/includes/avatar/manager.php b/phpBB/includes/avatar/manager.php new file mode 100644 index 0000000000..04a4d8f425 --- /dev/null +++ b/phpBB/includes/avatar/manager.php @@ -0,0 +1,91 @@ +<?php +/** +* +* @package avatar +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +/** +* @ignore +*/ +if (!defined('IN_PHPBB')) +{ + exit; +} + +/** +* @package acm +*/ +class phpbb_avatar_manager +{ + private $phpbb_root_path; + private $php_ext; + private $config; + private $cache; + private static $valid_drivers = false; + + public function __construct($phpbb_root_path, $php_ext = '.php', phpbb_config $config, phpbb_cache_driver_interface $cache = null) + { + $this->phpbb_root_path = $phpbb_root_path; + $this->php_ext = $php_ext; + $this->config = $config; + $this->cache = $cache; + } + + public function get_singleton($avatar_type) + { + if (self::$valid_drivers === false) + { + $this->load_valid_drivers(); + } + + if (isset(self::$valid_drivers[$avatar_type])) + { + if (!is_object(self::$valid_drivers[$avatar_type])) + { + $class_name = 'phpbb_avatar_driver_' . $avatar_type; + self::$valid_drivers[$avatar_type] = new $class_name($this->config, $this->phpbb_root_path, $this->php_ext); + } + + return self::$valid_drivers[$avatar_type]; + } + else + { + return null; + } + } + + private function load_valid_drivers() + { + require_once($this->phpbb_root_path . 'includes/avatar/driver.' . $this->php_ext); + + if ($this->cache) + { + self::$valid_drivers = $this->cache->get('avatar_drivers'); + } + + if (empty($this->valid_drivers)) + { + self::$valid_drivers = array(); + + $iterator = new DirectoryIterator($this->phpbb_root_path . 'includes/avatar/driver'); + + foreach ($iterator as $file) + { + if (preg_match("/^(.*)\.{$this->php_ext}$/", $file, $match)) + { + self::$valid_drivers[] = $match[1]; + } + } + + self::$valid_drivers = array_flip(self::$valid_drivers); + + if ($this->cache) + { + $this->cache->put('avatar_drivers', self::$valid_drivers); + } + } + } +} diff --git a/phpBB/includes/functions_display.php b/phpBB/includes/functions_display.php index eba123be9d..3aeee5f704 100644 --- a/phpBB/includes/functions_display.php +++ b/phpBB/includes/functions_display.php @@ -1278,10 +1278,12 @@ function get_user_rank($user_rank, $user_posts, &$rank_title, &$rank_img, &$rank * * @return string Avatar html */ -function get_user_avatar(&$user_row, $alt = 'USER_AVATAR', $ignore_config = false) +function get_user_avatar($user_row, $alt = 'USER_AVATAR', $ignore_config = false) { global $user, $config, $cache, $phpbb_root_path, $phpEx; + static $avatar_manager = null; + if (!$config['allow_avatar'] && !$ignore_config) { return ''; @@ -1334,48 +1336,27 @@ function get_user_avatar(&$user_row, $alt = 'USER_AVATAR', $ignore_config = fals break; default: - $class = 'phpbb_avatar_' . $user_row['user_avatar_type']; - - if (!class_exists($class)) + if (empty($avatar_manager)) { - $avatar_types = $cache->get('avatar_types'); - - if (empty($avatar_types)) - { - $avatar_types = array(); - - if ($dh = @opendir($phpbb_root_path . 'includes/avatars')) - { - while ($file = @readdir($dh)) - { - if (preg_match("/avatar_(.*)\.$phpEx/", $file, $match)) - { - $avatar_types[] = $match[1]; - } - } - - @closedir($dh); + $avatar_manager = new phpbb_avatar_manager($phpbb_root_path, $phpEx, $config, $cache->get_driver()); + } - sort($avatar_types); - $cache->put('avatar_types', $avatar_types); - } - } + $avatar = $avatar_manager->get_singleton($user_row['user_avatar_type']); - if (in_array($user_row['user_avatar_type'], $avatar_types)) + if ($avatar) + { + if ($avatar->custom_html) { - require_once($phpbb_root_path . 'includes/avatars/avatar_' . $user_row['user_avatar_type'] . '.' . $phpEx); + return $avatar->get_html($user_row, $ignore_config); } - } - - $avatar = new $class($user_row); - if ($avatar->custom_html) + $avatar_data = $avatar->get_data($user_row, $ignore_config); + } + else { - return $avatar->get_custom_html($ignore_config); + $avatar_data['src'] = ''; } - $avatar_data = $avatar->get_data($ignore_config); - break; } @@ -1401,7 +1382,7 @@ function get_user_avatar(&$user_row, $alt = 'USER_AVATAR', $ignore_config = fals * * @return string Avatar html */ -function get_group_avatar(&$group_row, $alt = 'GROUP_AVATAR', $ignore_config = false) +function get_group_avatar($group_row, $alt = 'GROUP_AVATAR', $ignore_config = false) { // Kind of abusing this functionality... $avatar_row = array( |
