The StartupCheck package in Polyphony allows you to automate, with the use of "requirement modules", the process required by most web applications to check system resources and requirements, and run autonomous (those not requiring user input) or non-autonomous updates/installations. The StartupCheck package is useful in the following areas, including others
- Checking for the presence of PHP modules/extensions
- Checking for required versions of PHP or other programs
- Checking for existence of database tables (and creating them if necessary)
- Automating the process of first-time-run configuration; ie, setting up preferences, adding users, etc.
- Checking for required php.ini configuration values.
There are two main ways to use the StartupCheck package: either in conjunction with the PHP session handler (suggested), or run every pageload. We will explain here how to use it with the session handler, as using it without is just simplifying the same process. The following code will check to make sure that the version of Harmoni we are using is at least 0.2.0, that register_globals and magic_quotes_gpc are off, and that we have the PHP extension gettext installed. The class files for these pre-installed requirements can be found in polyphony/main/library/StartupCheck/CommonRequirements.
In the above block of code, the first section first checks to see if the session variable __startupCheck has been defined, and if not, it creates a new StartupCheck object and stores it in the session. Next, it adds a number of requirements via the addRequirement() function. The first parameter is just a unique name that you assign to the requirement. The second is a class that implements the StartupRequirement interface (the file can be found in polyphony/main/library/StartupCheck/StartupRequirement.interface.php). The StartupRequirement is where all the action occurs. By changing what StartupRequirements you add to the StartupCheck class, you can control how the startup/update/install process of your program goes. The next sections will concentrate on the creation of these StartupRequirement classes.
The second section of the code above tells the StartupCheck class to go through all of the requirements and check their status, update them if necessary, and ask for user input if that's needed as well. The handleAllUpdates() method takes the Harmoni object as its paramter and returns true if everything's up-to-date or if things were updated on their own. It will return false if either something went wrong or if user input is needed. If the latter is the case, then the StartupCheck class outputs an HTML page asking for the required information, so your program should halt execution to let the user enter information.
The StartupRequirement interface is pretty simple. It requires that your class have only four methods
- getDisplayName() - returns a string with the display name of this requirement.
- getStatus() - returns an integer which describes the status of the requirement. A number of constants have been defined to use here, which are outlined below.
- createWizard() - returns a Wizard object containing an interface for user input, if the requirement requires user input.
- doUpdate([$properties]) - returns an integer describing the new status of the requirement after updating. If the requirement needed user input, an array of WizardProperty objects is passed.
- STARTUP_STATUS_OK - everything is A-OK.
- STARTUP_STATUS_ERROR - this means that an error occured that can't be fixed. This should, hopefully, never happen.
- STARTUP_STATUS_NEEDS_UPDATE - the requirement needs to run an autonomous update to be satisfied.
- STARTUP_STATUS_NEEDS_USER_INPUT - the requirement needs user input before an update can be performed. In this case, StartupCheck will call the createWizard() method on the requirement.
- STARTUP_STATUS_NOT_CHECKED - used internally by the StartupCheck class - specifies that the status of the requirement hasn't even been checked.
- STARTUP_STATUS_NEEDS_INSTALL - the requirement needs to run an autonomous install to be satisfied. This is essentially synonymous with STARTUP_STATUS_NEEDS_UPDATE.