[feature/avatars] Rewrite drivers to use full class name

* Use full driver class name as avatar_type value
* Move avatar drivers to core namespace
* Make avatars installable through extensions

PHPBB3-10018
This commit is contained in:
Igor Wiedler 2012-06-27 21:02:07 +02:00
parent 13f4bfabbe
commit df16bd1c49
9 changed files with 68 additions and 38 deletions

View file

@ -130,7 +130,7 @@ $phpbb_style = new phpbb_style($phpbb_root_path, $phpEx, $config, $user, $phpbb_
$phpbb_subscriber_loader = new phpbb_event_extension_subscriber_loader($phpbb_dispatcher, $phpbb_extension_manager); $phpbb_subscriber_loader = new phpbb_event_extension_subscriber_loader($phpbb_dispatcher, $phpbb_extension_manager);
$phpbb_subscriber_loader->load(); $phpbb_subscriber_loader->load();
$phpbb_avatar_manager = new phpbb_avatar_manager($phpbb_root_path, $phpEx, $config, $request, $cache->get_driver()); $phpbb_avatar_manager = new phpbb_avatar_manager($phpbb_root_path, $phpEx, $config, $request, $phpbb_extension_manager, $cache->get_driver());
// Add own hook handler // Add own hook handler
require($phpbb_root_path . 'includes/hooks/index.' . $phpEx); require($phpbb_root_path . 'includes/hooks/index.' . $phpEx);

View file

@ -82,7 +82,7 @@ if (isset($_GET['avatar']))
$phpbb_subscriber_loader = new phpbb_event_extension_subscriber_loader($phpbb_dispatcher, $phpbb_extension_manager); $phpbb_subscriber_loader = new phpbb_event_extension_subscriber_loader($phpbb_dispatcher, $phpbb_extension_manager);
$phpbb_subscriber_loader->load(); $phpbb_subscriber_loader->load();
$phpbb_avatar_manager = new phpbb_avatar_manager($phpbb_root_path, $phpEx, $config, $request, $cache->get_driver()); $phpbb_avatar_manager = new phpbb_avatar_manager($phpbb_root_path, $phpEx, $config, $request, $phpbb_extension_manager, $cache->get_driver());
$filename = request_var('avatar', ''); $filename = request_var('avatar', '');
$avatar_group = false; $avatar_group = false;

View file

@ -19,7 +19,7 @@ if (!defined('IN_PHPBB'))
* Handles avatars selected from the board gallery * Handles avatars selected from the board gallery
* @package avatars * @package avatars
*/ */
class phpbb_avatar_driver_local extends phpbb_avatar_driver class phpbb_avatar_driver_core_local extends phpbb_avatar_driver
{ {
/** /**
* @inheritdoc * @inheritdoc

View file

@ -19,7 +19,7 @@ if (!defined('IN_PHPBB'))
* Handles avatars hosted remotely * Handles avatars hosted remotely
* @package avatars * @package avatars
*/ */
class phpbb_avatar_driver_remote extends phpbb_avatar_driver class phpbb_avatar_driver_core_remote extends phpbb_avatar_driver
{ {
/** /**
* @inheritdoc * @inheritdoc

View file

@ -19,7 +19,7 @@ if (!defined('IN_PHPBB'))
* Handles avatars uploaded to the board * Handles avatars uploaded to the board
* @package avatars * @package avatars
*/ */
class phpbb_avatar_driver_upload extends phpbb_avatar_driver class phpbb_avatar_driver_core_upload extends phpbb_avatar_driver
{ {
/** /**
* @inheritdoc * @inheritdoc

View file

@ -119,4 +119,25 @@ abstract class phpbb_avatar_driver implements phpbb_avatar_driver_interface
{ {
return true; return true;
} }
/**
* @inheritdoc
**/
public function is_enabled()
{
$driver = preg_replace('#^phpbb_avatar_driver_core_#', '', get_class($this));
return $this->config["allow_avatar_$driver"];
}
/**
* @inheritdoc
**/
public function get_template_name()
{
$driver = preg_replace('#^phpbb_avatar_driver_core_#', '', get_class($this));
$template = "ucp_avatar_options_$driver.html";
return $template;
}
} }

View file

@ -57,4 +57,14 @@ interface phpbb_avatar_driver_interface
* @TODO * @TODO
**/ **/
public function delete($row); public function delete($row);
/**
* @TODO
**/
public function is_enabled();
/**
* @TODO
**/
public function get_template_name();
} }

View file

@ -24,25 +24,27 @@ class phpbb_avatar_manager
private $phpEx; private $phpEx;
private $config; private $config;
private $request; private $request;
private $extension_manager;
private $cache; private $cache;
private static $valid_drivers = false; private static $valid_drivers = false;
/** /**
* @TODO * @TODO
**/ **/
public function __construct($phpbb_root_path, $phpEx, phpbb_config $config, phpbb_request $request, phpbb_cache_driver_interface $cache = null) public function __construct($phpbb_root_path, $phpEx, phpbb_config $config, phpbb_request $request, phpbb_extension_manager $extension_manager, phpbb_cache_driver_interface $cache = null)
{ {
$this->phpbb_root_path = $phpbb_root_path; $this->phpbb_root_path = $phpbb_root_path;
$this->phpEx = $phpEx; $this->phpEx = $phpEx;
$this->config = $config; $this->config = $config;
$this->request = $request; $this->request = $request;
$this->extension_manager = $extension_manager;
$this->cache = $cache; $this->cache = $cache;
} }
/** /**
* @TODO * @TODO
**/ **/
public function get_driver($avatar_type, $new = false) public function get_driver($avatar_type)
{ {
if (self::$valid_drivers === false) if (self::$valid_drivers === false)
{ {
@ -53,30 +55,33 @@ class phpbb_avatar_manager
switch ($avatar_type) switch ($avatar_type)
{ {
case AVATAR_GALLERY: case AVATAR_GALLERY:
$avatar_type = 'local'; $avatar_type = 'phpbb_avatar_driver_local';
break; break;
case AVATAR_UPLOAD: case AVATAR_UPLOAD:
$avatar_type = 'upload'; $avatar_type = 'phpbb_avatar_driver_upload';
break; break;
case AVATAR_REMOTE: case AVATAR_REMOTE:
$avatar_type = 'remote'; $avatar_type = 'phpbb_avatar_driver_remote';
break; break;
} }
if (isset(self::$valid_drivers[$avatar_type])) if (false === array_search($avatar_type, self::$valid_drivers))
{
if ($new || !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->request, $this->phpbb_root_path, $this->phpEx, $this->cache);
}
return self::$valid_drivers[$avatar_type];
}
else
{ {
return null; return null;
} }
$r = new ReflectionClass($avatar_type);
if ($r->isSubClassOf('phpbb_avatar_driver')) {
$driver = new $avatar_type($this->config, $this->request, $this->phpbb_root_path, $this->phpEx, $this->cache);
} else if ($r->implementsInterface('phpbb_avatar_driver')) {
$driver = new $avatar_type();
} else {
$message = "Invalid avatar driver class name '%s' provided. It must implement phpbb_avatar_driver_interface.";
trigger_error(sprintf($message, $avatar_type));
}
return $driver;
} }
/** /**
@ -93,18 +98,12 @@ class phpbb_avatar_manager
{ {
self::$valid_drivers = array(); self::$valid_drivers = array();
$iterator = new DirectoryIterator($this->phpbb_root_path . 'includes/avatar/driver'); $finder = $this->extension_manager->get_finder();
foreach ($iterator as $file) self::$valid_drivers = $finder
{ ->extension_directory('/avatar/driver/')
// Match all files that appear to be php files ->core_path('includes/avatar/driver/core/')
if (preg_match("/^(.*)\.{$this->phpEx}$/", $file, $match)) ->get_classes();
{
self::$valid_drivers[] = $match[1];
}
}
self::$valid_drivers = array_flip(self::$valid_drivers);
if ($this->cache) if ($this->cache)
{ {
@ -123,7 +122,7 @@ class phpbb_avatar_manager
$this->load_valid_drivers(); $this->load_valid_drivers();
} }
return array_keys(self::$valid_drivers); return self::$valid_drivers;
} }
/** /**

View file

@ -623,18 +623,18 @@ class ucp_profile
} }
$focused_driver = request_var('avatar_driver', $user->data['user_avatar_type']); $focused_driver = request_var('avatar_driver', $user->data['user_avatar_type']);
foreach ($avatar_drivers as $driver) foreach ($avatar_drivers as $driver)
{ {
if ($config["allow_avatar_$driver"]) $avatar = $phpbb_avatar_manager->get_driver($driver);
if ($avatar->is_enabled())
{ {
$avatars_enabled = true; $avatars_enabled = true;
$template->set_filenames(array( $template->set_filenames(array(
'avatar' => "ucp_avatar_options_$driver.html", 'avatar' => $avatar->get_template_name(),
)); ));
$avatar = $phpbb_avatar_manager->get_driver($driver);
if ($avatar->prepare_form($template, $avatar_data, $error)) if ($avatar->prepare_form($template, $avatar_data, $error))
{ {
$driver_u = strtoupper($driver); $driver_u = strtoupper($driver);