Skip to content

Commit dad1882

Browse files
authored
Convert mod_messages to service provider (#42735)
* Convert mod_messages to service provider
1 parent 7c45ae8 commit dad1882

File tree

5 files changed

+169
-37
lines changed

5 files changed

+169
-37
lines changed

administrator/modules/mod_messages/mod_messages.php

Lines changed: 0 additions & 36 deletions
This file was deleted.

administrator/modules/mod_messages/mod_messages.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
<authorUrl>www.joomla.org</authorUrl>
1010
<version>4.0.0</version>
1111
<description>MOD_MESSAGES_XML_DESCRIPTION</description>
12+
<namespace path="src">Joomla\Module\Messages</namespace>
1213
<files>
13-
<filename module="mod_messages">mod_messages.php</filename>
14+
<folder module="mod_messages">services</folder>
15+
<folder>src</folder>
1416
<folder>tmpl</folder>
1517
</files>
1618
<languages>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/**
4+
* @package Joomla.Administrator
5+
* @subpackage mod_messages
6+
*
7+
* @copyright (C) 2024 Open Source Matters, Inc. <https://www.joomla.org>
8+
* @license GNU General Public License version 2 or later; see LICENSE.txt
9+
*/
10+
11+
use Joomla\CMS\Extension\Service\Provider\HelperFactory;
12+
use Joomla\CMS\Extension\Service\Provider\Module;
13+
use Joomla\CMS\Extension\Service\Provider\ModuleDispatcherFactory;
14+
use Joomla\DI\Container;
15+
use Joomla\DI\ServiceProviderInterface;
16+
17+
// phpcs:disable PSR1.Files.SideEffects
18+
\defined('_JEXEC') or die;
19+
// phpcs:enable PSR1.Files.SideEffects
20+
21+
/**
22+
* The messages module service provider.
23+
*
24+
* @since __DEPLOY_VERSION__
25+
*/
26+
return new class () implements ServiceProviderInterface {
27+
/**
28+
* Registers the service provider with a DI container.
29+
*
30+
* @param Container $container The DI container.
31+
*
32+
* @return void
33+
*
34+
* @since __DEPLOY_VERSION__
35+
*/
36+
public function register(Container $container)
37+
{
38+
$container->registerServiceProvider(new ModuleDispatcherFactory('\\Joomla\\Module\\Messages'));
39+
$container->registerServiceProvider(new HelperFactory('\\Joomla\\Module\\Messages\\Administrator\\Helper'));
40+
41+
$container->registerServiceProvider(new Module());
42+
}
43+
};
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
/**
4+
* @package Joomla.Administrator
5+
* @subpackage mod_messages
6+
*
7+
* @copyright (C) 2024 Open Source Matters, Inc. <https://www.joomla.org>
8+
* @license GNU General Public License version 2 or later; see LICENSE.txt
9+
*/
10+
11+
namespace Joomla\Module\Messages\Administrator\Dispatcher;
12+
13+
use Joomla\CMS\Dispatcher\AbstractModuleDispatcher;
14+
use Joomla\CMS\Helper\HelperFactoryAwareInterface;
15+
use Joomla\CMS\Helper\HelperFactoryAwareTrait;
16+
17+
// phpcs:disable PSR1.Files.SideEffects
18+
\defined('_JEXEC') or die;
19+
// phpcs:enable PSR1.Files.SideEffects
20+
21+
/**
22+
* Dispatcher class for mod_messages
23+
*
24+
* @since __DEPLOY_VERSION__
25+
*/
26+
class Dispatcher extends AbstractModuleDispatcher implements HelperFactoryAwareInterface
27+
{
28+
use HelperFactoryAwareTrait;
29+
30+
/**
31+
* Runs the dispatcher.
32+
*
33+
* @return void
34+
*
35+
* @since __DEPLOY_VERSION__
36+
*/
37+
public function dispatch()
38+
{
39+
// Check permissions.
40+
if (
41+
!$this->getApplication()->getIdentity()->authorise('core.login.admin')
42+
|| !$this->getApplication()->getIdentity()->authorise('core.manage', 'com_messages')
43+
) {
44+
return;
45+
}
46+
47+
parent::dispatch();
48+
}
49+
50+
/**
51+
* Returns the layout data.
52+
*
53+
* @return array
54+
*
55+
* @since __DEPLOY_VERSION__
56+
*/
57+
protected function getLayoutData()
58+
{
59+
$data = parent::getLayoutData();
60+
61+
$data['countUnread'] = $this->getHelperFactory()->getHelper('MessagesHelper')->getUnreadMessagesCount($data['params'], $this->getApplication());
62+
63+
return $data;
64+
}
65+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/**
4+
* @package Joomla.Administrator
5+
* @subpackage mod_messages
6+
*
7+
* @copyright (C) 2024 Open Source Matters, Inc. <https://www.joomla.org>
8+
* @license GNU General Public License version 2 or later; see LICENSE.txt
9+
*/
10+
11+
namespace Joomla\Module\Messages\Administrator\Helper;
12+
13+
use Joomla\CMS\Application\AdministratorApplication;
14+
use Joomla\Registry\Registry;
15+
16+
// phpcs:disable PSR1.Files.SideEffects
17+
\defined('_JEXEC') or die;
18+
// phpcs:enable PSR1.Files.SideEffects
19+
20+
/**
21+
* Helper for mod_messages
22+
*
23+
* @since __DEPLOY_VERSION__
24+
*/
25+
class MessagesHelper
26+
{
27+
/**
28+
* Get count of unread messages.
29+
*
30+
* @param Registry $params Object holding the module parameters
31+
* @param AdministratorApplication $app The application
32+
*
33+
* @return integer
34+
*
35+
* @since __DEPLOY_VERSION__
36+
*/
37+
public function getUnreadMessagesCount(Registry $params, AdministratorApplication $app)
38+
{
39+
// Try to get the items from the messages model
40+
try {
41+
/**
42+
* @var \Joomla\Component\Messages\Administrator\Model\MessagesModel $messagesModel
43+
*
44+
*/
45+
$messagesModel = $app->bootComponent('com_messages')->getMVCFactory()
46+
->createModel('Messages', 'Administrator', ['ignore_request' => true]);
47+
$messagesModel->setState('filter.state', 0);
48+
$messages = $messagesModel->getItems();
49+
50+
return \count($messages);
51+
} catch (\RuntimeException $e) {
52+
$messages = [];
53+
54+
// Still render the error message from the Exception object
55+
$app->enqueueMessage($e->getMessage(), 'error');
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)