-
☆ 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.!?
-
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.
-
☆ 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?
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);
}
}
}
}
}
}
-
☆ 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.