Skip to content

Commit 369b872

Browse files
laoneobrianteemanrichard67
authored
[4.2] Inject the application into the service provider plugins (#38060)
* Inject the application into the service provider plugins * Add some tests * more listener tests * Update libraries/src/Plugin/CMSPlugin.php Co-authored-by: Brian Teeman <[email protected]> * translate and custom dir in request * use translate * Update tests/Unit/Libraries/Cms/Plugin/CMSPluginTest.php Co-authored-by: Richard Fath <[email protected]> * do not spread * cs Co-authored-by: Brian Teeman <[email protected]> Co-authored-by: Richard Fath <[email protected]>
1 parent 49a8b79 commit 369b872

File tree

27 files changed

+1075
-210
lines changed

27 files changed

+1075
-210
lines changed

administrator/components/com_scheduler/src/Traits/TaskPluginTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ protected function getRoutineId(Form $form, $data): string
215215
// If we're unable to find a routineId, it might be in the form input.
216216
if (empty($routineId))
217217
{
218-
$app = $this->app ?? Factory::getApplication();
218+
$app = $this->getApplication() ?? ($this->app ?? Factory::getApplication());
219219
$form = $app->getInput()->get('jform', []);
220220
$routineId = ArrayHelper::getValue($form, 'type', '', 'STRING');
221221
}
@@ -248,7 +248,7 @@ protected function logTask(string $message, string $priority = 'info'): void
248248

249249
if (!$langLoaded)
250250
{
251-
$app = $this->app ?? Factory::getApplication();
251+
$app = $this->getApplication() ?? ($this->app ?? Factory::getApplication());
252252
$app->getLanguage()->load('com_scheduler', JPATH_ADMINISTRATOR);
253253
$langLoaded = true;
254254
}

libraries/src/Plugin/CMSPlugin.php

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
\defined('JPATH_PLATFORM') or die;
1212

13+
use Joomla\CMS\Application\CMSApplicationInterface;
1314
use Joomla\CMS\Extension\PluginInterface;
1415
use Joomla\CMS\Factory;
1516
use Joomla\Event\AbstractEvent;
@@ -75,6 +76,15 @@ abstract class CMSPlugin implements DispatcherAwareInterface, PluginInterface
7576
*/
7677
protected $allowLegacyListeners = true;
7778

79+
/**
80+
* The application object
81+
*
82+
* @var CMSApplicationInterface
83+
*
84+
* @since __DEPLOY_VERSION__
85+
*/
86+
private $application;
87+
7888
/**
7989
* Constructor
8090
*
@@ -120,6 +130,7 @@ public function __construct(&$subject, $config = array())
120130

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

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

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

@@ -162,7 +174,7 @@ public function loadLanguage($extension = '', $basePath = JPATH_ADMINISTRATOR)
162174
}
163175

164176
$extension = strtolower($extension);
165-
$lang = Factory::getLanguage();
177+
$lang = $this->getApplication() ? $this->getApplication()->getLanguage() : Factory::getLanguage();
166178

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

189+
/**
190+
* Translates the given key with the local applications language. If arguments are available, then
191+
* injects them into the translated string.
192+
*
193+
* @param string $key The key to translate
194+
* @param mixed[] $arguments The arguments
195+
*
196+
* @return string The translated string
197+
*
198+
* @since __DEPLOY_VERSION__
199+
*
200+
* @see sprintf
201+
*/
202+
protected function translate(string $key): string
203+
{
204+
$language = $this->getApplication()->getLanguage();
205+
206+
$arguments = \func_get_args();
207+
208+
if (count($arguments) > 1)
209+
{
210+
$arguments[0] = $language->_($key);
211+
212+
return \call_user_func_array('sprintf', $arguments);
213+
}
214+
215+
return $language->_($key);
216+
}
217+
177218
/**
178219
* Registers legacy Listeners to the Dispatcher, emulating how plugins worked under Joomla! 3.x and below.
179220
*
@@ -359,4 +400,30 @@ private function parameterImplementsEventInterface(\ReflectionParameter $paramet
359400

360401
return false;
361402
}
403+
404+
/**
405+
* Returns the internal application or null when not set.
406+
*
407+
* @return CMSApplicationInterface|null
408+
*
409+
* @since __DEPLOY_VERSION__
410+
*/
411+
protected function getApplication(): ?CMSApplicationInterface
412+
{
413+
return $this->application;
414+
}
415+
416+
/**
417+
* Sets the application to use.
418+
*
419+
* @param CMSApplicationInterface $application The application
420+
*
421+
* @return void
422+
*
423+
* @since __DEPLOY_VERSION__
424+
*/
425+
public function setApplication(CMSApplicationInterface $application): void
426+
{
427+
$this->application = $application;
428+
}
362429
}

plugins/actionlog/joomla/services/provider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
defined('_JEXEC') or die;
1111

1212
use Joomla\CMS\Extension\PluginInterface;
13+
use Joomla\CMS\Factory;
1314
use Joomla\CMS\Plugin\PluginHelper;
1415
use Joomla\Database\DatabaseInterface;
1516
use Joomla\DI\Container;
@@ -38,6 +39,7 @@ function (Container $container)
3839
$container->get(DispatcherInterface::class),
3940
(array) PluginHelper::getPlugin('actionlog', 'joomla')
4041
);
42+
$plugin->setApplication(Factory::getApplication());
4143
$plugin->setDatabase($container->get(DatabaseInterface::class));
4244

4345
return $plugin;

0 commit comments

Comments
 (0)