Last active
August 29, 2015 14:09
-
-
Save kent013/0e9b7b52b3e247c96eac to your computer and use it in GitHub Desktop.
Utility functions for **** php spaghetti codes.
This file contains 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 | |
function _findRequestFlag($name){ | |
if(isset($_REQUEST[$name])){ | |
return true; | |
} | |
return false; | |
} | |
function _getRequestOrDie($name, $message){ | |
$value = _getArg($name); | |
if(is_null($value)){ | |
_error($message); | |
} | |
return $value; | |
} | |
function _getRequest($name, $default = null){ | |
if(isset($_REQUEST[$name])){ | |
return $_REQUEST[$name]; | |
} | |
return $default; | |
} | |
function _getArgOrDie($name, $message){ | |
$value = _getArg($name, null); | |
if(is_null($value)){ | |
_error($message); | |
} | |
return $value; | |
} | |
function _getArg($name, $default){ | |
foreach($_SERVER['argv'] as $k => $v){ | |
if(($v == "--" . $name || $v == "-" . $name) && | |
isset($_SERVER['argv'][$k + 1])){ | |
return $_SERVER['argv'][$k + 1]; | |
} | |
} | |
return $default; | |
} | |
function _findArg($name){ | |
return in_array("-" . $name, $_SERVER['argv']) || in_array("--" . $name, $_SERVER['argv']); | |
} | |
function _msg($msg, $indent = 0){ | |
$sp = ''; | |
for ($i = 0; $i < $indent; $i++){ | |
$sp .= ' '; | |
} | |
echo $sp . $msg . "\n"; | |
} | |
function _info($msg, $indent = 0){ | |
$date = date('Y/m/d H:i:s'); | |
$msg = '[INFO] ' . $date . " : " . $msg; | |
_msg($msg, $indent); | |
} | |
function _warn($msg, $indent = 0){ | |
$date = date('Y/m/d H:i:s'); | |
$msg = '[WARN] ' . $date . " : " . $msg; | |
_msg($msg, $indent); | |
} | |
function _error($msg, $indent = 0){ | |
$date = date('Y/m/d H:i:s'); | |
$msg = '[FATAL] ' . $date . " : " . $msg; | |
_exit($msg, $indent); | |
} | |
function _exit($msg, $indent = 0){ | |
_msg($msg, $indent); | |
exit; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment