1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-08-06 16:56:44 +02:00

[ticket/11103] Use phpBB Container to load types/methods

PHPBB3-11103
This commit is contained in:
Nathaniel Guse
2012-11-09 07:40:08 -06:00
parent 03e348cf58
commit f09ee16252
25 changed files with 505 additions and 75 deletions

View File

@@ -7,6 +7,8 @@
*
*/
use Symfony\Component\DependencyInjection\ContainerBuilder;
/**
* @ignore
*/
@@ -21,6 +23,9 @@ if (!defined('IN_PHPBB'))
*/
class phpbb_notification_manager
{
/** @var ContainerBuilder */
protected $phpbb_container = null;
/** @var dbal */
protected $db = null;
@@ -55,8 +60,9 @@ class phpbb_notification_manager
*/
protected $users = array();
public function __construct(dbal $db, phpbb_cache_driver_interface $cache, phpbb_template $template, phpbb_extension_manager $extension_manager, $user, phpbb_auth $auth, phpbb_config $config, $phpbb_root_path, $php_ext)
public function __construct(ContainerBuilder $phpbb_container, dbal $db, phpbb_cache_driver_interface $cache, phpbb_template $template, phpbb_extension_manager $extension_manager, $user, phpbb_auth $auth, phpbb_config $config, $phpbb_root_path, $php_ext)
{
$this->phpbb_container = $phpbb_container;
$this->db = $db;
$this->cache = $cache;
$this->template = $template;
@@ -495,17 +501,17 @@ class phpbb_notification_manager
{
$subscription_types = array();
foreach ($this->get_subscription_files('notification/type/') as $class_name)
foreach ($this->phpbb_container->findTaggedServiceIds('notification.type') as $type_name => $data)
{
$class = $this->get_item_type_class($class_name);
$type = $this->get_item_type_class($type_name);
if ($class instanceof phpbb_notification_type_interface && $class->is_available())
if ($type instanceof phpbb_notification_type_interface && $type->is_available())
{
$options = array_merge(array(
'id' => $class_name,
'lang' => 'NOTIFICATION_TYPE_' . strtoupper($class_name),
'id' => $type->get_type(),
'lang' => 'NOTIFICATION_TYPE_' . strtoupper($type->get_type()),
'group' => 'NOTIFICATION_GROUP_MISCELLANEOUS',
), (($class_name::$notification_option !== false) ? $class_name::$notification_option : array()));
), (($type::$notification_option !== false) ? $type::$notification_option : array()));
$subscription_types[$options['group']][$options['id']] = $options;
}
@@ -531,13 +537,16 @@ class phpbb_notification_manager
{
$subscription_methods = array();
foreach ($this->get_subscription_files('notification/method/') as $class_name)
foreach ($this->phpbb_container->findTaggedServiceIds('notification.method') as $method_name => $data)
{
$method = $this->get_method_class($class_name);
$method = $this->get_method_class($method_name);
if ($method instanceof phpbb_notification_method_interface && $method->is_available())
{
$subscription_methods[] = $class_name;
$subscription_methods[$method_name] = array(
'id' => $method->get_type(),
'lang' => 'NOTIFICATION_METHOD_' . strtoupper($method->get_type()),
);
}
}
@@ -783,7 +792,7 @@ class phpbb_notification_manager
*/
public function get_item_type_class($item_type, $data = array())
{
$item = new $item_type($this, $this->db, $this->cache, $this->template, $this->extension_manager, $this->user, $this->auth, $this->config, $this->phpbb_root_path, $this->php_ext);
$item = $this->phpbb_container->get($item_type);
$item->set_initial_data($data);
@@ -795,43 +804,6 @@ class phpbb_notification_manager
*/
public function get_method_class($method_name)
{
return new $method_name($this, $this->db, $this->cache, $this->template, $this->extension_manager, $this->user, $this->auth, $this->config, $this->phpbb_root_path, $this->php_ext);
}
/**
* Helper to get subscription related files with the finder
*/
private function get_subscription_files($path)
{
$finder = $this->extension_manager->get_finder();
$subscription_files = array();
$classes = $finder
->core_path('includes/' . $path)
->extension_directory($path)
->get_classes();
if (array_search('phpbb_notification_type_interface', $classes) !== false)
{
unset($classes[array_search('phpbb_notification_type_interface', $classes)]);
}
if (array_search('phpbb_notification_type_base', $classes) !== false)
{
unset($classes[array_search('phpbb_notification_type_base', $classes)]);
}
if (array_search('phpbb_notification_method_interface', $classes) !== false)
{
unset($classes[array_search('phpbb_notification_method_interface', $classes)]);
}
if (array_search('phpbb_notification_method_base', $classes) !== false)
{
unset($classes[array_search('phpbb_notification_method_base', $classes)]);
}
return $classes;
return $this->phpbb_container->get($method_name);
}
}

View File

@@ -23,6 +23,16 @@ if (!defined('IN_PHPBB'))
*/
class phpbb_notification_method_email extends phpbb_notification_method_base
{
/**
* Get notification method name
*
* @return string
*/
public function get_type()
{
return 'email';
}
/**
* Notify method (since jabber gets sent through the same messenger, we let the jabber class inherit from this to reduce code duplication)
*

View File

@@ -21,6 +21,13 @@ if (!defined('IN_PHPBB'))
*/
interface phpbb_notification_method_interface
{
/**
* Get notification method name
*
* @return string
*/
public function get_type();
/**
* Is this method available for the user?
* This is checked on the notifications options

View File

@@ -23,6 +23,16 @@ if (!defined('IN_PHPBB'))
*/
class phpbb_notification_method_jabber extends phpbb_notification_method_email
{
/**
* Get notification method name
*
* @return string
*/
public function get_type()
{
return 'jabber';
}
/**
* Notify method (since jabber gets sent through the same messenger, we let the jabber class inherit from this to reduce code duplication)
*

View File

@@ -23,6 +23,16 @@ if (!defined('IN_PHPBB'))
*/
class phpbb_notification_type_approve_post extends phpbb_notification_type_post
{
/**
* Get notification type name
*
* @return string
*/
public function get_type()
{
return 'approve_post';
}
/**
* Language key used to output the text
*

View File

@@ -23,6 +23,16 @@ if (!defined('IN_PHPBB'))
*/
class phpbb_notification_type_approve_topic extends phpbb_notification_type_topic
{
/**
* Get notification type name
*
* @return string
*/
public function get_type()
{
return 'approve_topic';
}
/**
* Language key used to output the text
*

View File

@@ -116,7 +116,7 @@ abstract class phpbb_notification_type_base implements phpbb_notification_type_i
public function __toString()
{
return (!empty($this->data)) ? var_export($this->data, true) : get_class($this);
return (!empty($this->data)) ? var_export($this->data, true) : $this->get_type();
}
/**
@@ -156,7 +156,7 @@ abstract class phpbb_notification_type_base implements phpbb_notification_type_i
// Defaults
$this->data = array_merge(array(
'item_id' => static::get_item_id($type_data),
'item_type' => get_class($this),
'item_type' => $this->get_type(),
'item_parent_id' => static::get_item_parent_id($type_data),
'time' => time(),
@@ -319,7 +319,7 @@ abstract class phpbb_notification_type_base implements phpbb_notification_type_i
{
$options = array_merge(array(
'ignore_users' => array(),
'item_type' => get_class($this),
'item_type' => $this->get_type(),
'item_id' => 0, // Global by default
), $options);

View File

@@ -23,6 +23,16 @@ if (!defined('IN_PHPBB'))
*/
class phpbb_notification_type_bookmark extends phpbb_notification_type_post
{
/**
* Get notification type name
*
* @return string
*/
public function get_type()
{
return 'bookmark';
}
/**
* Language key used to output the text
*
@@ -93,7 +103,7 @@ class phpbb_notification_type_bookmark extends phpbb_notification_type_post
$update_notifications = array();
$sql = 'SELECT *
FROM ' . NOTIFICATIONS_TABLE . "
WHERE item_type = '" . get_class($this) . "'
WHERE item_type = '" . $this->get_type() . "'
AND item_parent_id = " . (int) self::get_item_parent_id($post) . '
AND unread = 1
AND is_enabled = 1';
@@ -103,7 +113,7 @@ class phpbb_notification_type_bookmark extends phpbb_notification_type_post
// Do not create a new notification
unset($notify_users[$row['user_id']]);
$notification = $this->notification_manager->get_item_type_class(get_class($this), $row);
$notification = $this->notification_manager->get_item_type_class($this->get_type(), $row);
$sql = 'UPDATE ' . NOTIFICATIONS_TABLE . '
SET ' . $this->db->sql_build_array('UPDATE', $notification->add_responders($post)) . '
WHERE notification_id = ' . $row['notification_id'];

View File

@@ -23,6 +23,16 @@ if (!defined('IN_PHPBB'))
*/
class phpbb_notification_type_disapprove_post extends phpbb_notification_type_approve_post
{
/**
* Get notification type name
*
* @return string
*/
public function get_type()
{
return 'disapprove_post';
}
/**
* Language key used to output the text
*

View File

@@ -23,6 +23,16 @@ if (!defined('IN_PHPBB'))
*/
class phpbb_notification_type_disapprove_topic extends phpbb_notification_type_approve_topic
{
/**
* Get notification type name
*
* @return string
*/
public function get_type()
{
return 'disapprove_topic';
}
/**
* Language key used to output the text
*

View File

@@ -21,6 +21,13 @@ if (!defined('IN_PHPBB'))
*/
interface phpbb_notification_type_interface
{
/**
* Get notification type name
*
* @return string
*/
public function get_type();
/**
* Set initial data from the database
*

View File

@@ -23,6 +23,16 @@ if (!defined('IN_PHPBB'))
*/
class phpbb_notification_type_pm extends phpbb_notification_type_base
{
/**
* Get notification type name
*
* @return string
*/
public function get_type()
{
return 'pm';
}
/**
* Notification option data (for outputting to the user)
*

View File

@@ -23,6 +23,16 @@ if (!defined('IN_PHPBB'))
*/
class phpbb_notification_type_post extends phpbb_notification_type_base
{
/**
* Get notification type name
*
* @return string
*/
public function get_type()
{
return 'post';
}
/**
* Language key used to output the text
*
@@ -114,7 +124,7 @@ class phpbb_notification_type_post extends phpbb_notification_type_base
$update_notifications = array();
$sql = 'SELECT *
FROM ' . NOTIFICATIONS_TABLE . "
WHERE item_type = '" . get_class($this) . "'
WHERE item_type = '" . $this->get_type() . "'
AND item_parent_id = " . (int) self::get_item_parent_id($post) . '
AND unread = 1
AND is_enabled = 1';
@@ -124,7 +134,7 @@ class phpbb_notification_type_post extends phpbb_notification_type_base
// Do not create a new notification
unset($notify_users[$row['user_id']]);
$notification = $this->notification_manager->get_item_type_class(get_class($this), $row);
$notification = $this->notification_manager->get_item_type_class($this->get_type(), $row);
$sql = 'UPDATE ' . NOTIFICATIONS_TABLE . '
SET ' . $this->db->sql_build_array('UPDATE', $notification->add_responders($post)) . '
WHERE notification_id = ' . $row['notification_id'];

View File

@@ -23,6 +23,16 @@ if (!defined('IN_PHPBB'))
*/
class phpbb_notification_type_post_in_queue extends phpbb_notification_type_post
{
/**
* Get notification type name
*
* @return string
*/
public function get_type()
{
return 'post_in_queue';
}
/**
* Language key used to output the text
*

View File

@@ -23,6 +23,16 @@ if (!defined('IN_PHPBB'))
*/
class phpbb_notification_type_quote extends phpbb_notification_type_post
{
/**
* Get notification type name
*
* @return string
*/
public function get_type()
{
return 'quote';
}
/**
* regular expression to match to find usernames
*
@@ -112,7 +122,7 @@ class phpbb_notification_type_quote extends phpbb_notification_type_post
$update_notifications = array();
$sql = 'SELECT *
FROM ' . NOTIFICATIONS_TABLE . "
WHERE item_type = '" . get_class($this) . "'
WHERE item_type = '" . $this->get_type() . "'
AND item_parent_id = " . (int) self::get_item_parent_id($post) . '
AND unread = 1
AND is_enabled = 1';
@@ -122,7 +132,7 @@ class phpbb_notification_type_quote extends phpbb_notification_type_post
// Do not create a new notification
unset($notify_users[$row['user_id']]);
$notification = $this->notification_manager->get_item_type_class(get_class($this), $row);
$notification = $this->notification_manager->get_item_type_class($this->get_type(), $row);
$sql = 'UPDATE ' . NOTIFICATIONS_TABLE . '
SET ' . $this->db->sql_build_array('UPDATE', $notification->add_responders($post)) . '
WHERE notification_id = ' . $row['notification_id'];
@@ -143,7 +153,7 @@ class phpbb_notification_type_quote extends phpbb_notification_type_post
$old_notifications = array();
$sql = 'SELECT user_id
FROM ' . NOTIFICATIONS_TABLE . "
WHERE item_type = '" . get_class($this) . "'
WHERE item_type = '" . $this->get_type() . "'
AND item_id = " . self::get_item_id($post) . '
AND is_enabled = 1';
$result = $this->db->sql_query($sql);
@@ -167,13 +177,13 @@ class phpbb_notification_type_quote extends phpbb_notification_type_post
}
// Add the necessary notifications
$this->notification_manager->add_notifications_for_users(get_class($this), $post, $add_notifications);
$this->notification_manager->add_notifications_for_users($this->get_type(), $post, $add_notifications);
// Remove the necessary notifications
if (!empty($remove_notifications))
{
$sql = 'DELETE FROM ' . NOTIFICATIONS_TABLE . "
WHERE item_type = '" . get_class($this) . "'
WHERE item_type = '" . $this->get_type() . "'
AND item_id = " . self::get_item_id($post) . '
AND ' . $this->db->sql_in_set('user_id', $remove_notifications);
$this->db->sql_query($sql);

View File

@@ -23,6 +23,16 @@ if (!defined('IN_PHPBB'))
*/
class phpbb_notification_type_report_pm extends phpbb_notification_type_pm
{
/**
* Get notification type name
*
* @return string
*/
public function get_type()
{
return 'report_pm';
}
/**
* Language key used to output the text
*

View File

@@ -23,6 +23,16 @@ if (!defined('IN_PHPBB'))
*/
class phpbb_notification_type_report_pm_closed extends phpbb_notification_type_pm
{
/**
* Get notification type name
*
* @return string
*/
public function get_type()
{
return 'report_pm_closed';
}
/**
* Email template to use to send notifications
*

View File

@@ -23,6 +23,16 @@ if (!defined('IN_PHPBB'))
*/
class phpbb_notification_type_report_post extends phpbb_notification_type_post_in_queue
{
/**
* Get notification type name
*
* @return string
*/
public function get_type()
{
return 'report_post';
}
/**
* Language key used to output the text
*

View File

@@ -23,6 +23,16 @@ if (!defined('IN_PHPBB'))
*/
class phpbb_notification_type_report_post_closed extends phpbb_notification_type_post
{
/**
* Get notification type name
*
* @return string
*/
public function get_type()
{
return 'report_post_closed';
}
/**
* Email template to use to send notifications
*

View File

@@ -23,6 +23,16 @@ if (!defined('IN_PHPBB'))
*/
class phpbb_notification_type_topic extends phpbb_notification_type_base
{
/**
* Get notification type name
*
* @return string
*/
public function get_type()
{
return 'topic';
}
/**
* Language key used to output the text
*

View File

@@ -23,6 +23,16 @@ if (!defined('IN_PHPBB'))
*/
class phpbb_notification_type_topic_in_queue extends phpbb_notification_type_topic
{
/**
* Get notification type name
*
* @return string
*/
public function get_type()
{
return 'topic_in_queue';
}
/**
* Language key used to output the text
*

View File

@@ -48,15 +48,15 @@ class ucp_notifications
{
foreach($subscription_types as $type => $data)
{
foreach($notification_methods as $method)
foreach($notification_methods as $method => $method_data)
{
if ($request->is_set_post($type . '_' . $method) && (!isset($subscriptions[$type]) || !in_array($method, $subscriptions[$type])))
if ($request->is_set_post($type . '_' . $method_data['id']) && (!isset($subscriptions[$type]) || !in_array($method_data['id'], $subscriptions[$type])))
{
$phpbb_notifications->add_subscription($type, 0, $method);
$phpbb_notifications->add_subscription($type, 0, $method_data['id']);
}
else if (!$request->is_set_post($type . '_' . $method) && isset($subscriptions[$type]) && in_array($method, $subscriptions[$type]))
else if (!$request->is_set_post($type . '_' . $method_data['id']) && isset($subscriptions[$type]) && in_array($method_data['id'], $subscriptions[$type]))
{
$phpbb_notifications->delete_subscription($type, 0, $method);
$phpbb_notifications->delete_subscription($type, 0, $method_data['id']);
}
}
@@ -186,14 +186,14 @@ class ucp_notifications
'SUBSCRIBED' => (isset($subscriptions[$type])) ? true : false,
));
foreach($notification_methods as $method)
foreach($notification_methods as $method => $method_data)
{
$template->assign_block_vars($block . '.notification_methods', array(
'METHOD' => $method,
'METHOD' => $method_data['id'],
'NAME' => $user->lang(strtoupper($method)),
'NAME' => $user->lang($method_data['lang']),
'SUBSCRIBED' => (isset($subscriptions[$type]) && in_array($method, $subscriptions[$type])) ? true : false,
'SUBSCRIBED' => (isset($subscriptions[$type]) && in_array($method_data['id'], $subscriptions[$type])) ? true : false,
));
}
}
@@ -212,12 +212,12 @@ class ucp_notifications
{
$notification_methods = $phpbb_notifications->get_subscription_methods();
foreach($notification_methods as $method)
foreach($notification_methods as $method => $method_data)
{
$template->assign_block_vars($block, array(
'METHOD' => $method,
'METHOD' => $method_data['id'],
'NAME' => $user->lang(strtoupper($method)),
'NAME' => $user->lang($method_data['lang']),
));
}
}