cvs: pear /Perm_LiveUser/Perm DefineGenerator
| From: | Arnaud Limbourg | Date: | Mon, 17 Feb 2003 21:33:07 +0000 |
| Subject: | cvs: pear /Perm_LiveUser/Perm DefineGenerator | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-8922@lists.php.net to get a copy of this message | ||
arnaud Mon Feb 17 16:33:07 2003 EDT
Modified files:
/pear/Perm_LiveUser/Perm DefineGenerator
Log:
* rewrite using Console_Getopt
* now accepting both short and long options
* the generated file now contains php tags (useful to include right away)
* udpate help to reflect changes
* forgot a _ when using a prefix
* freeing of DB result before calling numRows() caused an error
* output during execution a bit nicer
Index: pear/Perm_LiveUser/Perm/DefineGenerator diff -u pear/Perm_LiveUser/Perm/DefineGenerator:1.4 pear/Perm_LiveUser/Perm/DefineGenerator:1.5 --- pear/Perm_LiveUser/Perm/DefineGenerator:1.4 Mon Feb 17 14:21:50 2003 +++ pear/Perm_LiveUser/Perm/DefineGenerator Mon Feb 17 16:33:07 2003 @@ -4,179 +4,167 @@ * DefineGenerator.php * * Description: - * So you´ve decided to use LiveUser in your application - good for you :-) - * But - it´s a pain to manually make all the constant definitions for authareas + * So you've decided to use LiveUser in your application - good for you :-) + * But - it's a pain to manually make all the constant definitions for authareas * and authrights that your application needs. That´s where this script comes * in handy. Currently, you can only call it from the commandline. * Once the GUI is ready, you´ll be able to generate define() files from there. * * Syntax: * DefineGenerator [options] - * + * ...where [options] can be: - * -help : Shows this list of options - * -dsn : Defines the PEAR::DB DSN to connect to the database. - * Example: -dsn=mysql://user:passwd@hostname/databasename - * -app : Defines the name of the application for which to generate the constant definitions. - * Example: -app=MyApplication - * -area : Defines the AuthArea to be used. If not set, this script will generate constants - * for all AuthAreas within the chosen application. - * Example: -area=MyArea - * -file : Defines the output file. - * Example: -file=/path/to/output/file - * -prefix : Prefix to use for naming the constants. - * Example: -prefix=LIVEUSER (prefixes all constant names with - * LIVEUSER_, i.e. LIVEUSER_ADMINAREA_ADMINRIGHT) - * Defaults to none. - * -naming : Defines the way how the constants are being named. Can be a - * number between 1 and 3. Examples for naming schemes: - * -naming=1 : PREFIX_RIGHTNAME - * -naming=2 : PREFIX_AREANAME_RIGHTNAME - * -naming=3 : PREFIX_APPLICATIONNAME_AREANAME_RIGHTNAME - * Defaults to 1. + * -h --help : Shows this list of options * - * @author Markus Wolff <wolff@21st.de> - * @version $Id: DefineGenerator,v 1.4 2003/02/17 19:21:50 arnaud Exp $ - */ - - - -/** - * printHelp() + * -d --dsn (required) : Defines the PEAR::DB DSN to connect to the database. + * Example: --dsn=mysql://user:passwd@hostname/databasename * - * @return void - * @desc Prints out a list of commandline options + * -o --output (required) : Defines the output file. Example: -file=/path/to/output/file + * + * -q --app (optional) : Defines the name of the application for which to generate the + * constant definitions. Example: --app=MyApplication + * + * -a --area (optional) : Defines the AuthArea to be used. If not set, this script will + * generate constants for all AuthAreas within the chosen + * application. Example: --area=MyArea + * + * -p --prefix (optional) : Prefix to use for naming the constants. + * Example: --prefix=LIVEUSER (prefixes all constant names with + * LIVEUSER_, i.e. LIVEUSER_ADMINAREA_ADMINRIGHT) + * Defaults to none. + * + * -n --naming (optional) : Defines the way how the constants are being named. Can be a + * number between 1 and 3. Examples for naming schemes: + * --naming=1 : PREFIX_RIGHTNAME + * --naming=2 : PREFIX_AREANAME_RIGHTNAME + * --naming=3 : PREFIX_APPLICATIONNAME_AREANAME_RIGHTNAME + * Defaults to 1. + * + * @author Markus Wolff <wolff@21st.de> + * @author Arnaud Limbourg <arnaud@limbourg.com> + * @version $Id: DefineGenerator,v 1.5 2003/02/17 21:33:07 arnaud Exp $ */ -function printHelp() -{ - echo (' -Syntax: -DefineGenerator [options] - -...where [options] can be: --help : Shows this list of options +require_once 'Console/Getopt.php'; --dsn : Defines the PEAR::DB DSN to connect to the database. - Example: -dsn=mysql://user:passwd@hostname/databasename +$argv = Console_Getopt::readPHPArgv(); --app : Defines the name of the application for which to generate the - constant definitions. Example: -app=MyApplication +$shortoptions = "h?d:o:a::q::p::n::"; --area : Defines the AuthArea to be used. If not set, this script will - generate constants for all AuthAreas within the chosen - application. Example: -area=MyArea - --file : Defines the output file. Example: -file=/path/to/output/file +$longoptions = array('output=', 'dsn=', 'app==', + 'help', 'prefix==', 'naming==', 'area=='); --prefix : Prefix to use for naming the constants. - Example: -prefix=LIVEUSER (prefixes all constant names with - LIVEUSER_, i.e. LIVEUSER_ADMINAREA_ADMINRIGHT) - Defaults to none. +// Initialize variables +$naming = '1'; +$area = $output = $dsn = $application = $prefix = ''; --naming : Defines the way how the constants are being named. Can be a - number between 1 and 3. Examples for naming schemes: - -naming=1 : PREFIX_RIGHTNAME - -naming=2 : PREFIX_AREANAME_RIGHTNAME - -naming=3 : PREFIX_APPLICATIONNAME_AREANAME_RIGHTNAME - Defaults to 1. +$con = new Console_Getopt; +$args = $con->readPHPArgv(); +array_shift($args); +$options = $con->getopt($args, $shortoptions, $longoptions); -'); +if (PEAR::isError($options)) { + printHelp($options); } +$options = $options[0]; -/** - * parseArgs() - * - * @return array - * @desc Removes "-" chars from CLI arguments and returns an associative array with the lowercased argument names as key. - */ -function parseArgs() -{ - global $argv, $argc; - - $args = array(); - $ret = null; - - for ($i = 1; $i < $argc; $i++) { - $argStruct = array(); - $argStruct = explode('=', $argv[$i]); - $name = strtolower(str_replace('-', '', $argStruct[0])); - if ($name == 'help') { - $args[$name] = ''; - } else { - $args[$name] = $argStruct[1]; - } - - } - - if (!isset($args['naming'])) { - $args['naming'] = 1; +foreach ($options as $opt) { + switch ($opt[0]) { + case 'd': + case '--dsn': + $dsn = $opt[1]; + break; + case 'n': + case '--naming': + $naming = $opt[1]; + break; + case 'p': + case '--prefix': + $prefix = strtoupper($opt[1]); + break; + case 'a': + case '--area': + $area = $opt[1]; + break; + case 'q': + case '--app': + $application = $opt[1]; + break; + case 'o': + case '--output': + $output = $opt[1]; + break; + case 'h': + case '--help': + printHelp(); + break; } - - return $args; } - -// Import parameters -$args = parseArgs(); - /****************************************************************** Begin sanity checks on arguments ******************************************************************/ -if (isset($args['help'])) { - die(printHelp()); -} - -if (!isset($args['dsn']) || !isset($args['app']) || !isset($args['file'])) { - die ("DefineGenerator: Mandatory argument is missing!\n\n".printHelp()); -} - -if (isset($args['prefix'])) { - $prefix = strtoupper($args['prefix']).'_'; -} else { - $prefix = ''; +if ($dsn == '' && $output == '') { + printHelp(); } /****************************************************************** End sanity checks on arguments ******************************************************************/ // Import PEAR::DB -require_once('DB.php'); +require_once 'DB.php'; // Connect to database -$db = DB::connect($args['dsn']); +$db = DB::connect($dsn); + if (DB::isError($db)) { - die("DefineGenerator: Database connection failed.\n".$db->toString()); + die("DefineGenerator: Database connection failed.\n" . $db->toString() . "\n"); } -echo "Successfully connected to database.\nFetching rights..."; +echo "Successfully connected to database.\nFetching rights... "; $sql = "SELECT * FROM liveuser_rights R, liveuser_areas A WHERE R.area_id=A.area_id"; -if (isset($args['area'])) { - $sql .= " AND A.area_define_name='$args[area]'"; +if ($area != '') { + $sql .= " AND A.area_define_name='$area'"; } $res = $db->query($sql); if (DB::isError($res)) { - die("DefineGenerator: Database query failed.\n".$res->toString()); + die("DefineGenerator: Database query failed.\n" . $res->toString() . "\n"); } -$strDef = ''; +$strDef = "<?php\n"; +$foundrights = ''; -while($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) { - switch($args['naming']) { +while($res->fetchInto($row, DB_FETCHMODE_ASSOC)) { + switch($naming) { case 2: - $strDef .= sprintf("define('%s_%s', %s);\n", - $prefix.strtoupper($row['area_define_name']), + $strDef .= sprintf("define('%s_%s_%s', %s);\n", + $prefix, + strtoupper($row['area_define_name']), + strtoupper($row['right_define_name']), + $row['right_id'] + ); + $foundrights .= sprintf("define('%s_%s_%s', %s);\n", + $prefix, + strtoupper($row['area_define_name']), strtoupper($row['right_define_name']), $row['right_id'] ); break; case 3: - $strDef .= sprintf("define('%s_%s_%s', %s);\n", - $prefix.strtoupper($args['app']), + $strDef .= sprintf("define('%s_%s_%s_%s', %s);\n", + $prefix, + strtoupper($application), + strtoupper($row['area_define_name']), + strtoupper($row['right_define_name']), + $row['right_id'] + ); + $foundrights .= sprintf("define('%s_%s_%s_%s', %s);\n", + $prefix, + strtoupper($application), strtoupper($row['area_define_name']), strtoupper($row['right_define_name']), $row['right_id'] @@ -187,17 +175,22 @@ $prefix.strtoupper($row['right_define_name']), $row['right_id'] ); + $foundrights .= sprintf("define('%s', %s);\n", + $prefix.strtoupper($row['right_define_name']), + $row['right_id'] + ); } } +$strDef .= '?>'; + +printf("done (%s rights found).\n\n", $res->numRows()); $res->free(); $db->disconnect(); -printf("done (%s rights found).\n", $res->numRows()); - -echo ("Result:\n\n$strDef"); +echo ("Result:\n\n$foundrights"); /***** Write result to file *****/ @@ -205,10 +198,52 @@ echo ("\nWriting file..."); -$fp = @fopen($args['file'], 'w') or die("Could not write to file $args[file]"); +$fp = @fopen($output, 'w') or die("Could not write to file $output"); fputs($fp, $strDef); fclose($fp); echo ("done!\n"); +/** + * printHelp() + * + * @return void + * @desc Prints out a list of commandline options + */ +function printHelp() +{ + echo (' +Syntax: +DefineGenerator [options] + +...where [options] can be: +-h --help : Shows this list of options + +-d --dsn (required) : Defines the PEAR::DB DSN to connect to the database. + Example: --dsn=mysql://user:passwd@hostname/databasename + +-o --output (required) : Defines the output file. Example: -file=/path/to/output/file + +-q --app (optional) : Defines the name of the application for which to generate the + constant definitions. Example: --app=MyApplication + +-a --area (optional) : Defines the AuthArea to be used. If not set, this script will + generate constants for all AuthAreas within the chosen + application. Example: --area=MyArea + +-p --prefix (optional) : Prefix to use for naming the constants. + Example: --prefix=LIVEUSER (prefixes all constant names with + LIVEUSER_, i.e. LIVEUSER_ADMINAREA_ADMINRIGHT) + Defaults to none. + +-n --naming (optional) : Defines the way how the constants are being named. Can be a + number between 1 and 3. Examples for naming schemes: + --naming=1 : PREFIX_RIGHTNAME + --naming=2 : PREFIX_AREANAME_RIGHTNAME + --naming=3 : PREFIX_APPLICATIONNAME_AREANAME_RIGHTNAME + Defaults to 1. + +'); +exit; +} ?>
Index: pear/Perm_LiveUser/Perm/DefineGenerator diff -u pear/Perm_LiveUser/Perm/DefineGenerator:1.4 pear/Perm_LiveUser/Perm/DefineGenerator:1.5 --- pear/Perm_LiveUser/Perm/DefineGenerator:1.4 Mon Feb 17 14:21:50 2003 +++ pear/Perm_LiveUser/Perm/DefineGenerator Mon Feb 17 16:33:07 2003 @@ -4,179 +4,167 @@ * DefineGenerator.php * * Description: - * So you´ve decided to use LiveUser in your application - good for you :-) - * But - it´s a pain to manually make all the constant definitions for authareas + * So you've decided to use LiveUser in your application - good for you :-) + * But - it's a pain to manually make all the constant definitions for authareas * and authrights that your application needs. That´s where this script comes * in handy. Currently, you can only call it from the commandline. * Once the GUI is ready, you´ll be able to generate define() files from there. * * Syntax: * DefineGenerator [options] - * + * ...where [options] can be: - * -help : Shows this list of options - * -dsn : Defines the PEAR::DB DSN to connect to the database. - * Example: -dsn=mysql://user:passwd@hostname/databasename - * -app : Defines the name of the application for which to generate the constant definitions. - * Example: -app=MyApplication - * -area : Defines the AuthArea to be used. If not set, this script will generate constants - * for all AuthAreas within the chosen application. - * Example: -area=MyArea - * -file : Defines the output file. - * Example: -file=/path/to/output/file - * -prefix : Prefix to use for naming the constants. - * Example: -prefix=LIVEUSER (prefixes all constant names with - * LIVEUSER_, i.e. LIVEUSER_ADMINAREA_ADMINRIGHT) - * Defaults to none. - * -naming : Defines the way how the constants are being named. Can be a - * number between 1 and 3. Examples for naming schemes: - * -naming=1 : PREFIX_RIGHTNAME - * -naming=2 : PREFIX_AREANAME_RIGHTNAME - * -naming=3 : PREFIX_APPLICATIONNAME_AREANAME_RIGHTNAME - * Defaults to 1. + * -h --help : Shows this list of options * - * @author Markus Wolff <wolff@21st.de> - * @version $Id: DefineGenerator,v 1.4 2003/02/17 19:21:50 arnaud Exp $ - */ - - - -/** - * printHelp() + * -d --dsn (required) : Defines the PEAR::DB DSN to connect to the database. + * Example: --dsn=mysql://user:passwd@hostname/databasename * - * @return void - * @desc Prints out a list of commandline options + * -o --output (required) : Defines the output file. Example: -file=/path/to/output/file + * + * -q --app (optional) : Defines the name of the application for which to generate the + * constant definitions. Example: --app=MyApplication + * + * -a --area (optional) : Defines the AuthArea to be used. If not set, this script will + * generate constants for all AuthAreas within the chosen + * application. Example: --area=MyArea + * + * -p --prefix (optional) : Prefix to use for naming the constants. + * Example: --prefix=LIVEUSER (prefixes all constant names with + * LIVEUSER_, i.e. LIVEUSER_ADMINAREA_ADMINRIGHT) + * Defaults to none. + * + * -n --naming (optional) : Defines the way how the constants are being named. Can be a + * number between 1 and 3. Examples for naming schemes: + * --naming=1 : PREFIX_RIGHTNAME + * --naming=2 : PREFIX_AREANAME_RIGHTNAME + * --naming=3 : PREFIX_APPLICATIONNAME_AREANAME_RIGHTNAME + * Defaults to 1. + * + * @author Markus Wolff <wolff@21st.de> + * @author Arnaud Limbourg <arnaud@limbourg.com> + * @version $Id: DefineGenerator,v 1.5 2003/02/17 21:33:07 arnaud Exp $ */ -function printHelp() -{ - echo (' -Syntax: -DefineGenerator [options] - -...where [options] can be: --help : Shows this list of options +require_once 'Console/Getopt.php'; --dsn : Defines the PEAR::DB DSN to connect to the database. - Example: -dsn=mysql://user:passwd@hostname/databasename +$argv = Console_Getopt::readPHPArgv(); --app : Defines the name of the application for which to generate the - constant definitions. Example: -app=MyApplication +$shortoptions = "h?d:o:a::q::p::n::"; --area : Defines the AuthArea to be used. If not set, this script will - generate constants for all AuthAreas within the chosen - application. Example: -area=MyArea - --file : Defines the output file. Example: -file=/path/to/output/file +$longoptions = array('output=', 'dsn=', 'app==', + 'help', 'prefix==', 'naming==', 'area=='); --prefix : Prefix to use for naming the constants. - Example: -prefix=LIVEUSER (prefixes all constant names with - LIVEUSER_, i.e. LIVEUSER_ADMINAREA_ADMINRIGHT) - Defaults to none. +// Initialize variables +$naming = '1'; +$area = $output = $dsn = $application = $prefix = ''; --naming : Defines the way how the constants are being named. Can be a - number between 1 and 3. Examples for naming schemes: - -naming=1 : PREFIX_RIGHTNAME - -naming=2 : PREFIX_AREANAME_RIGHTNAME - -naming=3 : PREFIX_APPLICATIONNAME_AREANAME_RIGHTNAME - Defaults to 1. +$con = new Console_Getopt; +$args = $con->readPHPArgv(); +array_shift($args); +$options = $con->getopt($args, $shortoptions, $longoptions); -'); +if (PEAR::isError($options)) { + printHelp($options); } +$options = $options[0]; -/** - * parseArgs() - * - * @return array - * @desc Removes "-" chars from CLI arguments and returns an associative array with the lowercased argument names as key. - */ -function parseArgs() -{ - global $argv, $argc; - - $args = array(); - $ret = null; - - for ($i = 1; $i < $argc; $i++) { - $argStruct = array(); - $argStruct = explode('=', $argv[$i]); - $name = strtolower(str_replace('-', '', $argStruct[0])); - if ($name == 'help') { - $args[$name] = ''; - } else { - $args[$name] = $argStruct[1]; - } - - } - - if (!isset($args['naming'])) { - $args['naming'] = 1; +foreach ($options as $opt) { + switch ($opt[0]) { + case 'd': + case '--dsn': + $dsn = $opt[1]; + break; + case 'n': + case '--naming': + $naming = $opt[1]; + break; + case 'p': + case '--prefix': + $prefix = strtoupper($opt[1]); + break; + case 'a': + case '--area': + $area = $opt[1]; + break; + case 'q': + case '--app': + $application = $opt[1]; + break; + case 'o': + case '--output': + $output = $opt[1]; + break; + case 'h': + case '--help': + printHelp(); + break; } - - return $args; } - -// Import parameters -$args = parseArgs(); - /****************************************************************** Begin sanity checks on arguments ******************************************************************/ -if (isset($args['help'])) { - die(printHelp()); -} - -if (!isset($args['dsn']) || !isset($args['app']) || !isset($args['file'])) { - die ("DefineGenerator: Mandatory argument is missing!\n\n".printHelp()); -} - -if (isset($args['prefix'])) { - $prefix = strtoupper($args['prefix']).'_'; -} else { - $prefix = ''; +if ($dsn == '' && $output == '') { + printHelp(); } /****************************************************************** End sanity checks on arguments ******************************************************************/ // Import PEAR::DB -require_once('DB.php'); +require_once 'DB.php'; // Connect to database -$db = DB::connect($args['dsn']); +$db = DB::connect($dsn); + if (DB::isError($db)) { - die("DefineGenerator: Database connection failed.\n".$db->toString()); + die("DefineGenerator: Database connection failed.\n" . $db->toString() . "\n"); } -echo "Successfully connected to database.\nFetching rights..."; +echo "Successfully connected to database.\nFetching rights... "; $sql = "SELECT * FROM liveuser_rights R, liveuser_areas A WHERE R.area_id=A.area_id"; -if (isset($args['area'])) { - $sql .= " AND A.area_define_name='$args[area]'"; +if ($area != '') { + $sql .= " AND A.area_define_name='$area'"; } $res = $db->query($sql); if (DB::isError($res)) { - die("DefineGenerator: Database query failed.\n".$res->toString()); + die("DefineGenerator: Database query failed.\n" . $res->toString() . "\n"); } -$strDef = ''; +$strDef = "<?php\n"; +$foundrights = ''; -while($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) { - switch($args['naming']) { +while($res->fetchInto($row, DB_FETCHMODE_ASSOC)) { + switch($naming) { case 2: - $strDef .= sprintf("define('%s_%s', %s);\n", - $prefix.strtoupper($row['area_define_name']), + $strDef .= sprintf("define('%s_%s_%s', %s);\n", + $prefix, + strtoupper($row['area_define_name']), + strtoupper($row['right_define_name']), + $row['right_id'] + ); + $foundrights .= sprintf("define('%s_%s_%s', %s);\n", + $prefix, + strtoupper($row['area_define_name']), strtoupper($row['right_define_name']), $row['right_id'] ); break; case 3: - $strDef .= sprintf("define('%s_%s_%s', %s);\n", - $prefix.strtoupper($args['app']), + $strDef .= sprintf("define('%s_%s_%s_%s', %s);\n", + $prefix, + strtoupper($application), + strtoupper($row['area_define_name']), + strtoupper($row['right_define_name']), + $row['right_id'] + ); + $foundrights .= sprintf("define('%s_%s_%s_%s', %s);\n", + $prefix, + strtoupper($application), strtoupper($row['area_define_name']), strtoupper($row['right_define_name']), $row['right_id'] @@ -187,17 +175,22 @@ $prefix.strtoupper($row['right_define_name']), $row['right_id'] ); + $foundrights .= sprintf("define('%s', %s);\n", + $prefix.strtoupper($row['right_define_name']), + $row['right_id'] + ); } } +$strDef .= '?>'; + +printf("done (%s rights found).\n\n", $res->numRows()); $res->free(); $db->disconnect(); -printf("done (%s rights found).\n", $res->numRows()); - -echo ("Result:\n\n$strDef"); +echo ("Result:\n\n$foundrights"); /***** Write result to file *****/ @@ -205,10 +198,52 @@ echo ("\nWriting file..."); -$fp = @fopen($args['file'], 'w') or die("Could not write to file $args[file]"); +$fp = @fopen($output, 'w') or die("Could not write to file $output"); fputs($fp, $strDef); fclose($fp); echo ("done!\n"); +/** + * printHelp() + * + * @return void + * @desc Prints out a list of commandline options + */ +function printHelp() +{ + echo (' +Syntax: +DefineGenerator [options] + +...where [options] can be: +-h --help : Shows this list of options + +-d --dsn (required) : Defines the PEAR::DB DSN to connect to the database. + Example: --dsn=mysql://user:passwd@hostname/databasename + +-o --output (required) : Defines the output file. Example: -file=/path/to/output/file + +-q --app (optional) : Defines the name of the application for which to generate the + constant definitions. Example: --app=MyApplication + +-a --area (optional) : Defines the AuthArea to be used. If not set, this script will + generate constants for all AuthAreas within the chosen + application. Example: --area=MyArea + +-p --prefix (optional) : Prefix to use for naming the constants. + Example: --prefix=LIVEUSER (prefixes all constant names with + LIVEUSER_, i.e. LIVEUSER_ADMINAREA_ADMINRIGHT) + Defaults to none. + +-n --naming (optional) : Defines the way how the constants are being named. Can be a + number between 1 and 3. Examples for naming schemes: + --naming=1 : PREFIX_RIGHTNAME + --naming=2 : PREFIX_AREANAME_RIGHTNAME + --naming=3 : PREFIX_APPLICATIONNAME_AREANAME_RIGHTNAME + Defaults to 1. + +'); +exit; +} ?>