1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Check if event listener exists before install?

Discussion in 'XenForo Development Discussions' started by Mikey, May 23, 2011.

  1. Mikey

    Mikey Well-Known Member

    What I want to do is to be able to have the same code event listener packaged with all of my add ons, but only installed/enabled if it doesnt already exist.

    Is this something XenForo takes care of automatically, or is there a way to detect if it already exists?
     
  2. Mikey

    Mikey Well-Known Member

    I'm sure someone knows the answer to this, the way I phrased the question probably wasn't ideal.

    Add_On_One has /library/Mikey/Mikey.php which has an event listener set up with Add_On_One on XenForo's side.

    Add_On_Two also has the same event listener, pointing to the same code, same file even (/library/Mikey/Mikey.php).

    The problem is that if both add ons are enabled at the same time, then that code is executed twice.

    I intend to release both add ons (at some point), so removing the event listener from one isn't an option as I don't want to have to make one a prerequisite of another, they both do different things, and in future I want to be able to have this event listener in all my mods.

    How would I check either during install or in the /library/Mikey/Mikey.php file to prevent double/triple/quadruple execution?

    If it helps, /library/Mikey/Mikey.php is a template hook file.

    Hope that cleared up any confusion.. and hope someone can help me.. thx :)
     
  3. Martin Aronsen

    Martin Aronsen Active Member

    Can't you have a third add-on that just acts as your core for the other two, and bundle that add-on with the others when you release them? I've seen this done by one dev here, think it's either Jaxel or ragtek.
     
  4. Mikey

    Mikey Well-Known Member

    I've considered that, like the "Ragtek-Framework" add on which ragtek here uses, the problem is that it creates the dependency which isn't something I want, really.

    If it's needed, I'll do it, but eh, there has to be a more elegant solution.
     
  5. Kier

    Kier XenForo Developer Staff Member

    Allow it to be called twice (or many times) but set a static variable on first execution that will return early without running the main part of the callback once set.
     
    Mikey likes this.
  6. Mikey

    Mikey Well-Known Member

    Okay, so I've just tried this.

    I tried:
    PHP:
    if (empty($tempvar)) {
    switch (
    $hookName) {
    // hook
    $tempvar '1'
    }
    }
    which didn't seem to do anything..

    PHP:
    switch ($hookName) {
    if (empty(
    $contents)) {
    // hook

    }
    }
    Which just caused the code to not execute.

    And as a last ditch attempt, a while loop.
    PHP:
    $tempvar '0';
    while (
    $tempvar '1') {
    switch (
    $hookName) {
    // hook stuff
    $tempvar++;
    }
    }
    Broke the site (max execution time exceeded, lol).

    Am I going about that all wrong?
     
  7. Martin Aronsen

    Martin Aronsen Active Member

    I think Kier was thinking more in lines of:
    PHP:
    if ( defined'IVE_ALREADY_BEEN_TROUGH_THIS_AWESOME_MIKEYFUNCTION' ) )
    {
        return 
    false;
    }

    define'IVE_ALREADY_BEEN_TROUGH_THIS_AWESOME_MIKEYFUNCTION'true );

    /* Continue with the rest */
    Or set a class variable, which is only possible if the Controller don't make a new instance of the listeners.
    PHP:
    class myclass ....
    {
        private 
    $weveBeenTroughThis false;

        public function ... ()
        {
            if ( 
    $this->weveBeenTroughThis ) { return false; }
            
    $this->weveBeenTroughThis true;
        }
    }
     
    Tilki├čey, Kier and Mikey like this.
  8. Mikey

    Mikey Well-Known Member

    Thanks Kier and Martin, I opted for the define method which works perfectly. I don't usually use defines so I hadn't thought of that. :coffee:
     
    Martin Aronsen likes this.

Share This Page