Created
March 12, 2013 17:25
-
-
Save akoimeexx/5144971 to your computer and use it in GitHub Desktop.
Basic functions I tend to re-create on a project by project basis, consolidated and documented for easy use.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** 1 2 3 4 5 6 7 8 | |
* 5678901234567890123456789012345678901234567890123456789012345678901234567890 | |
* | |
* Standardized Function Include file, v.0.0.2 | |
* Johnathan McKnight ([email protected]) | |
* | |
* | |
* This functions are basic activities I find myself writing over and over | |
* again from project to project, on a nearly constant basis. They have been | |
* collected and re-written in a more standardized way so I no longer have to | |
* re-invent the wheel, but rather include this file. | |
* | |
* The only non-standard function thus far in this include is usage(), which | |
* provides development information on each function that has a $__usage__ | |
* nowdoc string, outputting to error_log via debug() the contents therein. | |
* | |
* Writing these usage nowdoc strings forces me to think through how a | |
* function will be used and how it should perform. This leads to writing | |
* cleaner, more concise functions, which seems to me to be a | |
* "Very Good Thing" (TM). | |
*/ | |
function debug($s, $show_path=false) { | |
$__usage__ = <<<'USAGE' | |
DEBUG(STRING[, BOOLEAN]); | |
Description: | |
(void) debug( (string) $s ) | |
Writes out a string to to error_log, formatting it to include | |
details like the line number and file that the message was | |
logged from. | |
Parameters: | |
(string) s | |
Passed in string to write out to error_log | |
(boolean) show_path | |
Prints out full file path of calling script if set to true, | |
basename of script if false. Defaults to false. | |
Returns: | |
(void) No value is returned. | |
USAGE; | |
$dbg = array_pop(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1)); | |
error_log(sprintf( | |
"[%s: Line %s] %s", | |
($show_path === true) ? $dbg['file'] : basename($dbg['file']), | |
$dbg['line'], | |
$s | |
)); | |
} | |
function is_assoc($arr) { | |
$__usage__ = <<<'USAGE' | |
IS_ASSOC(ARRAY); | |
Description: | |
(boolean) is_assoc( (array) $arr ) | |
Checks a variable to see if it is an associative array by | |
evaluating is_int() on array keys. | |
Parameters: | |
(array) arr | |
Passed in array to check | |
Returns: | |
(boolean) True if variable is an associative array, false if otherwise. | |
USAGE; | |
if(is_array($arr) && count($arr) > 0) { | |
foreach(array_keys($arr) as $key) { | |
if(!is_int($key)) return true; | |
} | |
} | |
return false; | |
} | |
function print_rr($mixed, $return=false) { | |
$__usage__ = <<<'USAGE' | |
PRINT_RR(MIXED[, BOOLEAN]); | |
Description: | |
(string) print_rr( (mixed) $mixed[, (boolean) $return] ) | |
Prints a print_r'ed string to output buffer, wrapped with html | |
<pre> tags. | |
Parameters: | |
(mixed) mixed | |
Mixed data to print out. | |
(boolean) return | |
Returns results as a string if set to true, outputs to buffer | |
if false. Defaults to false. | |
Returns: | |
(string) String of print_r'ed data with <pre> tags if $return is set to | |
true, otherwise outputs to buffer and returns nothing. | |
USAGE; | |
$output = sprintf('<pre>%s</pre>', print_r($mixed, true)); | |
if($return) { | |
return $output; | |
} | |
echo $output; | |
} | |
function salt($min=8) { | |
$__usage__ = <<<'USAGE' | |
SALT([INTEGER]); | |
Description: | |
(string) salt( (integer) $min ) | |
Generates a salt string from shuffle'd md5'ed microtime, | |
between $min and 32 characters in length. | |
Parameters: | |
(integer) min | |
Minimum salt length to accept. Default is 8 characters. | |
Returns: | |
(string) String of random characters between $min & 32 characters. | |
USAGE; | |
if($min > 31) { $min = 8; } | |
return substr(str_shuffle(md5(microtime())), 0, rand($min, 31)); | |
} | |
function usage($f, $return=false) { | |
$__usage__ = <<<'USAGE' | |
USAGE(STRING[, BOOLEAN]); | |
Description: | |
(string) usage( (string $f[, (boolean) $return ) | |
Prints function usage details out to error_log via debug(). | |
Parameters: | |
(string) f | |
function name to look up usage details for. | |
(boolean) return | |
Returns results as a string if set to true, outputs to | |
error_log via debug() if false. Defaults to false. | |
Returns: | |
(string) String of function usage details if $return is set to true, | |
otherwise outputs to error_log via debug() and returns nothing. | |
USAGE; | |
try { | |
$func = new ReflectionFunction($f); | |
$srcfile = $func->getFileName(); | |
$func_start = $func->getStartLine() - 1; | |
$func_end = $func->getEndLine(); | |
$arr_func_body = array_slice(file($srcfile), $func_start, ($func_end - $func_start)); | |
$usage_start = array_search("\$__usage__ = <<<'USAGE'" . PHP_EOL, $arr_func_body); | |
$usage_end = array_search("USAGE;" . PHP_EOL, $arr_func_body); | |
if($usage_start === false) { | |
throw new Exception('No $__usage__ variable found.'); | |
} | |
if($usage_end === false) { | |
throw new Exception('No ending usage marker found.'); | |
} | |
$usage = implode('', array_slice($arr_func_body, $usage_start + 1, ($usage_end - $usage_start - 1))); | |
if($return) { | |
return $usage; | |
} | |
debug("Usage details: \n$usage"); | |
} catch (Exception $e) { | |
debug($e->getMessage()); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment