My Ini Design: Ini Get

From The Uniform Server Wiki
Jump to navigation Jump to search

MPG UniCenter

MPG UniCenter

Uniform Server Cron Configuration

This page looks at proof of concept for a my_ini_get function.

Most of the groundwork has been completed on the previous page.

Requirement

Basic requirement is to target a specific configuration block and option, once targeted return that option's value.

For Example:

Suppose we want to target block [drupal] and return the value for the option period.

  • All blocks are uniquely named and placed in square brackets.
    • The only issue with detecting this block is one of spaces. Easily resolved using the trim() function


  • All options within a block are uniquely named.
    • Again an issue with spaces resolved using the trim() function
    • If the option does not exist! A potential issue of retrieving a value from block.


  • Reading a value is relatively easy; just extract the value from array, return it and exit.
    • The line contains addition data that requires removing such as the option, equals and newline character.

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 = 

Hey! I have just read something similar on the previous page. Yep! That’s the whole point the two function are symmetrical. Instead of changing the array value we read it.

Top

Example 4

Modify Test Script as shown on right:

It's a small change, we print the value contained in the array:

if($flag &&  preg_match('/^\s*period/',$value)){  
  print "####".$ini_array[$key]."####"; 
  break;                             
}
  • Run the batch file (Double click on Run_test.bat)
  • Note the output displayed shown below.
####period = daily
####Press any key to continue . . .

What is displayed is raw data contained in the array. It clearly shows the need to
remove “period = “ and the new line character to give daily.
The #’s are just for reference and will be removed.

UniServer\plugins\test\test.php

<?php
 $ini_array = file("cron.ini");

 $flag=false;
 foreach($ini_array as $key => $value){

   if($flag &&  preg_match('/^\s*\[/',$value)){ 
     $flag=false;                                      
   }

   if(trim($value) == "[drupal]"){             
     $flag=true;                             
   }

   if($flag &&  preg_match('/^\s*period/',$value)){  
     print "####".$ini_array[$key]."####"; 
     break;                             
   }                                                   
 }

 file_put_contents("out_cron.txt",$ini_array);
?>

Top

Clean Array Output

You can use several methods to clean the array output it just depends what you are comfortable with.

If you are cleaver and want to make the code hard to read do it in a single regx.

Alternatively break it down and do it in easy stages.

  • One quick clean up is to trim the array output and save it to a variable.
  • Change the test script section shown on right and run.
  • Output looks like:
####period = daily####
Press any key to continue . . .

 

Part of UniServer\plugins\test\test.php

if($flag &&  preg_match('/^\s*period/',$value)){
 $str =  trim($ini_array[$key]); 
 print "####".$str."####\n"; 
 break;                             
} 

The reference markers # show spaces and new line have been removed. This makes a regex easy to use. OK these things can be as difficult as you like to make them I just like simple.

What we want to eliminate is "period = " the "period" and "=" are well defined however what we do not know is how many spaces a user used.

Well scanning from left to right create a regex pattern:

^ - Indicates start at the beginning of a line
period - That's the word we are looking for hence use as is
\s* - \s is a special code meaning a space, the * is another special code meaning 0 or more of the previous character. hence chects for any number of space.
= - A well defined character we are looking for.
\s* - As above.

That completes what we are looking for (pattern). All that remains is to replace it with nothing "" (replacement).

  • Search replace function preg_replace($pattern, $replacement, $string);
  • Change the test script section shown on right and run.
  • Output looks like:
####daily####
Press any key to continue . . .

 

Part of UniServer\plugins\test\test.php

   if($flag &&  preg_match('/^\s*period/',$value)){
     $str  =  trim($ini_array[$key]); 
     $str1 =  preg_replace('/^period\s*=\s*/', "", $str); 
     print "####".$str1."####\n"; 
     break;                             
   } 

Top

Code

Final test code is shown on the right:

  • File cron.ini is loaded into array $ini_array
  • A tracker $flag marks a specific block found
  • Every line of this array is scanned using a foreach
  • First if checks for a new block,if found resets flag
  • Second if tests for a specific block, sets flag if found
  • Last if tests for a specific option. If found
    • Value is read from array and trimmed of white space
    • The option,spaces and = are remove using preg_replace
  • The print is there for testing and can be removed


UniServer\plugins\test\test.php

<?php
 $ini_array = file("cron.ini");

 $flag=false;
 foreach($ini_array as $key => $value){

   if($flag &&  preg_match('/^\s*\[/',$value)){ 
     $flag=false;                                      
   }

   if(trim($value) == "[drupal]"){             
     $flag=true;                             
   }

   if($flag &&  preg_match('/^\s*period/',$value)){
     $str  =  trim($ini_array[$key]); 
     $str1 =  preg_replace('/^period\s*=\s*/', "", $str); 
     print "####".$str1."####\n"; 
     break;                             
   }                                                   
 }

 file_put_contents("out_cron.txt",$ini_array);
?>

Top

Summary

Above completes our test code for the second function.

The two snippets of code are proof of concept on the next page these are converted into functions.

Top