My Ini Design: Introduction: Difference between revisions
(New page: {{Nav My Ini Design}} PHP has a number of functions for manipulating the php.ini file great I thought there must be similar functions for my.ini files. If there is I could not find them h...) |
m (Reverted edits by Upazixorys (Talk); changed back to last version by Ric) |
(One intermediate revision by one other user not shown) | |
(No difference)
|
Latest revision as of 08:19, 24 November 2010
My Ini Design: Introduction | Ini Set | Ini Get | Final Functions | parse_ini_file
|
Uniform Server Cron Configuration |
PHP has a number of functions for manipulating the php.ini file great I thought there must be similar functions for my.ini files.
If there is I could not find them hence decided to write two functions loosely based on ini_set and ini_get another function I hoped would be of use is parse_ini_file however this strips all comments.
This tutorial again is practical it covers design steps with example code that can be run on Uniform Server 5-Nano.
Problems
Above two functions ini_set and ini_get target unique option names in the php.ini file, however my options are unique to each block hence the same option name can appear in any block.
The two functions resolve this issue.
Functions
Before looking at design details here are the two functions:
my_ini_set
Description: Sets the value of the given configuration option within the specified block.
my_ini_set ( string $file, string $block, string $optionname , string $newvalue )
my_ini_get
Description: Returns the value of the configuration option within the specified block on success.
my_ini_get (string $file, string $block, string $optionname )
Starting point
There are two basic requirements an easy way to read a file into an array and to save that modified array back to the file.
PHP has two really neat functions that perform this task with ease:
$array=file($file) | This reads every line from named file $file and saves each line as an element in the array $array |
file_put_contents($file,$array) | This writes the entire contents of array $array into file $file. |
The above hides detail such as open files for write, write data and close file or open file for read, read data into array and close file. Top
Examples Initial Set-up
All examples are CLI scripts I like to run them from a batch file this makes testing easier. I have assumed scripts we will be turned into a pugin hence folder UniServer\plugins is a convenient place to locate test files.
Create a new folder named test and the following files with content as shown below
UniServer\plugins\test\Run_test.bat COLOR B0 @echo off cls ..\..\usr\local\php\php.exe test.php pause UniServer\plugins\test\test.php <?php $ini_array = file("cron.ini"); file_put_contents("out_cron.txt",$ini_array); ?> UniServer\plugins\test\cron.ini ; Test example file for cron timers ; Period values hourly, daily or weekly [moodle] path = http://localhost/moodle/admin/cron.php period = daily ref = [drupal] path = http://localhost/drupal/cron.php period = daily ref = [dtdns] path = ..\..\plugins\dtdns_updater\dtdns_updater.php period = hourly ref = |
|
Run_test.bat: Sole purpose of this batch file is to run our test script test.php It display any printed results and pauses allowing output to be read. Press any key to closes this window.
This file is the script we want to develop. It currently contains two lines the first reads cron.ini into an array named $ini_array. The second line in a final script would write the array back to cron.ini however for testing it’s clearer to write the array to a second file out_cron.txt
This is the configuration file we wish to manipulate. It can be named anything you like and have a different file extension if you wish. It currently contains comments three bocks [moodle],[drupal] and [dtdns] Each block contains identical configuration options path, period and ref. The first two (path and period) are user configurable. The last option (ref) is dynamically configured during script run time purpose is for tracking current status. Additional blocks added must have a unique name and options set as follows:
|
Testing
Check the above files are correctly set-up by running Run_test.bat as follows
- Double click on Run_test.bat
- Command window displays: Press any key to continue . . .
- A new file out_cron.txt is created
- Check the content of out_cron.txt and cron.ini are identical
Step four confirms correct operation.
Display Array Content
To display an array's content we could use function print_r() for example print_r($ini_array) would display content of $ini_array.
However to manipulate the content of an array we need access to its keys and values. Further we need to scan every element (line) of the array. A foreach loop performs this task giving access to both keys and values.
Replacing the Do something with a print statement we can display array's content. Example on the right prints the variables $key and $value Note: A key points to a location of an element (value) within an array. |
|
Scan Array: foreach($ini_array as $key => $value){ Do something } Scan Array and print: foreach($ini_array as $key => $value){ print "Key = $key Value = $value"; } |
Test Code
Edit Script Edit test file UniServer\plugins\test\test.php Add lines as shown on the right the script now performs the following:
|
<?php $ini_array = file("cron.ini"); foreach($ini_array as $key => $value){ print "Key = $key Value = $value"; } file_put_contents("out_cron.txt",$ini_array); ?> |
Run Script Double click on Run_test.bat Result as shown on right
The line: print "Key = $key Value = $value"; does not require a new line character "\n" each line read from a file is intact new line characters are not stripped. Hence each value (line) in the array contains a new line character.
|
Key = 0 Value = ; Test example file for cron timers Key = 1 Value = ; Period values hourly, daily or weekly Key = 2 Value = Key = 3 Value = [moodle] Key = 4 Value = path = http://localhost/moodle/admin/cron.php Key = 5 Value = period = daily Key = 6 Value = ref = Key = 7 Value = Key = 8 Value = [drupal] Key = 9 Value = path = http://localhost/drupal/cron.php Key = 10 Value = period = daily Key = 11 Value = ref = Key = 12 Value = Key = 13 Value = [dtdns] Key = 14 Value = path = ..\..\plugins\dtdns_updater\dtdns_updater.php Key = 15 Value = period = hourly Key = 16 Value = ref = Press any key to continue . . . |
Summary
That completes our test set-up. Although the test script contains only few lines of code it is a very powerful structure,
The next page covers my_ini_set function.