Skip to content

Commit ca3faf7

Browse files
authored
[5.1] Convert mod_tags_similar to service provider (#42898)
1 parent 3073ec7 commit ca3faf7

File tree

5 files changed

+124
-30
lines changed

5 files changed

+124
-30
lines changed

modules/mod_tags_similar/mod_tags_similar.php

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

modules/mod_tags_similar/mod_tags_similar.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<description>MOD_TAGS_SIMILAR_XML_DESCRIPTION</description>
1212
<namespace path="src">Joomla\Module\TagsSimilar</namespace>
1313
<files>
14-
<filename module="mod_tags_similar">mod_tags_similar.php</filename>
14+
<folder module="mod_tags_similar">services</folder>
1515
<folder>src</folder>
1616
<folder>tmpl</folder>
1717
</files>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/**
4+
* @package Joomla.Site
5+
* @subpackage mod_tags_similar
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+
\defined('_JEXEC') or die;
12+
13+
use Joomla\CMS\Extension\Service\Provider\HelperFactory;
14+
use Joomla\CMS\Extension\Service\Provider\Module;
15+
use Joomla\CMS\Extension\Service\Provider\ModuleDispatcherFactory;
16+
use Joomla\DI\Container;
17+
use Joomla\DI\ServiceProviderInterface;
18+
19+
/**
20+
* The tags similar module service provider.
21+
*
22+
* @since __DEPLOY_VERSION__
23+
*/
24+
return new class () implements ServiceProviderInterface {
25+
/**
26+
* Registers the service provider with a DI container.
27+
*
28+
* @param Container $container The DI container.
29+
*
30+
* @return void
31+
*
32+
* @since __DEPLOY_VERSION__
33+
*/
34+
public function register(Container $container): void
35+
{
36+
$container->registerServiceProvider(new ModuleDispatcherFactory('\\Joomla\\Module\\TagsSimilar'));
37+
$container->registerServiceProvider(new HelperFactory('\\Joomla\\Module\\TagsSimilar\\Site\\Helper'));
38+
39+
$container->registerServiceProvider(new Module());
40+
}
41+
};
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
/**
4+
* @package Joomla.Site
5+
* @subpackage mod_tags_similar
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\TagsSimilar\Site\Dispatcher;
12+
13+
use Joomla\CMS\Dispatcher\AbstractModuleDispatcher;
14+
use Joomla\CMS\Helper\HelperFactoryAwareInterface;
15+
use Joomla\CMS\Helper\HelperFactoryAwareTrait;
16+
use Joomla\CMS\Helper\ModuleHelper;
17+
18+
// phpcs:disable PSR1.Files.SideEffects
19+
\defined('_JEXEC') or die;
20+
// phpcs:enable PSR1.Files.SideEffects
21+
22+
/**
23+
* Dispatcher class for mod_tags_similar
24+
*
25+
* @since __DEPLOY_VERSION__
26+
*/
27+
class Dispatcher extends AbstractModuleDispatcher implements HelperFactoryAwareInterface
28+
{
29+
use HelperFactoryAwareTrait;
30+
31+
/**
32+
* Returns the layout data.
33+
*
34+
* @return array
35+
*
36+
* @since __DEPLOY_VERSION__
37+
*/
38+
protected function getLayoutData()
39+
{
40+
$data = parent::getLayoutData();
41+
42+
$cacheparams = new \stdClass();
43+
$cacheparams->cachemode = 'safeuri';
44+
$cacheparams->class = $this->getHelperFactory()->getHelper('TagsSimilarHelper');
45+
$cacheparams->method = 'getItems';
46+
$cacheparams->methodparams = $data['params'];
47+
$cacheparams->modeparams = ['id' => 'array', 'Itemid' => 'int'];
48+
49+
$data['list'] = ModuleHelper::moduleCache($this->module, $data['params'], $cacheparams);
50+
51+
return $data;
52+
}
53+
}

modules/mod_tags_similar/src/Helper/TagsSimilarHelper.php

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use Joomla\CMS\Helper\TagsHelper;
1717
use Joomla\CMS\Language\Text;
1818
use Joomla\Component\Tags\Site\Helper\RouteHelper;
19+
use Joomla\Database\DatabaseAwareInterface;
20+
use Joomla\Database\DatabaseAwareTrait;
1921
use Joomla\Database\ParameterType;
2022
use Joomla\Registry\Registry;
2123

@@ -28,16 +30,20 @@
2830
*
2931
* @since 3.1
3032
*/
31-
abstract class TagsSimilarHelper
33+
class TagsSimilarHelper implements DatabaseAwareInterface
3234
{
35+
use DatabaseAwareTrait;
36+
3337
/**
34-
* Get a list of tags
38+
* Get a list of items with similar tags
3539
*
3640
* @param Registry &$params Module parameters
3741
*
3842
* @return array
43+
*
44+
* @since __DEPLOY_VERSION__
3945
*/
40-
public static function getList(&$params)
46+
public function getItems(&$params)
4147
{
4248
$app = Factory::getApplication();
4349
$option = $app->getInput()->get('option');
@@ -49,8 +55,8 @@ public static function getList(&$params)
4955
return [];
5056
}
5157

52-
$db = Factory::getDbo();
53-
$user = Factory::getUser();
58+
$db = $this->getDatabase();
59+
$user = $app->getIdentity();
5460
$groups = $user->getAuthorisedViewLevels();
5561
$matchtype = $params->get('matchtype', 'all');
5662
$ordering = $params->get('ordering', 'count');
@@ -218,4 +224,22 @@ public static function getList(&$params)
218224

219225
return $results;
220226
}
227+
228+
/**
229+
* Get a list of items with similar tags
230+
*
231+
* @param Registry &$params Module parameters
232+
*
233+
* @return array
234+
*
235+
* @deprecated __DEPLOY_VERSION__ will be removed in 6.0
236+
* Use the non-static method getItems
237+
* Example: Factory::getApplication()->bootModule('mod_tags_similar', 'site')
238+
* ->getHelper('TagsSimilarHelper')
239+
* ->getItems($params)
240+
*/
241+
public static function getList(&$params)
242+
{
243+
return (new self())->getItems($params);
244+
}
221245
}

0 commit comments

Comments
 (0)