Skip to content

Commit

Permalink
More cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
donatj committed Mar 15, 2022
1 parent ecbdaae commit 83c56fe
Showing 1 changed file with 29 additions and 17 deletions.
46 changes: 29 additions & 17 deletions drop.php
Expand Up @@ -7,27 +7,34 @@

namespace {

use function donatj\drop\is_cli;

/**
* A VERY handy function to empty output buffers and take any number of arguments and expose them.
* This is particularly helpful for looking into arrays and objects to see what information lies
* within. This function will also kill the script after echoing the information. This function
* takes any number of any typed arguments and displays them all.
*
* @param mixed ...$args
*/
function drop( ...$args ) : void {
if( donatj\drop\is_cli() === true ) {
if( is_cli() === true ) {
echo "\033[2J\n";
}

see(...$args);
die();
exit(1);
}

/**
* A handy function to expose any number of any typed arguments while NOT killing the script
* after output.
*
* @param mixed ...$args
*/
function see( ...$args ) : void {
echo donatj\drop\output_format_args(...$args);
$out = donatj\drop\output_format_args(...$args);
echo is_cli() ? $out : "<pre>{$out}</pre>";
}
}

Expand All @@ -36,26 +43,31 @@ function see( ...$args ) : void {
/**
* A handy function to expose any number of any typed arguments while NOT killing the script
* after output. Unlike drop(), this function must be "echoed".
*
* @param mixed ...$args
* @return string
*/
function output_format_args() : string {
$final = "";
$arguments = func_get_args();
if( !empty($arguments) ) {
foreach( $arguments as $i => $argument ) {
$final .= sprintf("\n\n%'|21s Arg No. %-2s %'|43s\n\n", "", $i, "");
$argument = is_null($argument) ? "(null)null" : $argument;
$argument = $argument === false ? "(bool)false" : $argument;
$argument = $argument === true ? "(bool)true" : $argument;
$final .= sprintf("\n\n%s\n\n", print_r($argument, true));
function output_format_args( ...$args ) : string {
$final = "";
foreach( $args as $i => $argument ) {
$final .= sprintf("\n\n%'|21s Arg No. %-2s %'|43s\n\n", "", $i, "");
if( is_scalar($argument) ) {
$val = var_export($argument, true);
} else {
$val = print_r($argument, true);
}
$final .= sprintf("\n\n%'|21s EOF %'|50s\n\n", "", "");

return is_cli() === true ? $final : "<pre>{$final}</pre>";
$final .= "\n\n{$val}\n\n";
}
$final .= sprintf("\n\n%'|21s EOF %'|50s\n\n", "", "");

return "";
return $final;
}

/**
* Utility function for detecting CLI usage.
*
* @return bool
*/
function is_cli() : bool {
if( defined('IS_CLI') ) {
return \IS_CLI;
Expand Down

0 comments on commit 83c56fe

Please sign in to comment.