XF 2.2 How to call function from specific class in setup

FoxSecrets

Active member
I need to call a function from my class during the uninstall process.

How can I do that?

Code:
public function uninstallStep1()
    {
        \XF::app()->actionRemove();
    }
 
You can instantiate objects and call methods in uninstall methods as usual. If you mean a controller action, those are geared towards the request/response lifecycle. In the core, a lot of logic is typically abstracted into services so that they can be reused in different settings (including controllers).

Though for removing data during uninstallation I'd probably recommend just using the database adapter directly.
 
Following the example from Chris, I'm getting error in return new YourHelperClass();
It's expecting 2 argument but found zero. Is this related to $app and $request?
I'm newbie, can't find these in my class. Does anyone know what should I write there?

1691859239263.png

1691861814952.png
 
Last edited:
Hi all. I had it working in localhost during development, but when I run in production I got error "Container key 'lic' not found.
What's the reason to not work in production?

Code:
InvalidArgumentException: Container key 'lic' was not found in src/XF/Container.php at line 46

XF\Container->offsetGet() in src/XF/App.php at line 2524
XF\App->offsetGet() in src/addons/My/Addon/Setup.php at line 60
My\Addon\Setup->uninstallStep1() in src/XF/AddOn/StepRunnerUninstallTrait.php at line 61
// ...

SETUP
Code:
public function uninstallStep1()
    {
        $app = \XF::app();
        $container = $app['lic'];   ----> line 60

        if ($container) {
          $container->actionRemove();
        }
    }
 
Double check your code event listener - if you don't select your addon, it will work in dev, but it won't be included when you package up the addon and deploy it to production:

1693365656501.webp
 
I checked again the listener and it's correctly configured.
Is it due to call in the same add-on I'm trying to uninstall? Any other suggestion?
I see this information in console log, however the listener exists.

ContentDispatcherService: no listeners for an event TAB_STATE__GET_NAVIGATION_METHOD

PS: Checking again in localhost dev, it works when I run from CLI, but when uninstalling from addo-on menu got same error. So it seems maybe something related to XF\App->offsetGet() , what you think?
 
Last edited:
When the event listener cache gets rebuilt, it will exclude listeners from add-ons which are inactive or processing (installing/uninstalling/upgrading). I think the cache is normally rebuilt after the uninstall has completed, but it can be the case that the add-on was disabled prior to uninstallation or something else triggered it in the middle of processing.

I would say it is generally unsafe to rely on event listeners and class extensions during the processing phase (that is, inside the setup class) at all, which is why I had originally recommended using the database adapter (or schema manager) directly. If you need to instantiate your own class, you can do so using plain old PHP rather than the container:

PHP:
$myObject = new \My\Class(...)
 
Back
Top Bottom