From d91490fb7ba1090554bec62c0a520c62270e344f Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 23 Mar 2022 14:43:25 -0600 Subject: [PATCH 1/2] feat: add sample helpers --- .gitattributes | 7 ++++ src/sample_helpers.php | 87 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 .gitattributes create mode 100644 src/sample_helpers.php diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..7797468 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,7 @@ +/.gitattributes export-ignore +/.gitignore export-ignore +/test export-ignore +/docs export-ignore +/tests export-ignore +/*.xml.dist export-ignore +/.github export-ignore diff --git a/src/sample_helpers.php b/src/sample_helpers.php new file mode 100644 index 0000000..8d76a84 --- /dev/null +++ b/src/sample_helpers.php @@ -0,0 +1,87 @@ +getNumberOfRequiredParameters() + || count($argv) > $functionReflection->getNumberOfParameters() + ) { + print(get_usage(basename($file), $functionReflection)); + return; + } + + // Require composer autoload for the user + $autoloadDir = dirname(dirname($functionReflection->getFileName())); + if (!file_exists($autoloadFile = $autoloadDir . '/vendor/autoload.php')) { + printf( + 'You must run "composer install" in the sample root (%s/)' . PHP_EOL, + $autoloadDir + ); + return; + } + require_once $autoloadFile; + + // If any parameters are typehinted as "array", explode user input on "," + $parameterReflections = $functionReflection->getParameters(); + foreach (array_values($argv) as $i => $val) { + $parameterReflection = $parameterReflections[$i]; + if ( + $parameterReflection->hasType() + && 'array' === $parameterReflection->getType()->getName() + ) { + $argv[$i] = explode(',', $argv[$i]); + } + } + + // Run the function + call_user_func_array($functionName, $argv); +} + +function get_usage(string $file, ReflectionFunction $functionReflection) +{ + // Print basic usage + $paramNames = []; + foreach ($functionReflection->getParameters() as $param) { + $name = '$' . $param->getName(); + if ($param->isOptional()) { + $default = var_export($param->getDefaultValue(), true); + $name = "[$name=$default]"; + } + $paramNames[] = $name; + } + $usage = sprintf('Usage: %s %s' . PHP_EOL, $file, implode(' ', $paramNames)); + + // Print @param docs if they exist + preg_match_all( + "#(@param+\s*[a-zA-Z0-9, ()_].*)#", + $functionReflection->getDocComment(), + $matches + ); + if (isset($matches[0])) { + $usage .= PHP_EOL . "\t"; + $usage .= implode(PHP_EOL . "\t", $matches[0]) . PHP_EOL; + $usage .= PHP_EOL; + } + + return $usage; +} From a7f9b48860c8683b25a3b0ce2b8456c38fb31506 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 23 Mar 2022 14:45:13 -0600 Subject: [PATCH 2/2] removed unused lines --- .gitattributes | 2 -- 1 file changed, 2 deletions(-) diff --git a/.gitattributes b/.gitattributes index 7797468..31fc067 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,7 +1,5 @@ /.gitattributes export-ignore /.gitignore export-ignore /test export-ignore -/docs export-ignore -/tests export-ignore /*.xml.dist export-ignore /.github export-ignore