games Open Menu Banner Image PHP Setup Script

php deploying setup custom scripts small projects web form configuration constants post install instructions

Scrapbook / PHP Setup Script
2009/11/22 22:11:43

To help deploy several mid-sized projects I've developed a previously half-baked script into a useful, fully resuable, PHP class for writing setup variables from a webform and executing instructions. Its best explained by the code snippets seen below-

Sample setup script:


<?php include('setup.class.php');/* Variables for the application */$setup = new SetupScript('constants.php''pe5ches4''Setup and configuration');// Note: If a value is required, set the default value to false to force validation on the variable// Note: $setup->AddVariable('name', max_length, default_value, 'helpful_description', 'helpful_example');// Note: $setup->AddInstruction('MethodName', 'Description of function');$setup->AddVariable('MYSQL_SERVER'0falsefalse'MySQL server''localhost:/tmp/mysql5.sock');$setup->AddVariable('MYSQL_DATABASE'0falsefalse'- Database''database');$setup->AddVariable('MYSQL_USERNAME'0falsefalse'- User''user');$setup->AddVariable('MYSQL_PASSWORD'0truefalse' - Password''password');$setup->AddLinebreak();$setup->AddVariable('COMMENTS_TABLE'0falsefalse' Table name to store entries''oc_comments');$setup->AddVariable('SERVICE_DIRECTORY'0falsefalse' URL of OpenComments Service''//');$setup->AddLinebreak();$setup->AddVariable('BBCODE_SITE_ROOT'0falsefalse' Default for bbcode links''//');$setup->AddLinebreak();$setup->AddVariable('TRUSTED_USER_1'0falsefalse'''//');$setup->AddVariable('TRUSTED_USER_2'0falsefalse'''//localhost/openid/Markavian');/* End variables *//* Post setup instructions */$setup->AddInstruction('CreateTables''Create tables in the database needed for the application'true);$setup->AddInstruction('AnnounceSuccess''Annouce successful installation');/* End instructions */function CreateTables(){    echo '<h2>SQL Tables</h2>';    echo '<p>Creating tables...</p>';        require_once('constants.php');    require_once('../config/sql_class.php');    $sql = new sql($MYSQL_SERVER$MYSQL_DATABASE$MYSQL_USERNAME$MYSQL_PASSWORD);    $instructions = array();        $instructions[] = sprintf("CREATE TABLE IF NOT EXISTS `%s` (  `comment_id` int(11) NOT NULL auto_increment,  `url` varchar(255) NOT NULL,  `comment` text NOT NULL,  `openid` tinytext NOT NULL,  `username` varchar(32) NOT NULL,  `website` varchar(128) NOT NULL,  `postdate` datetime NOT NULL,  `comment_type` enum('comment','feedback','todo','bug','suggestion') NOT NULL,  PRIMARY KEY  (`comment_id`,`url`)) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;    "$COMMENTS_TABLE);        foreach($instructions as $name=>$query)    {        $sql->query($query$name);    }        echo '<p>Sparing error messages listed above; tables created.</p>';}function AnnounceSuccess(){    echo "<h2>Success</h2>";    echo "<p>Installation was a success.</p>";}/* Create, process, and display page */$setup->DisplayPage();?>

This produces a HTML form like so:

The above file would be written by the project developer responsible for packaging and deploying a project. They would usually identify common variables likely to change on deployment and define them in a file, say constants.php ("Constant" in the context of unchanging through the application); using the format above these variables could instead be defined in a setup file with appropriate descriptions. In turn, a html based setup form can be created and basic validation can be applied, allowing constants.php to be created on the fly through a web interface. This eases and speeds up deployment and provides documentation for the next developer to work from.

From the web interface, when the form is submitted, constants.php is created and the selected instructions are executed:

Sample output file:


<?php // WARNING: This is a dynamically created file; edit only as a last resort// Generated by SetupScript class on Monday 23rd of November 2009 01:08:51 AM // - Local path:  C:/Documents and Settings/John/My Documents/Websites/opencomments/trunk/services/setup.php// - Remote path: //localhost/opencomments/services/setup.php// Dependencies: setup.class.php// Title:                Setup and configuration// Description:    This installation script will create the file constants.php.// Configuration variables:$MYSQL_SERVER "localhost:/tmp/mysql5.sock"// MySQL server$MYSQL_DATABASE "db"// - Database$MYSQL_USERNAME "un"// - User$MYSQL_PASSWORD "pw"//  - Password$COMMENTS_TABLE "oc_comments"//  Table name to store entries$SERVICE_DIRECTORY "//localhost/opencomments/services/"//  URL of OpenComments Service$BBCODE_SITE_ROOT "//"//  Default for bbcode links$TRUSTED_USER_1 "//";$TRUSTED_USER_2 "//localhost/openid/Markavian";// Additionally, the following instructions were executed: // 1.    Create tables in the database needed for the application (optional)// 2.    Annouce successful installation?>

Once successful the output of the setup script then contains information about how the file was generated, what each of the variables corresponds to, and information about any instructions executed afterwards- doubling up as a usable part of an application and a log file.

If the rest of the application is written correctly it can detect the presence of the configuration file, or a variable inside, and react / redirect appropriately to prompt the user for action.

Feel free to download and try the script out; setup.class.php is self contained and the sample above is included in the top of its comments.


[zip] SetupScript v1.02