This page was imported from my old Wordpress Blog (which I stopped updating in 2011) and are here mainly for posterity. Although I have tried to clean things up there will be broken links and potentially other issues.
This is a port of the Application Constants plugin for Mach-II Coldfusion by Martin Laine, there is one difference between the two, which is outlined below.
This plugin allows you to have a different set of application constants for each server running your Mach-II application. This can be useful if you have different settings for your different environments, for example debugging turned on within the development environment.
Note: this plugin requires the PHP 5 version of Mach-II, this is mainly because of the usage of simple_xml. It could be re-worked to run on the PHP 4 version; however I don’t have anyway to test that.
The Coldfusion version also registers a preEvent()
method, which places the AppConstants structure into the event-args for each event. This was done due to the hacky nature of getting at Mach-II properties prior to the 1.0.10 Coldfusion version. I never thought this was nice, and we even hacked the Coldfusion version we were using at work to put each constant directly into the properties.
As the PHP version of Mach-II has being built with the BaseComponent in place since it’s public release, which allows direct access to Mach-II properties from anywhere where you’re likely to need them, I decided not to include this fudge, and Martin had already added the ability to set properties directly in the latest version.
I’m sure the preEvent()
method is is only still in place in the Coldfusion version to provided backwards compatability for current users.
Place AppConstants.php in the MachII plugins folder.
<plugin name="appConstants" type="MachII.plugins.appConstants">
<parameters>
<parameter name="constantsFile" value="path/to/constants/file.xml" />
</parameters>
</plugin>
The constantsFile optional parameter should be a path to your constants XML file relative to your application root (set in the properties section of your configuration file). It defaults to ` /config/constants.xml`.
The plugin will attempt to resolve the full path to the file by using testing the path combined with each of the values set in include_path
ini variable.
<?xml version="1.0" encoding="utf-8"?>
<constants>
<common>
<constant name="constant_name_1" value="constant_value_1" />
...
<property name="property_name_1" value="property_value_1" />
...
</common>
<server name="your_dev_server_name">
<constant name="constant_name_1" value="constant_value_1" />
...
<property name="property_name_1" value="property_value_1" />
...
</server>
<server name="your_testing_server_name">
<constant name="constant_name_1" value="constant_value_1" />
...
<property name="property_name_1" value="property_value_1" />
...
</server>
<server name="your_live_server_name">
<constant name="constant_name_1" value="constant_value_1" />
...
<property name="property_name_1" value="property_value_1" />
...
</server>
</constants>
<common>
All constants & properties within the common section will be available no matter which server the application is running on. Constants & properties set within the common section are over-ridden by those with the same name in the individual server sections.
<server>
You will have access to the constants within the server section of the constants file that corresponds to the server currently running your application on. The server name can be an individual server or a comma separated list, useful if several domains point to the same application or in a development environment where each developer works on a local copy etc.
<constant>
All constants are placed in an array, with named indices, named appConstants that you can access as a Mach-II property, this is handy if you are worried about conflicts with property names within the Mach-II configuration file.
<property>
All properties are placed directly in Mach-II properties, these will overwrite any existing Mach-II properties with the same name.
Note: You can use this plugin for a single server setup. Place all your constants in the common block and don’t bother with any server blocks.