Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 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
10 changes: 9 additions & 1 deletion libraries/src/Extension/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Joomla\CMS\Application\CMSApplicationInterface;
use Joomla\CMS\Dispatcher\DispatcherInterface;
use Joomla\CMS\Dispatcher\ModuleDispatcherFactoryInterface;
use Joomla\CMS\Helper\HelperFactoryAwareInterface;
use Joomla\CMS\Helper\HelperFactoryInterface;
use Joomla\Input\Input;

Expand Down Expand Up @@ -68,7 +69,14 @@ public function __construct(ModuleDispatcherFactoryInterface $dispatcherFactory,
*/
public function getDispatcher(\stdClass $module, CMSApplicationInterface $application, Input $input = null): DispatcherInterface
{
return $this->dispatcherFactory->createDispatcher($module, $application, $input);
$dispatcher = $this->dispatcherFactory->createDispatcher($module, $application, $input);

if ($dispatcher instanceof HelperFactoryAwareInterface)
{
$dispatcher->setHelperFactory($this->helperFactory);
}

return $dispatcher;
}

/**
Expand Down
6 changes: 5 additions & 1 deletion libraries/src/Extension/Service/Provider/HelperFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
\defined('JPATH_PLATFORM') or die;

use Joomla\CMS\Helper\HelperFactoryInterface;
use Joomla\Database\DatabaseInterface;
use Joomla\DI\Container;
use Joomla\DI\ServiceProviderInterface;

Expand Down Expand Up @@ -57,7 +58,10 @@ public function register(Container $container)
HelperFactoryInterface::class,
function (Container $container)
{
return new \Joomla\CMS\Helper\HelperFactory($this->namespace);
$factory = new \Joomla\CMS\Helper\HelperFactory($this->namespace);
$factory->setDatabase($container->get(DatabaseInterface::class));

return $factory;
}
);
}
Expand Down
14 changes: 13 additions & 1 deletion libraries/src/Helper/HelperFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

namespace Joomla\CMS\Helper;

use Joomla\Database\DatabaseAwareInterface;
use Joomla\Database\DatabaseAwareTrait;

\defined('_JEXEC') or die;

/**
Expand All @@ -17,6 +20,8 @@
*/
class HelperFactory implements HelperFactoryInterface
{
use DatabaseAwareTrait;

/**
* The extension namespace
*
Expand Down Expand Up @@ -57,6 +62,13 @@ public function getHelper(string $name, array $config = [])
return null;
}

return new $className($config);
$helper = new $className($config);

if ($helper instanceof DatabaseAwareInterface)
{
$helper->setDatabase($this->getDatabase());
}

return $helper;
}
}
30 changes: 30 additions & 0 deletions libraries/src/Helper/HelperFactoryAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* Joomla! Content Management System
*
* @copyright (C) 2022 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\CMS\Helper;

\defined('_JEXEC') or die;

/**
* Interface to be implemented by classes depending on a helper factory.
*
* @since __DEPLOY_VERSION__
*/
interface HelperFactoryAwareInterface
{
/**
* Sets the helper factory to use.
*
* @param HelperFactory $helper The helper factory to use.
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function setHelperFactory(HelperFactory $helper);
}
61 changes: 61 additions & 0 deletions libraries/src/Helper/HelperFactoryAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* Joomla! Content Management System
*
* @copyright (C) 2022 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\CMS\Helper;

\defined('_JEXEC') or die;

/**
* Defines the trait for a HelperFactory Aware Class.
*
* @since __DEPLOY_VERSION__
*/
trait HelperFactoryAwareTrait
{
/**
* HelperFactory
*
* @var HelperFactory
*
* @since __DEPLOY_VERSION__
*/
private $helperFactory;

/**
* Get the HelperFactory.
*
* @return HelperFactory
*
* @since __DEPLOY_VERSION__
*
* @throws \UnexpectedValueException May be thrown if the HelperFactory has not been set.
*/
public function getHelperFactory(): HelperFactory
{
if ($this->helper)
{
return $this->helper;
}

throw new \UnexpectedValueException('HelperFactory not set in ' . __CLASS__);
}

/**
* Sets the helper factory to use.
*
* @param HelperFactory $helperFactory The helper factory to use.
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function setHelperFactory(HelperFactory $helperFactory)
{
$this->helper = $helperFactory;
}
}
17 changes: 0 additions & 17 deletions modules/mod_articles_news/mod_articles_news.php

This file was deleted.

4 changes: 2 additions & 2 deletions modules/mod_articles_news/mod_articles_news.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
<description>MOD_ARTICLES_NEWS_XML_DESCRIPTION</description>
<namespace path="src">Joomla\Module\ArticlesNews</namespace>
<files>
<filename module="mod_articles_news">mod_articles_news.php</filename>
<folder>src</folder>
<folder module="mod_articles_news">src</folder>
<folder>services</folder>
<folder>tmpl</folder>
</files>
<languages>
Expand Down
41 changes: 41 additions & 0 deletions modules/mod_articles_news/services/provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* @package Joomla.Administrator
* @subpackage mod_articles_news
*
* @copyright (C) 2022 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 article news 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\\ArticlesNews'));
$container->registerServiceProvider(new HelperFactory('\\Joomla\\Module\\ArticlesNews\\Site\\Helper'));

$container->registerServiceProvider(new Module);
}
};
42 changes: 42 additions & 0 deletions modules/mod_articles_news/src/Dispatcher/Dispatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* @package Joomla.Site
* @subpackage mod_articles_news
*
* @copyright (C) 2022 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\Module\ArticlesNews\Site\Dispatcher;

\defined('JPATH_PLATFORM') or die;

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

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

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

$data['list'] = $this->getHelperFactory()->getHelper('ArticlesNewsHelper')->getArticles($data['params'], $this->getApplication());

return $data;
}
}
42 changes: 31 additions & 11 deletions modules/mod_articles_news/src/Helper/ArticlesNewsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,41 @@
\defined('_JEXEC') or die;

use Joomla\CMS\Access\Access;
use Joomla\CMS\Application\SiteApplication;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Uri\Uri;
use Joomla\Component\Content\Site\Helper\RouteHelper;
use Joomla\Database\DatabaseAwareInterface;
use Joomla\Database\DatabaseAwareTrait;
use Joomla\Registry\Registry;

/**
* Helper for mod_articles_news
*
* @since 1.6
*/
abstract class ArticlesNewsHelper
class ArticlesNewsHelper implements DatabaseAwareInterface
{
use DatabaseAwareTrait;

/**
* Get a list of the latest articles from the article model
* Get a list of the latest articles from the article model.
*
* @param \Joomla\Registry\Registry &$params object holding the models parameters
* @param Registry $params Object holding the models parameters
* @param SiteApplication $app The app
*
* @return mixed
*
* @since 1.6
* @since __DEPLOY_VERSION__
*/
public static function getList(&$params)
public function getArticles(Registry $params, SiteApplication $app)
{
$app = Factory::getApplication();

/** @var \Joomla\Component\Content\Site\Model\ArticlesModel $model */
$model = $app->bootComponent('com_content')
->getMVCFactory()->createModel('Articles', 'Site', ['ignore_request' => true]);
$model = $app->bootComponent('com_content')->getMVCFactory()->createModel('Articles', 'Site', ['ignore_request' => true]);

// Set application parameters in model
$appParams = $app->getParams();
Expand All @@ -59,7 +63,7 @@ public static function getList(&$params)

// Access filter
$access = !ComponentHelper::getParams('com_content')->get('show_noauth');
$authorised = Access::getAuthorisedViewLevels(Factory::getUser()->get('id'));
$authorised = Access::getAuthorisedViewLevels($app->getIdentity() ? $app->getIdentity()->id : 0);
$model->setState('filter.access', $access);

// Category filter
Expand Down Expand Up @@ -103,7 +107,7 @@ public static function getList(&$params)

if (trim($ordering) === 'rand()')
{
$model->setState('list.ordering', Factory::getDbo()->getQuery(true)->rand());
$model->setState('list.ordering', $this->getDatabase()->getQuery(true)->rand());
}
else
{
Expand Down Expand Up @@ -198,4 +202,20 @@ public static function getList(&$params)

return $items;
}

/**
* Get a list of the latest articles from the article model
*
* @param \Joomla\Registry\Registry &$params object holding the models parameters
*
* @return mixed
*
* @since 1.6
*
* @deprecated 5.0 Use the none static function getArticles
*/
public static function getList(&$params)
{
return (new self)->getArticles($params, Factory::getApplication());
}
}