Jump to content

Modules hooks calling Context even with commands


Recommended Posts

I call some methods of my modules from cron with the implementation of Symfony Commands.

It works perfectly, but sometimes, when I create a user for instance, and a module like PSrecaptcha has a hook with this kind of context call, it triggers a notice which stops the execution of my script.

/**
     * HookActionObjectCustomerAddBefore
     * Magic Hook
     */
    public function hookActionObjectCustomerAddBefore()
    {
        if (!$this->isModuleActive() || Configuration::get('RECAPTCHA_REGISTRATIONFORM') == '0') {
            return;
        }

        if ($this->context->controller->controller_type == 'front') {
            if (!$this->verifyRecaptcha()) {
                $params['create_account'] = 1;
                $params['captchaErrorMsg'] = $this->l('Invalid captcha challenge, please retry.');
                Tools::redirect($this->context->link->getPageLink(
                    'authentication',
                    true,
                    null,
                    $params,
                    true
                ));
            }
        }
    }

The following notice is triggered obviously... :

 Notice: Trying to get property 'controller_type' of non-object

Of course, since this is called in command line there is no context.

Do you have an elegant way to solve this, for example :

  • Modifying the Psrecaptcha code and checks if context exists before the call to the context (is it safe anymore ?). The problem with this solution is that I'm not responsible of the code of every module that hooks themselves everywhere and call Context : I can't modify them all.
  • Find a way to continue the execution of the command even if it triggers a notice like with " --no-debug" option, but will it hide all errors ?
  • Find a way to bypass all context calling or bypass all hooks when the script is launched from command line.

I work with PS 1.7.6.3

Thank you

Edited by Bruno G
added --no-debug option (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...