|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Google\Analytics\Admin\Samples; |
| 4 | + |
| 5 | +use ReflectionFunction; |
| 6 | + |
| 7 | +function execute_sample(string $file, string $namespace, ?array $argv) |
| 8 | +{ |
| 9 | + // Return if sample file is not being executed via CLI |
| 10 | + if (is_null($argv)) { |
| 11 | + return; |
| 12 | + } |
| 13 | + |
| 14 | + // Return if sample file is being included via PHPUnit |
| 15 | + $argvFile = array_shift($argv); |
| 16 | + if ('.php' != substr($argvFile, -4)) { |
| 17 | + return; |
| 18 | + } |
| 19 | + |
| 20 | + // Determine the name of the function to execute |
| 21 | + $functionName = sprintf('%s\\%s', $namespace, basename($file, '.php')); |
| 22 | + |
| 23 | + // Verify the user has supplied the correct number of arguments |
| 24 | + $functionReflection = new ReflectionFunction($functionName); |
| 25 | + if ( |
| 26 | + count($argv) < $functionReflection->getNumberOfRequiredParameters() |
| 27 | + || count($argv) > $functionReflection->getNumberOfParameters() |
| 28 | + ) { |
| 29 | + print(get_usage(basename($file), $functionReflection)); |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + // Require composer autoload for the user |
| 34 | + $autoloadDir = dirname(dirname($functionReflection->getFileName())); |
| 35 | + if (!file_exists($autoloadFile = $autoloadDir . '/vendor/autoload.php')) { |
| 36 | + printf( |
| 37 | + 'You must run "composer install" in the sample root (%s/)' . PHP_EOL, |
| 38 | + $autoloadDir |
| 39 | + ); |
| 40 | + return; |
| 41 | + } |
| 42 | + require_once $autoloadFile; |
| 43 | + |
| 44 | + // If any parameters are typehinted as "array", explode user input on "," |
| 45 | + $validArrayTypes = ['array', 'array<string>', 'string[]']; |
| 46 | + $parameterReflections = $functionReflection->getParameters(); |
| 47 | + foreach (array_values($argv) as $i => $val) { |
| 48 | + $parameterReflection = $parameterReflections[$i]; |
| 49 | + if ($parameterReflection->hasType()) { |
| 50 | + $parameterType = $parameterReflection->getType()->getName(); |
| 51 | + if (in_array($parameterType, $validArrayTypes) && !is_array($val)) { |
| 52 | + $key = array_search($val, $argv); |
| 53 | + $argv[$key] = explode(',', $argv[$key]); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + // Run the function |
| 59 | + return call_user_func_array($functionName, $argv); |
| 60 | +} |
| 61 | + |
| 62 | +function get_usage(string $file, ReflectionFunction $functionReflection) |
| 63 | +{ |
| 64 | + // Print basic usage |
| 65 | + $paramNames = []; |
| 66 | + foreach ($functionReflection->getParameters() as $param) { |
| 67 | + $name = '$' . $param->getName(); |
| 68 | + if ($param->isOptional()) { |
| 69 | + $default = var_export($param->getDefaultValue(), true); |
| 70 | + $name = "[$name=$default]"; |
| 71 | + } |
| 72 | + $paramNames[] = $name; |
| 73 | + } |
| 74 | + $usage = sprintf('Usage: %s %s' . PHP_EOL, $file, implode(' ', $paramNames)); |
| 75 | + |
| 76 | + // Print @param docs if they exist |
| 77 | + preg_match_all( |
| 78 | + "#(@param+\s*[a-zA-Z0-9, ()_].*)#", |
| 79 | + $functionReflection->getDocComment(), |
| 80 | + $matches |
| 81 | + ); |
| 82 | + if (isset($matches[0])) { |
| 83 | + $usage .= PHP_EOL . "\t"; |
| 84 | + $usage .= implode(PHP_EOL . "\t", $matches[0]) . PHP_EOL; |
| 85 | + $usage .= PHP_EOL; |
| 86 | + } |
| 87 | + |
| 88 | + return $usage; |
| 89 | +} |
0 commit comments