Skip to content
Merged
Show file tree
Hide file tree
Changes from 14 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
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ protected function getRoutineId(Form $form, $data): string
// If we're unable to find a routineId, it might be in the form input.
if (empty($routineId))
{
$app = $this->app ?? Factory::getApplication();
$app = $this->getApplication() ?? ($this->app ?? Factory::getApplication());
$form = $app->getInput()->get('jform', []);
$routineId = ArrayHelper::getValue($form, 'type', '', 'STRING');
}
Expand Down Expand Up @@ -248,7 +248,7 @@ protected function logTask(string $message, string $priority = 'info'): void

if (!$langLoaded)
{
$app = $this->app ?? Factory::getApplication();
$app = $this->getApplication() ?? ($this->app ?? Factory::getApplication());
$app->getLanguage()->load('com_scheduler', JPATH_ADMINISTRATOR);
$langLoaded = true;
}
Expand Down
65 changes: 64 additions & 1 deletion libraries/src/Plugin/CMSPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

\defined('JPATH_PLATFORM') or die;

use Joomla\CMS\Application\CMSApplicationInterface;
use Joomla\CMS\Extension\PluginInterface;
use Joomla\CMS\Factory;
use Joomla\Event\AbstractEvent;
Expand Down Expand Up @@ -75,6 +76,15 @@ abstract class CMSPlugin implements DispatcherAwareInterface, PluginInterface
*/
protected $allowLegacyListeners = true;

/**
* The application object
*
* @var CMSApplicationInterface
*
* @since __DEPLOY_VERSION__
*/
private $application;

/**
* Constructor
*
Expand Down Expand Up @@ -120,6 +130,7 @@ public function __construct(&$subject, $config = array())

if (property_exists($this, 'app'))
{
@trigger_error('The application should be injected through setApplication() and requested through getApplication().', E_USER_DEPRECATED);
$reflection = new \ReflectionClass($this);
$appProperty = $reflection->getProperty('app');

Expand All @@ -131,6 +142,7 @@ public function __construct(&$subject, $config = array())

if (property_exists($this, 'db'))
{
@trigger_error('The database should be injected through the DatabaseAwareInterface and trait.', E_USER_DEPRECATED);
$reflection = new \ReflectionClass($this);
$dbProperty = $reflection->getProperty('db');

Expand Down Expand Up @@ -162,7 +174,7 @@ public function loadLanguage($extension = '', $basePath = JPATH_ADMINISTRATOR)
}

$extension = strtolower($extension);
$lang = Factory::getLanguage();
$lang = $this->getApplication() ? $this->getApplication()->getLanguage() : Factory::getLanguage();

// If language already loaded, don't load it again.
if ($lang->getPaths($extension))
Expand All @@ -174,6 +186,31 @@ public function loadLanguage($extension = '', $basePath = JPATH_ADMINISTRATOR)
|| $lang->load($extension, JPATH_PLUGINS . '/' . $this->_type . '/' . $this->_name);
}

/**
* Translates the given key with the local applications language. If arguments are available, then
* injects them into the translated string.
*
* @param string $key The key to translate
* @param mixed[] $arguments The arguments
*
* @return string The translated string
*
* @since __DEPLOY_VERSION__
*
* @see sprintf
*/
protected function translate(string $key, ...$arguments): string
{
$language = $this->getApplication()->getLanguage();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we add some kind of check here? If application is not injected, this line would cause fatal error.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be not bad. Can you make a pr?


if ($arguments)
{
return sprintf($language->_($key), ...$arguments);
}

return $language->_($key);
}

/**
* Registers legacy Listeners to the Dispatcher, emulating how plugins worked under Joomla! 3.x and below.
*
Expand Down Expand Up @@ -359,4 +396,30 @@ private function parameterImplementsEventInterface(\ReflectionParameter $paramet

return false;
}

/**
* Returns the internal application or null when not set.
*
* @return CMSApplicationInterface|null
*
* @since __DEPLOY_VERSION__
*/
protected function getApplication(): ?CMSApplicationInterface
{
return $this->application;
}

/**
* Sets the application to use.
*
* @param CMSApplicationInterface $application The application
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function setApplication(CMSApplicationInterface $application): void
{
$this->application = $application;
}
}
2 changes: 2 additions & 0 deletions plugins/actionlog/joomla/services/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
defined('_JEXEC') or die;

use Joomla\CMS\Extension\PluginInterface;
use Joomla\CMS\Factory;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\Database\DatabaseInterface;
use Joomla\DI\Container;
Expand Down Expand Up @@ -38,6 +39,7 @@ function (Container $container)
$container->get(DispatcherInterface::class),
(array) PluginHelper::getPlugin('actionlog', 'joomla')
);
$plugin->setApplication(Factory::getApplication());
$plugin->setDatabase($container->get(DatabaseInterface::class));

return $plugin;
Expand Down
Loading