Skip to content

Commit

Permalink
Convert mod_tags_popular to service provider
Browse files Browse the repository at this point in the history
  • Loading branch information
joomlaweby committed Feb 26, 2024
1 parent 75fca46 commit 1d95c6f
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 37 deletions.
30 changes: 0 additions & 30 deletions modules/mod_tags_popular/mod_tags_popular.php

This file was deleted.

2 changes: 1 addition & 1 deletion modules/mod_tags_popular/mod_tags_popular.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<description>MOD_TAGS_POPULAR_XML_DESCRIPTION</description>
<namespace path="src">Joomla\Module\TagsPopular</namespace>
<files>
<filename module="mod_tags_popular">mod_tags_popular.php</filename>
<folder module="mod_tags_popular">services</folder>
<folder>src</folder>
<folder>tmpl</folder>
</files>
Expand Down
41 changes: 41 additions & 0 deletions modules/mod_tags_popular/services/provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

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

\defined('_JEXEC') or die;

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;

/**
* The popular tags 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\\TagsPopular'));
$container->registerServiceProvider(new HelperFactory('\\Joomla\\Module\\TagsPopular\\Site\\Helper'));

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

/**
* @package Joomla.Site
* @subpackage mod_tags_popular
*
* @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\TagsPopular\Site\Dispatcher;

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

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

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

/**
* Runs the dispatcher.
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function dispatch()
{
$displayData = $this->getLayoutData();

if (!\count($displayData['list']) && !$displayData['params']->get('no_results_text')) {
return;
}

parent::dispatch();
}

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

$cacheparams = new \stdClass();
$cacheparams->cachemode = 'safeuri';
$cacheparams->class = $this->getHelperFactory()->getHelper('TagsPopularHelper');
$cacheparams->method = 'getTags';
$cacheparams->methodparams = $data['params'];
$cacheparams->modeparams = ['id' => 'array', 'Itemid' => 'int'];

$data['list'] = ModuleHelper::moduleCache($this->module, $data['params'], $cacheparams);
$data['display_count'] = $data['params']->get('display_count', 0);

return $data;
}
}
37 changes: 31 additions & 6 deletions modules/mod_tags_popular/src/Helper/TagsPopularHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Helper\ContentHelper;
use Joomla\Database\DatabaseAwareInterface;
use Joomla\Database\DatabaseAwareTrait;
use Joomla\Database\ParameterType;

// phpcs:disable PSR1.Files.SideEffects
Expand All @@ -24,21 +26,24 @@
*
* @since 3.1
*/
abstract class TagsPopularHelper
class TagsPopularHelper implements DatabaseAwareInterface
{
use DatabaseAwareTrait;

/**
* Get list of popular tags
*
* @param \Joomla\Registry\Registry &$params module parameters
*
* @return mixed
*
* @since 3.1
* @since __DEPLOY_VERSION__
*/
public static function getList(&$params)
public function getTags(&$params)
{
$db = Factory::getDbo();
$user = Factory::getUser();
$db = $this->getDatabase();
$app = Factory::getApplication();
$user = $app->getIdentity();
$groups = $user->getAuthorisedViewLevels();
$timeframe = $params->get('timeframe', 'alltime');
$maximum = (int) $params->get('maximum', 5);
Expand Down Expand Up @@ -183,9 +188,29 @@ public static function getList(&$params)
$results = $db->loadObjectList();
} catch (\RuntimeException $e) {
$results = [];
Factory::getApplication()->enqueueMessage($e->getMessage(), 'error');
$app->enqueueMessage($e->getMessage(), 'error');
}

return $results;
}

/**
* Get list of popular tags
*
* @param \Joomla\Registry\Registry &$params module parameters
*
* @return mixed
*
* @since 3.1
*
* @deprecated __DEPLOY_VERSION__ will be removed in 6.0
* Use the non-static method getTags
* Example: Factory::getApplication()->bootModule('mod_tags_popular', 'site')
* ->getHelper('TagsPopularHelper')
* ->getTags($params)
*/
public static function getList(&$params)
{
return (new self())->getTags($params);
}
}

0 comments on commit 1d95c6f

Please sign in to comment.