Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert mod_messages to service provider #42735

Merged
merged 14 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 0 additions & 36 deletions administrator/modules/mod_messages/mod_messages.php

This file was deleted.

4 changes: 3 additions & 1 deletion administrator/modules/mod_messages/mod_messages.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
<authorUrl>www.joomla.org</authorUrl>
<version>4.0.0</version>
<description>MOD_MESSAGES_XML_DESCRIPTION</description>
<namespace path="src">Joomla\Module\Messages</namespace>
<files>
<filename module="mod_messages">mod_messages.php</filename>
<folder module="mod_messages">services</folder>
<folder>src</folder>
<folder>tmpl</folder>
</files>
<languages>
Expand Down
43 changes: 43 additions & 0 deletions administrator/modules/mod_messages/services/provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/**
* @package Joomla.Administrator
* @subpackage mod_messages
*
* @copyright (C) 2024 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

use Joomla\CMS\Extension\Service\Provider\HelperFactory;
use Joomla\CMS\Extension\Service\Provider\Module;
use Joomla\CMS\Extension\Service\Provider\ModuleDispatcherFactory;
use Joomla\DI\Container;
use Joomla\DI\ServiceProviderInterface;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects

/**
* The messages module service provider.
*
* @since __DEPLOY_VERSION__
*/
return new class () implements ServiceProviderInterface {
/**
* Registers the service provider with a DI container.
*
* @param Container $container The DI container.
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function register(Container $container)
{
$container->registerServiceProvider(new ModuleDispatcherFactory('\\Joomla\\Module\\Messages'));
$container->registerServiceProvider(new HelperFactory('\\Joomla\\Module\\Messages\\Administrator\\Helper'));

$container->registerServiceProvider(new Module());
}
};
65 changes: 65 additions & 0 deletions administrator/modules/mod_messages/src/Dispatcher/Dispatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

/**
* @package Joomla.Administrator
* @subpackage mod_messages
*
* @copyright (C) 2024 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\Module\Messages\Administrator\Dispatcher;

use Joomla\CMS\Dispatcher\AbstractModuleDispatcher;
use Joomla\CMS\Helper\HelperFactoryAwareInterface;
use Joomla\CMS\Helper\HelperFactoryAwareTrait;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects

/**
* Dispatcher class for mod_messages
*
* @since __DEPLOY_VERSION__
*/
class Dispatcher extends AbstractModuleDispatcher implements HelperFactoryAwareInterface
{
use HelperFactoryAwareTrait;

/**
* Runs the dispatcher.
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function dispatch()
{
// Check permissions.
if (
!$this->getApplication()->getIdentity()->authorise('core.login.admin')
|| !$this->getApplication()->getIdentity()->authorise('core.manage', 'com_messages')
) {
return;
}

parent::dispatch();
}

/**
* Returns the layout data.
*
* @return array
*
* @since __DEPLOY_VERSION__
*/
protected function getLayoutData()
{
$data = parent::getLayoutData();

$data['countUnread'] = $this->getHelperFactory()->getHelper('MessagesHelper')->getUnreadMessagesCount($data['params'], $this->getApplication());

return $data;
}
}
58 changes: 58 additions & 0 deletions administrator/modules/mod_messages/src/Helper/MessagesHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/**
* @package Joomla.Administrator
* @subpackage mod_messages
*
* @copyright (C) 2024 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\Module\Messages\Administrator\Helper;

use Joomla\CMS\Application\AdministratorApplication;
use Joomla\Registry\Registry;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects

/**
* Helper for mod_messages
*
* @since __DEPLOY_VERSION__
*/
class MessagesHelper
{
/**
* Get count of unread messages.
*
* @param Registry $params Object holding the module parameters
* @param AdministratorApplication $app The application
*
* @return integer
*
* @since __DEPLOY_VERSION__
*/
public function getUnreadMessagesCount(Registry $params, AdministratorApplication $app)
{
// Try to get the items from the messages model
try {
/**
* @var \Joomla\Component\Messages\Administrator\Model\MessagesModel $messagesModel
*
*/
$messagesModel = $app->bootComponent('com_messages')->getMVCFactory()
->createModel('Messages', 'Administrator', ['ignore_request' => true]);
$messagesModel->setState('filter.state', 0);
$messages = $messagesModel->getItems();

return \count($messages);
} catch (\RuntimeException $e) {
$messages = [];

// Still render the error message from the Exception object
$app->enqueueMessage($e->getMessage(), 'error');
}
}
}