XF 2.0 Extending App() containers?

Jaxel

Well-known member
I'm setting up a new report system... and to do it, I'm going through how the existing report system works and trying to emulate it as much as possible.

However, I'm running into a snag in XF\App. In this class, reportCounts is used twice.

Firstly, its preloaded in $preLoadShared. Secondly, its cached in initialize():
Code:
        $container['reportCounts'] = $this->fromRegistry('reportCounts',
            function(Container $c) { return $c['em']->getRepository('XF:Report')->rebuildReportCounts(); }
        );

How would I extend this class to create my own counters?
 
Set up an app_setup event listener and do something like:
PHP:
        $container = $app->container();

        $container['vendor.key'] = $app->fromRegistry(
            'vendorKey',
            function (Container $c) {
                $data = []; // or whatever you want to set in the registry/container

                return $data;
            }
        );

This will fetch any existing data from the registry, or get the data from the closure and cache it to the registry if it does not already exist. Note that you will need to manually clean up the registry during your uninstall routines.

Adding the key to the $preLoadShared array will pre-load the data from the registry on every page, allowing you to avoid an additional query if your data is needed for most pages. There doesn't actually seem to be any way to add to this list though, as far as I can see.

You can fetch the value in your add-on like this:
PHP:
$data = $this->app()->container('vendor.key');
 
Last edited:
Just an FYI; but code event listeners don't run when your add-on is upgrading but template modifications/macros may still be.
 
Hello all!
I need some help about an app_setup listener.
I need to load on every page load, some rows from the registry (on the very initial query of xF, cause currently the registry rows when the ->get called, is fetching that via separate queries).

Could someone let me know the exact code which I have to apply on my app_setup method, to extend the array $preLoadShared etc?
Sorry, I don't understand the relative code on the post #2.

Let's say that the extra rows (data_key) on the registry are ['a', 'b', 'c'].

Maybe @Jeremy P knows? (y)
 
Could someone let me know the exact code which I have to apply on my app_setup method, to extend the array $preLoadShared etc?
Sorry, I don't understand the relative code on the post #2.
Adding the key to the $preLoadShared array will pre-load the data from the registry on every page, allowing you to avoid an additional query if your data is needed for most pages. There doesn't actually seem to be any way to add to this list though, as far as I can see.
You can't. You can set up caching though, in which case data registry entries will be cached and you can typically avoid hitting the database.
 
Back
Top Bottom