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

Open
wants to merge 6 commits into
base: 5.2-dev
Choose a base branch
from

Conversation

Fedik
Copy link
Member

@Fedik Fedik commented Jun 4, 2024

User description

Summary of Changes

Update extension and installer plugins to use SubscriberInterface event classes.

Testing Instructions

Test that extension installation works as before.
Joomla update also works as before.

Actual result BEFORE applying this Pull Request

Works

Expected result AFTER applying this Pull Request

Works

Link to documentations

Please select:

  • Documentation link for docs.joomla.org:
  • No documentation changes for docs.joomla.org needed
  • Pull Request link for manual.joomla.org: New Event classes Manual#177
  • No documentation changes for manual.joomla.org needed

PR Type

Enhancement


Description

  • Updated extension and installer plugins to use SubscriberInterface event classes.
  • Introduced AbstractJoomlaUpdateEvent class and its concrete implementations BeforeJoomlaUpdateEvent and AfterJoomlaUpdateEvent.
  • Modified Joomla update process to dispatch new event classes.
  • Implemented SubscriberInterface for various extension plugins and updated their methods to use event classes.

Changes walkthrough 📝

Relevant files
Enhancement
9 files
UpdateModel.php
Use event classes for Joomla update process                           

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

  • Added dispatching of BeforeJoomlaUpdateEvent and
    AfterJoomlaUpdateEvent instead of triggering events directly.
  • Included new event classes for Joomla update process.
  • +8/-3     
    CoreEventAware.php
    Map Joomla update events to core events                                   

    libraries/src/Event/CoreEventAware.php

  • Added onJoomlaBeforeUpdate and onJoomlaAfterUpdate to the core event
    mapping.
  • +2/-0     
    AbstractJoomlaUpdateEvent.php
    Introduce abstract class for Joomla update events               

    libraries/src/Event/Extension/AbstractJoomlaUpdateEvent.php

  • Introduced abstract class AbstractJoomlaUpdateEvent for Joomla update
    events.
  • +55/-0   
    AfterJoomlaUpdateEvent.php
    Add AfterJoomlaUpdateEvent class                                                 

    libraries/src/Event/Extension/AfterJoomlaUpdateEvent.php

  • Added AfterJoomlaUpdateEvent class extending
    AbstractJoomlaUpdateEvent.
  • +58/-0   
    BeforeJoomlaUpdateEvent.php
    Add BeforeJoomlaUpdateEvent class                                               

    libraries/src/Event/Extension/BeforeJoomlaUpdateEvent.php

  • Added BeforeJoomlaUpdateEvent class extending
    AbstractJoomlaUpdateEvent.
  • +23/-0   
    Finder.php
    Implement SubscriberInterface for Finder plugin                   

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

  • Implemented SubscriberInterface for the Finder extension plugin.
  • Updated methods to use event classes.
  • +35/-13 
    Joomla.php
    Implement SubscriberInterface for Joomla plugin                   

    plugins/extension/joomla/src/Extension/Joomla.php

  • Implemented SubscriberInterface for the Joomla extension plugin.
  • Updated methods to use event classes.
  • +36/-13 
    NamespaceMap.php
    Implement SubscriberInterface for NamespaceMap plugin       

    plugins/extension/namespacemap/src/Extension/NamespaceMap.php

  • Implemented SubscriberInterface for the NamespaceMap extension plugin.
  • Updated methods to use event classes.
  • +30/-15 
    Override.php
    Implement SubscriberInterface for Override plugin               

    plugins/installer/override/src/Extension/Override.php

  • Implemented SubscriberInterface for the Override extension plugin.
  • Added event subscription methods.
  • +21/-1   

    💡 PR-Agent usage:
    Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    Copy link

    PR Review 🔍

    ⏱️ Estimated effort to review [1-5]

    3, because the PR involves multiple files and changes related to event handling in a complex system like Joomla. Understanding the context and ensuring that the new event classes integrate seamlessly with the existing system requires a moderate level of effort.

    🧪 Relevant tests

    No

    ⚡ Possible issues

    Duplicate Event Dispatch: In UpdateModel.php, the AfterJoomlaUpdateEvent is dispatched twice in close succession within the cleanUp method. This could lead to unintended side effects or duplicate processing. One of these dispatches should likely be removed to prevent potential issues.

    🔒 Security concerns

    No

    @Fedik Fedik added the Feature label Jun 4, 2024
    Copy link

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Possible bug
    Change the parameter type to AfterInstallEvent to match the event being dispatched

    The method onExtensionAfterInstall is defined to accept an AbstractExtensionEvent
    parameter, but it should accept an AfterInstallEvent parameter to match the event being
    dispatched.

    plugins/extension/finder/src/Extension/Finder.php [62]

    -public function onExtensionAfterInstall(AbstractExtensionEvent $event): void
    +public function onExtensionAfterInstall(AfterInstallEvent $event): void
     
    Suggestion importance[1-10]: 10

    Why: The suggestion correctly identifies a mismatch in the parameter type for the method onExtensionAfterInstall, which should align with the dispatched event type for consistency and correctness.

    10
    Add a null check for fileCreator before calling its create method

    In the onExtensionAfterInstall method, the create method of fileCreator should be called
    only if the fileCreator is not null to avoid potential null pointer exceptions.

    plugins/extension/namespacemap/src/Extension/NamespaceMap.php [84-86]

    -if ($event->getEid()) {
    +if ($event->getEid() && $this->fileCreator !== null) {
         // Update / Create new map
         $this->fileCreator->create();
     }
     
    Suggestion importance[1-10]: 7

    Why: This is a good suggestion to prevent potential null pointer exceptions by ensuring fileCreator is not null before invoking its method, enhancing the robustness of the code.

    7
    Add a null check for installer before accessing its properties in the onExtensionAfterUninstall method

    In the onExtensionAfterUninstall method, add a null check for installer before accessing
    its properties to prevent potential null pointer exceptions.

    plugins/extension/joomla/src/Extension/Joomla.php [195-196]

    -if ($eid && $removed) {
    +if ($eid && $removed && $this->installer !== null) {
         $this->removeUpdateSites($eid);
     }
     
    Suggestion importance[1-10]: 7

    Why: Adding a null check for installer is a prudent measure to avoid null pointer exceptions, especially since the method relies on properties of installer. This suggestion improves the safety and reliability of the method.

    7
    Possible issue
    Remove the redundant event dispatch call to avoid triggering the same event twice

    The @TODO comment indicates that the event is dispatched twice in the cleanUp method. To
    avoid redundancy, remove the first dispatch call.

    administrator/components/com_joomlaupdate/src/Model/UpdateModel.php [887-888]

    -// @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'));
    +// Trigger event after joomla update.
    +$app->getDispatcher()->dispatch('onJoomlaAfterUpdate', new AfterJoomlaUpdateEvent('onJoomlaAfterUpdate', [
    +    'oldVersion' => $oldVersion ?: '',
    +]));
     
    Suggestion importance[1-10]: 8

    Why: The suggestion is valid as it addresses a redundancy issue where an event is dispatched twice in the same method, which could lead to unintended behaviors or performance issues.

    8
    Maintainability
    Use class constants for event names to avoid potential issues with method name changes

    To avoid potential issues with method name changes, consider using class constants for the
    event names in the getSubscribedEvents method.

    plugins/installer/override/src/Extension/Override.php [51-58]

     return [
    -    'onExtensionBeforeUpdate'    => 'onExtensionBeforeUpdate',
    -    'onExtensionAfterUpdate'     => 'onExtensionAfterUpdate',
    -    'onJoomlaBeforeUpdate'       => 'onJoomlaBeforeUpdate',
    -    'onJoomlaAfterUpdate'        => 'onJoomlaAfterUpdate',
    -    'onInstallerBeforeInstaller' => 'onInstallerBeforeInstaller',
    -    'onInstallerAfterInstaller'  => 'onInstallerAfterInstaller',
    +    self::EVENT_EXTENSION_BEFORE_UPDATE    => 'onExtensionBeforeUpdate',
    +    self::EVENT_EXTENSION_AFTER_UPDATE     => 'onExtensionAfterUpdate',
    +    self::EVENT_JOOMLA_BEFORE_UPDATE       => 'onJoomlaBeforeUpdate',
    +    self::EVENT_JOOMLA_AFTER_UPDATE        => 'onJoomlaAfterUpdate',
    +    self::EVENT_INSTALLER_BEFORE_INSTALLER => 'onInstallerBeforeInstaller',
    +    self::EVENT_INSTALLER_AFTER_INSTALLER  => 'onInstallerAfterInstaller',
     ];
     
    Suggestion importance[1-10]: 7

    Why: Using class constants for event names is a good practice for maintainability and avoids hard-coding strings, which can lead to errors if event names change.

    7
    Enhancement
    Add a constructor to the class to initialize properties or perform necessary setup

    Add a constructor to the BeforeJoomlaUpdateEvent class to initialize any necessary
    properties or perform any setup required for the event.

    libraries/src/Event/Extension/BeforeJoomlaUpdateEvent.php [21-23]

     class BeforeJoomlaUpdateEvent extends AbstractJoomlaUpdateEvent
     {
    +    public function __construct()
    +    {
    +        // Initialize properties or perform setup
    +        parent::__construct();
    +    }
     }
     
    Suggestion importance[1-10]: 5

    Why: Adding a constructor might be beneficial for initializing properties or performing setup, but it's not strictly necessary unless there's specific initialization required, which isn't indicated in the PR or the suggestion.

    5

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    None yet

    3 participants