Skip to content

Commit 153b66d

Browse files
authored
[5.2][Events] Use event classes for Extension plugins (joomla#43617)
1 parent 35e1d3e commit 153b66d

File tree

9 files changed

+268
-45
lines changed

9 files changed

+268
-45
lines changed

administrator/components/com_joomlaupdate/src/Model/UpdateModel.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
use Joomla\CMS\Authentication\Authentication;
1414
use Joomla\CMS\Component\ComponentHelper;
15+
use Joomla\CMS\Event\Extension\AfterJoomlaUpdateEvent;
16+
use Joomla\CMS\Event\Extension\BeforeJoomlaUpdateEvent;
1517
use Joomla\CMS\Extension\ExtensionHelper;
1618
use Joomla\CMS\Factory;
1719
use Joomla\CMS\Filter\InputFilter;
@@ -580,7 +582,7 @@ public function createUpdateFile($basename = null): bool
580582
$app = Factory::getApplication();
581583

582584
// Trigger event before joomla update.
583-
$app->triggerEvent('onJoomlaBeforeUpdate');
585+
$app->getDispatcher()->dispatch('onJoomlaBeforeUpdate', new BeforeJoomlaUpdateEvent('onJoomlaBeforeUpdate'));
584586

585587
// Get the absolute path to site's root.
586588
$siteroot = JPATH_SITE;
@@ -890,7 +892,8 @@ public function cleanUp()
890892
$app = Factory::getApplication();
891893

892894
// Trigger event after joomla update.
893-
$app->triggerEvent('onJoomlaAfterUpdate');
895+
// @TODO: The event dispatched twice, here and at the end of current method. One of it should be removed.
896+
$app->getDispatcher()->dispatch('onJoomlaAfterUpdate', new AfterJoomlaUpdateEvent('onJoomlaAfterUpdate'));
894897

895898
// Remove the update package.
896899
$tempdir = $app->get('tmp_path');
@@ -920,7 +923,9 @@ public function cleanUp()
920923
$oldVersion = $app->getUserState('com_joomlaupdate.oldversion');
921924

922925
// Trigger event after joomla update.
923-
$app->triggerEvent('onJoomlaAfterUpdate', [$oldVersion]);
926+
$app->getDispatcher()->dispatch('onJoomlaAfterUpdate', new AfterJoomlaUpdateEvent('onJoomlaAfterUpdate', [
927+
'oldVersion' => $oldVersion ?: '',
928+
]));
924929
$app->setUserState('com_joomlaupdate.oldversion', null);
925930

926931
try {

libraries/src/Event/CoreEventAware.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ trait CoreEventAware
153153
'onExtensionAfterSave' => Model\AfterSaveEvent::class,
154154
'onExtensionAfterDelete' => Model\AfterDeleteEvent::class,
155155
'onExtensionChangeState' => Model\BeforeChangeStateEvent::class,
156+
'onJoomlaBeforeUpdate' => Extension\BeforeJoomlaUpdateEvent::class,
157+
'onJoomlaAfterUpdate' => Extension\AfterJoomlaUpdateEvent::class,
156158
// Installer
157159
'onInstallerAddInstallationTab' => Installer\AddInstallationTabEvent::class,
158160
'onInstallerBeforeInstallation' => Installer\BeforeInstallationEvent::class,
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/**
4+
* Joomla! Content Management System
5+
*
6+
* @copyright (C) 2023 Open Source Matters, Inc. <https://www.joomla.org>
7+
* @license GNU General Public License version 2 or later; see LICENSE.txt
8+
*/
9+
10+
namespace Joomla\CMS\Event\Extension;
11+
12+
use Joomla\CMS\Event\AbstractImmutableEvent;
13+
use Joomla\CMS\Event\ReshapeArgumentsAware;
14+
15+
// phpcs:disable PSR1.Files.SideEffects
16+
\defined('_JEXEC') or die;
17+
// phpcs:enable PSR1.Files.SideEffects
18+
19+
/**
20+
* Base class for Joomla Update events
21+
*
22+
* @since __DEPLOY_VERSION__
23+
*/
24+
abstract class AbstractJoomlaUpdateEvent extends AbstractImmutableEvent
25+
{
26+
use ReshapeArgumentsAware;
27+
28+
/**
29+
* The argument names, in order expected by legacy plugins.
30+
*
31+
* @var array
32+
*
33+
* @since __DEPLOY_VERSION__
34+
* @deprecated __DEPLOY_VERSION__ will be removed in 6.0
35+
*/
36+
protected $legacyArgumentsOrder = [];
37+
38+
/**
39+
* Constructor.
40+
*
41+
* @param string $name The event name.
42+
* @param array $arguments The event arguments.
43+
*
44+
* @since __DEPLOY_VERSION__
45+
*/
46+
public function __construct($name, array $arguments = [])
47+
{
48+
// Reshape the arguments array to preserve b/c with legacy listeners
49+
if ($this->legacyArgumentsOrder) {
50+
$arguments = $this->reshapeArguments($arguments, $this->legacyArgumentsOrder);
51+
}
52+
53+
parent::__construct($name, $arguments);
54+
}
55+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/**
4+
* Joomla! Content Management System
5+
*
6+
* @copyright (C) 2023 Open Source Matters, Inc. <https://www.joomla.org>
7+
* @license GNU General Public License version 2 or later; see LICENSE.txt
8+
*/
9+
10+
namespace Joomla\CMS\Event\Extension;
11+
12+
// phpcs:disable PSR1.Files.SideEffects
13+
\defined('_JEXEC') or die;
14+
// phpcs:enable PSR1.Files.SideEffects
15+
16+
/**
17+
* Class for Joomla Update events
18+
*
19+
* @since __DEPLOY_VERSION__
20+
*/
21+
class AfterJoomlaUpdateEvent extends AbstractJoomlaUpdateEvent
22+
{
23+
/**
24+
* The argument names, in order expected by legacy plugins.
25+
*
26+
* @var array
27+
*
28+
* @since __DEPLOY_VERSION__
29+
* @deprecated __DEPLOY_VERSION__ will be removed in 6.0
30+
*/
31+
protected $legacyArgumentsOrder = ['oldVersion'];
32+
33+
/**
34+
* Pre-setter for the oldVersion argument.
35+
*
36+
* @param ?string $value The value to set
37+
*
38+
* @return string
39+
*
40+
* @since __DEPLOY_VERSION__
41+
*/
42+
protected function onSetOldVersion(?string $value): string
43+
{
44+
return $value ?? '';
45+
}
46+
47+
/**
48+
* Getter for the oldVersion.
49+
*
50+
* @return string
51+
*
52+
* @since __DEPLOY_VERSION__
53+
*/
54+
public function getOldVersion(): string
55+
{
56+
return $this->arguments['oldVersion'] ?? '';
57+
}
58+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/**
4+
* Joomla! Content Management System
5+
*
6+
* @copyright (C) 2023 Open Source Matters, Inc. <https://www.joomla.org>
7+
* @license GNU General Public License version 2 or later; see LICENSE.txt
8+
*/
9+
10+
namespace Joomla\CMS\Event\Extension;
11+
12+
// phpcs:disable PSR1.Files.SideEffects
13+
\defined('_JEXEC') or die;
14+
// phpcs:enable PSR1.Files.SideEffects
15+
16+
/**
17+
* Class for Joomla Update events
18+
*
19+
* @since __DEPLOY_VERSION__
20+
*/
21+
class BeforeJoomlaUpdateEvent extends AbstractJoomlaUpdateEvent
22+
{
23+
}

plugins/extension/finder/src/Extension/Finder.php

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@
1010

1111
namespace Joomla\Plugin\Extension\Finder\Extension;
1212

13-
use Joomla\CMS\Installer\Installer;
13+
use Joomla\CMS\Event\Extension\AbstractExtensionEvent;
14+
use Joomla\CMS\Event\Extension\AfterInstallEvent;
15+
use Joomla\CMS\Event\Extension\AfterUninstallEvent;
16+
use Joomla\CMS\Event\Extension\AfterUpdateEvent;
1417
use Joomla\CMS\Plugin\CMSPlugin;
1518
use Joomla\Component\Finder\Administrator\Indexer\Helper;
1619
use Joomla\Database\DatabaseAwareTrait;
1720
use Joomla\Database\ParameterType;
21+
use Joomla\Event\SubscriberInterface;
1822
use Joomla\String\StringHelper;
1923

2024
// phpcs:disable PSR1.Files.SideEffects
@@ -26,22 +30,39 @@
2630
*
2731
* @since 4.0.0
2832
*/
29-
final class Finder extends CMSPlugin
33+
final class Finder extends CMSPlugin implements SubscriberInterface
3034
{
3135
use DatabaseAwareTrait;
3236

37+
/**
38+
* Returns an array of events this subscriber will listen to.
39+
*
40+
* @return array
41+
*
42+
* @since __DEPLOY_VERSION__
43+
*/
44+
public static function getSubscribedEvents(): array
45+
{
46+
return [
47+
'onExtensionAfterInstall' => 'onExtensionAfterInstall',
48+
'onExtensionAfterUpdate' => 'onExtensionAfterUpdate',
49+
'onExtensionAfterUninstall' => 'onExtensionAfterUninstall',
50+
];
51+
}
52+
3353
/**
3454
* Add common words to finder after language got installed
3555
*
36-
* @param Installer $installer Installer object
37-
* @param integer $eid Extension Identifier
56+
* @param AfterInstallEvent $event Event instance.
3857
*
3958
* @return void
4059
*
4160
* @since 4.0.0
4261
*/
43-
public function onExtensionAfterInstall($installer, $eid)
62+
public function onExtensionAfterInstall(AbstractExtensionEvent $event): void
4463
{
64+
$eid = $event->getEid();
65+
4566
if (!$eid) {
4667
return;
4768
}
@@ -68,31 +89,32 @@ public function onExtensionAfterInstall($installer, $eid)
6889
/**
6990
* Add common words to finder after language got updated
7091
*
71-
* @param Installer $installer Installer object
72-
* @param integer $eid Extension identifier
92+
* @param AfterUpdateEvent $event Event instance.
7393
*
7494
* @return void
7595
*
7696
* @since 4.0.0
7797
*/
78-
public function onExtensionAfterUpdate($installer, $eid)
98+
public function onExtensionAfterUpdate(AfterUpdateEvent $event): void
7999
{
80-
$this->onExtensionAfterInstall($installer, $eid);
100+
$this->onExtensionAfterInstall($event);
81101
}
82102

83103
/**
84104
* Remove common words to finder after language got uninstalled
85105
*
86-
* @param Installer $installer Installer instance
87-
* @param integer $eid Extension id
88-
* @param boolean $removed Installation result
106+
* @param AfterUninstallEvent $event Event instance.
89107
*
90108
* @return void
91109
*
92110
* @since 4.0.0
93111
*/
94-
public function onExtensionAfterUninstall($installer, $eid, $removed)
112+
public function onExtensionAfterUninstall(AfterUninstallEvent $event): void
95113
{
114+
$installer = $event->getInstaller();
115+
$eid = $event->getEid();
116+
$removed = $event->getRemoved();
117+
96118
// Check that the language was successfully uninstalled.
97119
if ($eid && $removed && $installer->extension->type === 'language') {
98120
$this->removeCommonWords($installer->extension);

0 commit comments

Comments
 (0)