Version: 0.1
Type: Class
Category: Databases
License: GNU General Public License
Description: Inspired by Perl DBI's built in support to read MySQL options from the standard ~/.my.cnf, I thought I would be nice to have the same support in PHP. That's what this class does. It reads options from any specified group in ~/.my.cnf and returns an associative array. Read the top of the file for more info.
<?php
/*
FILENAME: mycnfOptions.class.php
AUTHOR: Benjamin D. Jones (benjones@superutility.net)
COPYRIGHT: Copyright (C) 2001 Benjamin D. Jones
PURPOSE: Read group options from ~/.my.cnf and return the values for use in a script
VERSION: 0.1 (22.Mar.2001)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
NOTE:
=====
Why is this an object? Are you a moron?
No. Two reasons why I took an object oriented approach.
1.) to eliminate namespace conflicts (you never know)
2.) because I find that code is easier to manage this way.
You're welcome to retool it as a single function if you like.
USAGE:
======
$mycnf = new mycnfOptions([$group], [$home]);
$array_of_options = $mycnf -> readMycnf();
DOCUMENTATION:
==============
This class reads MySQL options from the standard ~/.my.cnf file that
other MySQL programs read. You specify the group to be read when
instantiating the object. The group name can be anything you want, as
long as that section actually exists in ~/.my.cnf, then those options
will be read, and placed in an associative array.
Read more about the my.cnf file in the MySQL manual.
This is guaranteed not to work with the Win32 version of PHP and
MySQL. I don't use Windows so I don't develop for Windows.
A quick example . . .
<?php
require_once('mycnfOptions.class.hpp');
$mycnf = new mycnfOptions('phpscripts', '/home/username/');
$dbInfo = $mycnf -> readMycnf();
?>
If ~/.my.cnf contained the following group entries . . .
# ~/.my.cnf
[phpscripts]
host = localhost
user = username
password = secret
database = myDB
# end ~/.my.cnf
. . . then $dbInfo would have the indexes 'host', 'user',
'password', and 'database' with the corresponding values.
The first constructor argument is optional and defaults to
'php'. You can use any group name you want as long as you acutally
have entries for it in your ~/.my.cnf
The second constuctor argument is optional, if omitted, the
enviromental variable $HOME is used. This probably won't be set
unless you are running your script from the command line or have set
it previously with putenv().
By using this class, you can eliminate the need for various config
files, etc. for your different scripts. You can keep all you MySQL
options in the file in which they were meant to be kept.
*/
class mycnfOptions
{
var $groupName;
var $mycnfFile;
var $options;
function mycnfOptions($groupName = 'client', $homeDir = '')
{
$this -> groupName = $groupName;
$this -> mycnfFile = $this -> findMycnf ($homeDir);
$this -> options = array();
}
function findMycnf($homeDir = '')
{
// PURPOSE: set up the location of the ~/.my.cnf file
// if $homeDir is empty (it's optional), use the enviroment
if ( empty($homeDir) ) {
$homeDir = getenv('HOME');
}
// strip the trailing '/' if there is one
$homedir = preg_replace('@/$@', '', $homeDir);
return "$homeDir/.my.cnf";
}
function readMycnf()
{
// PURPOSE: Read the ~/.my.cnf file
// Return the options for the appropriate group
// as an associative array
if ( $this -> fh = fopen($this -> mycnfFile, 'r') ) {
// find the correct group
$this -> findFilePosition();
// extract the options for the appropriate group
$this -> getOptions();
fclose($this -> fh);
}
return $this -> options;
}
function getOptions()
{
// PURPOSE: extract the options from the file for this group
while ( $line = fgets($this -> fh, 100) ) {
$line = trim($line);
if ( preg_match('/^([a-z_A-Z]*) *= *(.*)$/', $line, $match) ) {
// an option is listed so create a new key/value set
// in the options array
$this -> options[$match[1]] = $match[2];
} else if ( preg_match('/^\[/', $line) ) {
// the next group has started so exit the loop
break;
}
}
return true;
}
function findFilePosition()
{
// PURPOSE: Find the desired group section in the file
while ( $line = fgets($this -> fh, 100) ) {
if ( preg_match('/^\[' . $this -> groupName . '\]$/', $line) ) {
// found the correct position
return true;
}
}
return false;
}
}
?>