/** * @param array $defaultStrategies * @param array $strategyMap * @param \FusePump\Cli\Inputs $inputs * @param array $arguments * @return callable */ function strategyFactory($defaultStrategies, $strategyMap = array(), $inputs = null, $arguments = array()) { $strategyList = array(function () { return false; }); $addStrategy = function ($strategy) use(&$strategyList, $arguments) { $args = array(); $reflection = new \ReflectionFunction($strategy); foreach ($reflection->getParameters() as $param) { if ($param->getName() === 'patchFile') { continue; } elseif ($param->getName() === 'superStrategy') { $args[] = end($strategyList); } elseif (array_key_exists($param->getName(), $arguments)) { $args[] = $arguments[$param->getName()]; } else { $args[] = null; } } $strategyList[] = function ($patchFile) use($strategy, $args) { return call_user_func_array($strategy, array_merge(array($patchFile), $args)); }; }; foreach ($strategyMap as $option => $strategy) { if ($inputs->get($option)) { $addStrategy($strategy); } } if (count($strategyList) < 2) { foreach ($defaultStrategies as $strategy) { $addStrategy($strategy); } } return end($strategyList); }
<?php /** * Sandwich maker */ require dirname(__FILE__) . '/../lib/FusePump/Cli/Inputs.php'; use FusePump\Cli\Inputs; $cli = new Inputs($argv); $cli->option('-h, --ham', 'Add ham'); $cli->option('-m, --mayo', 'Add mayonaise'); $cli->option('-c, --cheese [type]', 'Add a cheese'); $cli->option('-b, --bread [type]', 'Type of bread', true); // required input $cli->param('when', 'When do you want the sandwhich (now, tomorrow or never)', true); if (!$cli->parse()) { exit(1); } echo "You ordered a sandwich " . $cli->get('when') . " with: \n"; if ($cli->get('-h')) { echo " - Ham \n"; } if ($cli->get('-m')) { echo " - Mayonaise \n"; } if ($cli->get('--cheese')) { echo ' - ' . $cli->get('--cheese') . " cheese \n"; } echo 'On ' . $cli->get('-b') . " bread \n";