mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-28 06:08:52 +00:00
[feature/avatars] Refactor avatars to use manager
Manager now stores singletons of each driver to speed loading. PHPBB3-10018
This commit is contained in:
parent
4c699e0d0a
commit
1bd3d40121
3 changed files with 133 additions and 47 deletions
|
@ -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
|
||||
*/
|
||||
private $user_row;
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* Current $phpbb_root_path
|
||||
* @type string
|
||||
*/
|
||||
protected $phpbb_root_path;
|
||||
|
||||
/**
|
||||
* Current $phpEx
|
||||
* @type string
|
||||
*/
|
||||
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 '';
|
||||
}
|
91
phpBB/includes/avatar/manager.php
Normal file
91
phpBB/includes/avatar/manager.php
Normal file
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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,47 +1336,26 @@ 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);
|
||||
|
||||
sort($avatar_types);
|
||||
$cache->put('avatar_types', $avatar_types);
|
||||
}
|
||||
}
|
||||
|
||||
if (in_array($user_row['user_avatar_type'], $avatar_types))
|
||||
{
|
||||
require_once($phpbb_root_path . 'includes/avatars/avatar_' . $user_row['user_avatar_type'] . '.' . $phpEx);
|
||||
}
|
||||
$avatar_manager = new phpbb_avatar_manager($phpbb_root_path, $phpEx, $config, $cache->get_driver());
|
||||
}
|
||||
|
||||
$avatar = new $class($user_row);
|
||||
$avatar = $avatar_manager->get_singleton($user_row['user_avatar_type']);
|
||||
|
||||
if ($avatar->custom_html)
|
||||
if ($avatar)
|
||||
{
|
||||
return $avatar->get_custom_html($ignore_config);
|
||||
}
|
||||
if ($avatar->custom_html)
|
||||
{
|
||||
return $avatar->get_html($user_row, $ignore_config);
|
||||
}
|
||||
|
||||
$avatar_data = $avatar->get_data($ignore_config);
|
||||
$avatar_data = $avatar->get_data($user_row, $ignore_config);
|
||||
}
|
||||
else
|
||||
{
|
||||
$avatar_data['src'] = '';
|
||||
}
|
||||
|
||||
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(
|
||||
|
|
Loading…
Add table
Reference in a new issue