/** * @param Core\Events\ManageCommandOptions $event */ public static function execute(ManageCommandOptionsEvent $event) { $options = $event->getOptions(); $args = $event->getArgs(); $newInfos = array(); if ($event->getObjectName() == 'poller') { if ($event->getAction() == 'createAction' && isset($args['template'])) { $newInfos = PollerRepository::addCommandTemplateInfos($args['template']); } else { if ($event->getAction() == 'updateAction' && isset($args['poller'])) { $pollerIds = Poller::getIdByParameter('slug', array($args['poller'])); if (isset($pollerIds[0])) { $pollerId = $pollerIds[0]; $templateName = Poller::getParameters($pollerId, 'tmpl_name'); $newInfos = PollerRepository::addCommandTemplateInfos($templateName['tmpl_name']); } } } } foreach ($newInfos as $newInfo) { $newOption = array($newInfo['name'] => array('paramType' => 'params', 'help' => '', 'type' => 'string', 'toTransform' => $newInfo['name'], 'multiple' => '', 'required' => '0')); if (isset($newInfo['help'])) { $newOption[$newInfo['name']]['help'] = $newInfo['help']; } if (isset($newInfo['require']) && $newInfo['require'] == true && $event->getAction() == 'createAction') { $newOption[$newInfo['name']]['required'] = '1'; } $event->addOption($newOption); } }
/** * * @param array $argsList * @param type $aliveObject * @param string $action */ private function getArgs(array &$argsList, $aliveObject, $action) { $listOptions = array(); if (isset($aliveObject->options)) { $listOptions = $aliveObject->options; } $specs = new OptionCollection(); foreach ($listOptions as $option => $spec) { if ($spec['type'] != 'boolean') { if ($spec['multiple']) { $option .= '+'; } else { if ($spec['required']) { $option .= ':'; } else { $option .= '?'; } } } $specs->add($option, $spec['help'])->isa($spec['type']); } $parser = new OptionParser($specs); $parsedOptions = self::parseOptions($this->arguments, $parser); if (isset($aliveObject->objectName)) { $events = Di::getDefault()->get('events'); $manageCommandOptionsEvent = new ManageCommandOptionsEvent($aliveObject->objectName, $action, $listOptions, $parsedOptions); $events->emit('core.manage.command.options', array($manageCommandOptionsEvent)); $listOptions = $manageCommandOptionsEvent->getOptions(); $aliveObject->options = $listOptions; } $listOptions = array_merge($listOptions, array('h|help' => array('help' => 'help', 'type' => 'boolean', 'functionParams' => '', "toTransform" => '', 'required' => false, 'defaultValue' => false))); $specs = new OptionCollection(); foreach ($listOptions as $option => $spec) { if ($spec['type'] != 'boolean') { if ($spec['multiple']) { $option .= '+'; } else { if ($spec['required']) { $option .= ':'; } else { $option .= '?'; } } } $specs->add($option, $spec['help'])->isa($spec['type']); } try { $parser = new OptionParser($specs); $optionsParsed = $parser->parse($this->arguments); } catch (RequireValueException $ex) { echo $ex->getMessage(); } if ($optionsParsed->help) { $printer = new ConsoleOptionPrinter(); echo $printer->render($specs); die; } unset($listOptions['h|help']); $this->manageConsoleParams($listOptions, $optionsParsed, $argsList); }