[ticket/12932] Add method for getting the datetime class and allow overriding

An extension can now override the datetime class by overwriting the
datetime.class parameter in their own yml file.

PHPBB3-12932
This commit is contained in:
Marc Alexander 2014-08-10 12:55:49 +02:00
parent 7cc9f216e7
commit a57a96cbdd
6 changed files with 31 additions and 6 deletions

View file

@ -0,0 +1,2 @@
parameters:
datetime.class: \phpbb\datetime

View file

@ -10,6 +10,7 @@ imports:
- { resource: mimetype_guessers.yml } - { resource: mimetype_guessers.yml }
- { resource: passwords.yml } - { resource: passwords.yml }
- { resource: profilefields.yml } - { resource: profilefields.yml }
- { resource: parameters.yml }
services: services:
acl.permissions: acl.permissions:
@ -346,6 +347,8 @@ services:
user: user:
class: phpbb\user class: phpbb\user
arguments:
- %datetime.class%
user_loader: user_loader:
class: phpbb\user_loader class: phpbb\user_loader

View file

@ -1061,7 +1061,7 @@ function phpbb_timezone_select($user, $default = '', $truncate = false)
foreach ($unsorted_timezones as $timezone) foreach ($unsorted_timezones as $timezone)
{ {
$tz = new DateTimeZone($timezone); $tz = new DateTimeZone($timezone);
$dt = new \phpbb\datetime($user, 'now', $tz); $dt = $user->create_datetime($user, 'now', $tz);
$offset = $dt->getOffset(); $offset = $dt->getOffset();
$current_time = $dt->format($user->lang['DATETIME_FORMAT'], true); $current_time = $dt->format($user->lang['DATETIME_FORMAT'], true);
$offset_string = phpbb_format_timezone_offset($offset); $offset_string = phpbb_format_timezone_offset($offset);
@ -4881,7 +4881,7 @@ function page_header($page_title = '', $display_online_list = false, $item_id =
} }
} }
$dt = new \phpbb\datetime($user, 'now', $user->timezone); $dt = $user->create_datetime($user, 'now', $user->timezone);
$timezone_offset = 'GMT' . phpbb_format_timezone_offset($dt->getOffset()); $timezone_offset = 'GMT' . phpbb_format_timezone_offset($dt->getOffset());
$timezone_name = $user->timezone->getName(); $timezone_name = $user->timezone->getName();
if (isset($user->lang['timezones'][$timezone_name])) if (isset($user->lang['timezones'][$timezone_name]))

View file

@ -240,7 +240,7 @@ $sub = $request->variable('sub', '');
// Set PHP error handler to ours // Set PHP error handler to ours
set_error_handler(defined('PHPBB_MSG_HANDLER') ? PHPBB_MSG_HANDLER : 'msg_handler'); set_error_handler(defined('PHPBB_MSG_HANDLER') ? PHPBB_MSG_HANDLER : 'msg_handler');
$user = new \phpbb\user(); $user = new \phpbb\user('\phpbb\datetime');
$auth = new \phpbb\auth\auth(); $auth = new \phpbb\auth\auth();
// Add own hook handler, if present. :o // Add own hook handler, if present. :o

View file

@ -121,7 +121,7 @@ class soft_delete_mod_convert extends \phpbb\db\migration\migration
new \phpbb\auth\auth(), new \phpbb\auth\auth(),
$this->config, $this->config,
$this->db, $this->db,
new \phpbb\user(), new \phpbb\user('\phpbb\datetime'),
$this->phpbb_root_path, $this->phpbb_root_path,
$this->php_ext, $this->php_ext,
$this->table_prefix . 'forums', $this->table_prefix . 'forums',

View file

@ -31,6 +31,11 @@ class user extends \phpbb\session
*/ */
public $timezone; public $timezone;
/**
* @var string Class name of datetime object
*/
protected $datetime;
var $lang_name = false; var $lang_name = false;
var $lang_id = false; var $lang_id = false;
var $lang_path; var $lang_path;
@ -42,12 +47,14 @@ class user extends \phpbb\session
/** /**
* Constructor to set the lang path * Constructor to set the lang path
* @param string $datetime_class Class name of datetime class
*/ */
function __construct() function __construct($datetime_class)
{ {
global $phpbb_root_path; global $phpbb_root_path;
$this->lang_path = $phpbb_root_path . 'language/'; $this->lang_path = $phpbb_root_path . 'language/';
$this->datetime = $datetime_class;
} }
/** /**
@ -727,7 +734,7 @@ class user extends \phpbb\session
public function create_datetime($time = 'now', \DateTimeZone $timezone = null) public function create_datetime($time = 'now', \DateTimeZone $timezone = null)
{ {
$timezone = $timezone ?: $this->timezone; $timezone = $timezone ?: $this->timezone;
return new \phpbb\datetime($this, $time, $timezone); return new $this->datetime($this, $time, $timezone);
} }
/** /**
@ -918,4 +925,17 @@ class user extends \phpbb\session
return $forum_ids; return $forum_ids;
} }
/**
* Set class name of datetime class
*
* @param string $datetime Class name to set
* @return null
*/
public function set_datetime_class($datetime)
{
$this->datetime = $datetime;
}
} }