Issue with code events in a bridge


Well-known member
I'm currently working on a separate script connected to my XF installation using the code from this post
// Include custom libraries and initialization code here
// How to use XenForo Objects: Once initialized the below you can use XenForo objects
define('XF_ROOT', MY_SCRIPT_PATH . '/path/to/xf/root/');
define('TIMENOW', time());
define('SESSION_BYPASS', false); // if true: logged in user info and sessions are not needed
require_once(XF_ROOT . '/library/XenForo/Autoloader.php');
XenForo_Autoloader::getInstance()->setupAutoloader(XF_ROOT . '/library');
XenForo_Application::initialize(XF_ROOT . '/library', XF_ROOT);
XenForo_Application::set('page_start_time', TIMENOW);
error_reporting(E_ALL & ~E_NOTICE); // Turn off the strict error reporting.

It runs quite well except for one particular thing: the code event listeners I set up (especially in order to extend XenForo_Model_XXXX) do not seem to fire. Instead, I get an error
Call to undefined method XenForo_Model_XXX::YYY()
each time I try to access one of my custom functions.

NB: The code events fire totally fine (i.e.: I'm able to access my member functions) when I create an instance from within XF script. The problem is only within my bridge script.

I finally managed to get my own listeners working by adding them manually via this line of code, added just below the previous block:
XenForo_CodeEvent::addListener('load_class_model', array('My_Listeners_Class', 'listenerCallback'));
This is fine for listeners I am aware of, but it is not suitable in cases where other addons modify a function behaviour.

Where should I have a look in order to fix this issue?


Well-known member
Slept on this, figured it out.

I've had a look at how code event listeners are gathered and stored within the XenForo_CodeEvent::$_listeners array. I found that its source is initialized in XenForo_Dependencies_Abstract::preloadData().

Examples of a call to this function can be found in XenForo_Cron::runAndOutput() and XenForo_CssOutput::run().
All it takes is two lines:
$dependencies = new XenForo_Dependencies_Public();

That's it. Adding these two lines below my code enabled my models to be correctly extended.

Disclaimer: This works, but I don't know whether this is a good way of doing things. Only XF devs may be able to answer this? If anybody has clues about possible issues with this workaround, I'm interested in hearing them.