Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.2][Events] Use event classes for Extension plugins #43617

Merged
merged 8 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -12,6 +12,8 @@

use Joomla\CMS\Authentication\Authentication;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Event\Extension\AfterJoomlaUpdateEvent;
use Joomla\CMS\Event\Extension\BeforeJoomlaUpdateEvent;
use Joomla\CMS\Extension\ExtensionHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Filter\InputFilter;
Expand Down Expand Up @@ -580,7 +582,7 @@ public function createUpdateFile($basename = null): bool
$app = Factory::getApplication();

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

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

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

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

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

try {
Expand Down
2 changes: 2 additions & 0 deletions libraries/src/Event/CoreEventAware.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ trait CoreEventAware
'onExtensionAfterSave' => Model\AfterSaveEvent::class,
'onExtensionAfterDelete' => Model\AfterDeleteEvent::class,
'onExtensionChangeState' => Model\BeforeChangeStateEvent::class,
'onJoomlaBeforeUpdate' => Extension\BeforeJoomlaUpdateEvent::class,
'onJoomlaAfterUpdate' => Extension\AfterJoomlaUpdateEvent::class,
// Installer
'onInstallerAddInstallationTab' => Installer\AddInstallationTabEvent::class,
'onInstallerBeforeInstallation' => Installer\BeforeInstallationEvent::class,
Expand Down
55 changes: 55 additions & 0 deletions libraries/src/Event/Extension/AbstractJoomlaUpdateEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/**
* Joomla! Content Management System
*
* @copyright (C) 2023 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\CMS\Event\Extension;

use Joomla\CMS\Event\AbstractImmutableEvent;
use Joomla\CMS\Event\ReshapeArgumentsAware;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects

/**
* Base class for Joomla Update events
*
* @since __DEPLOY_VERSION__
*/
abstract class AbstractJoomlaUpdateEvent extends AbstractImmutableEvent
{
use ReshapeArgumentsAware;

/**
* The argument names, in order expected by legacy plugins.
*
* @var array
*
* @since __DEPLOY_VERSION__
* @deprecated __DEPLOY_VERSION__ will be removed in 6.0
*/
protected $legacyArgumentsOrder = [];

/**
* Constructor.
*
* @param string $name The event name.
* @param array $arguments The event arguments.
*
* @since __DEPLOY_VERSION__
*/
public function __construct($name, array $arguments = [])
{
// Reshape the arguments array to preserve b/c with legacy listeners
if ($this->legacyArgumentsOrder) {
$arguments = $this->reshapeArguments($arguments, $this->legacyArgumentsOrder);
}

parent::__construct($name, $arguments);
}
}
58 changes: 58 additions & 0 deletions libraries/src/Event/Extension/AfterJoomlaUpdateEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/**
* Joomla! Content Management System
*
* @copyright (C) 2023 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\CMS\Event\Extension;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects

/**
* Class for Joomla Update events
*
* @since __DEPLOY_VERSION__
*/
class AfterJoomlaUpdateEvent extends AbstractJoomlaUpdateEvent
{
/**
* The argument names, in order expected by legacy plugins.
*
* @var array
*
* @since __DEPLOY_VERSION__
* @deprecated __DEPLOY_VERSION__ will be removed in 6.0
*/
protected $legacyArgumentsOrder = ['oldVersion'];

/**
* Pre-setter for the oldVersion argument.
*
* @param ?string $value The value to set
*
* @return string
*
* @since __DEPLOY_VERSION__
*/
protected function onSetOldVersion(?string $value): string
{
return $value ?? '';
}

/**
* Getter for the oldVersion.
*
* @return string
*
* @since __DEPLOY_VERSION__
*/
public function getOldVersion(): string
{
return $this->arguments['oldVersion'] ?? '';
}
}
23 changes: 23 additions & 0 deletions libraries/src/Event/Extension/BeforeJoomlaUpdateEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/**
* Joomla! Content Management System
*
* @copyright (C) 2023 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\CMS\Event\Extension;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects

/**
* Class for Joomla Update events
*
* @since __DEPLOY_VERSION__
*/
class BeforeJoomlaUpdateEvent extends AbstractJoomlaUpdateEvent
{
}
48 changes: 35 additions & 13 deletions plugins/extension/finder/src/Extension/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@

namespace Joomla\Plugin\Extension\Finder\Extension;

use Joomla\CMS\Installer\Installer;
use Joomla\CMS\Event\Extension\AbstractExtensionEvent;
use Joomla\CMS\Event\Extension\AfterInstallEvent;
use Joomla\CMS\Event\Extension\AfterUninstallEvent;
use Joomla\CMS\Event\Extension\AfterUpdateEvent;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\Component\Finder\Administrator\Indexer\Helper;
use Joomla\Database\DatabaseAwareTrait;
use Joomla\Database\ParameterType;
use Joomla\Event\SubscriberInterface;
use Joomla\String\StringHelper;

// phpcs:disable PSR1.Files.SideEffects
Expand All @@ -26,22 +30,39 @@
*
* @since 4.0.0
*/
final class Finder extends CMSPlugin
final class Finder extends CMSPlugin implements SubscriberInterface
{
use DatabaseAwareTrait;

/**
* Returns an array of events this subscriber will listen to.
*
* @return array
*
* @since __DEPLOY_VERSION__
*/
public static function getSubscribedEvents(): array
{
return [
'onExtensionAfterInstall' => 'onExtensionAfterInstall',
'onExtensionAfterUpdate' => 'onExtensionAfterUpdate',
'onExtensionAfterUninstall' => 'onExtensionAfterUninstall',
];
}

/**
* Add common words to finder after language got installed
*
* @param Installer $installer Installer object
* @param integer $eid Extension Identifier
* @param AfterInstallEvent $event Event instance.
*
* @return void
*
* @since 4.0.0
*/
public function onExtensionAfterInstall($installer, $eid)
public function onExtensionAfterInstall(AbstractExtensionEvent $event): void
{
$eid = $event->getEid();

if (!$eid) {
return;
}
Expand All @@ -68,31 +89,32 @@ public function onExtensionAfterInstall($installer, $eid)
/**
* Add common words to finder after language got updated
*
* @param Installer $installer Installer object
* @param integer $eid Extension identifier
* @param AfterUpdateEvent $event Event instance.
*
* @return void
*
* @since 4.0.0
*/
public function onExtensionAfterUpdate($installer, $eid)
public function onExtensionAfterUpdate(AfterUpdateEvent $event): void
{
$this->onExtensionAfterInstall($installer, $eid);
$this->onExtensionAfterInstall($event);
}

/**
* Remove common words to finder after language got uninstalled
*
* @param Installer $installer Installer instance
* @param integer $eid Extension id
* @param boolean $removed Installation result
* @param AfterUninstallEvent $event Event instance.
*
* @return void
*
* @since 4.0.0
*/
public function onExtensionAfterUninstall($installer, $eid, $removed)
public function onExtensionAfterUninstall(AfterUninstallEvent $event): void
{
$installer = $event->getInstaller();
$eid = $event->getEid();
$removed = $event->getRemoved();

// Check that the language was successfully uninstalled.
if ($eid && $removed && $installer->extension->type === 'language') {
$this->removeCommonWords($installer->extension);
Expand Down
Loading