We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 42050 ☆ A M B ☆
    • 4 Posts
    I am trying to implement a custom error and exception handler. It is in the form of a PHP class and a single call to instantiate it.

    My problem is to have the class loaded and the calls executed at the moment MODX is instantiated, so they are ready and available everywhere.

    I have looked into quite a few ways of doing it, and think the most proper way would be using the system setting 'extension_packages'. My problem is that this is for loading packages that extend MODX objects, which is not what I need, and I can't find any examples of creating a package I can load that doesn't extend any object or use any database tables.

    I've got a feeling that it isn't that hard, or at least that it shouldn't be.!?
      • 22303 MODX Staff
      • 10,725 Posts
      You can certainly use MODX extension_packages to specify a location to load your own classes from, or you can use PHP autoloading conventions if you prefer.
        • 42050 ☆ A M B ☆
        • 4 Posts
        I would prefer keeping everything MODX for this, for several reasons.

        What I can't seem to find anywhere, is documentation of what I should put in 'extension_packages' and how, for the class to get included and instantiated when MODX is. Do I really have to create a package or can I somehow just specify the path for the class file?
          • 9207 ☆ A M B ☆
          • 2,475 Posts
          Agreed: it shouldn't be that hard. For all of its inane idiocy, WordPress' functions.php file is damn useful.

          The format in the System Settings is that of a JSON array/hash:

          [{"your_package":{"path":"[[++core_path]]components/your_package/model/"}}]


          I don't find much docs on the addExtensionPackage, and from looking at the above, it's not at all clear what files inside of the model directory might get loaded. It would be damn useful if we could just have a list of files that would be included on every page request (or at least on page requests in the manager).
            • 3749
            • 24,544 Posts
            This runs whenever $modx is initialized (i.e., on every page request).

             protected function _loadExtensionPackages($options = null) {
                    $extPackages = $this->getOption('extension_packages');
                    $extPackages = $this->fromJSON($extPackages);
                    if (!is_array($extPackages)) $extPackages = array();
                    if (is_array($options) && array_key_exists('extension_packages', $options)) {
                        $optPackages = $this->fromJSON($options['extension_packages']);
                        if (is_array($optPackages)) {
                            $extPackages = array_merge($extPackages, $optPackages);
                        }
                    }
                    if (!empty($extPackages)) {
                        foreach ($extPackages as $extPackage) {
                            if (!is_array($extPackage)) continue;
            
                            foreach ($extPackage as $packageName => $package) {
                                if (!empty($package) && !empty($package['path'])) {
                                    $package['tablePrefix'] = !empty($package['tablePrefix']) ? $package['tablePrefix'] : null;
                                    $package['path'] = str_replace(array(
                                        '[[++core_path]]',
                                        '[[++base_path]]',
                                        '[[++assets_path]]',
                                        '[[++manager_path]]',
                                    ),array(
                                        $this->config['core_path'],
                                        $this->config['base_path'],
                                        $this->config['assets_path'],
                                        $this->config['manager_path'],
                                    ),$package['path']);
                                    $this->addPackage($packageName,$package['path'],$package['tablePrefix']);
                                    if (!empty($package['serviceName']) && !empty($package['serviceClass'])) {
                                        $packagePath = str_replace('//','/',$package['path'].$packageName.'/');
                                        $this->getService($package['serviceName'],$package['serviceClass'],$packagePath);
                                    }
                                }
                            }
                        }
                    }
                }
              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting
              • 42050 ☆ A M B ☆
              • 4 Posts
              Thanks for your replies smiley

              I ended up just making a plugin and having it run on OnHandleRequest. I was trying to avoid that solution, but it seemed better (as in, it works and hardly takes any time to set up) when I only had a single PHP class and no packages, database tables, etc.

              I didn't want to go into loading it through any PHP or server settings, as I think that would make it less portable when moving from development to production server, both because it's one more thing to remember setting up and it's not something all hosts support.
                • 9207 ☆ A M B ☆
                • 2,475 Posts
                I think you've hit upon the simplest solution... gives me an idea for a plugin: a simple CMP that lets a user list files to be included/required on every page request.