setName('event:run') ->setDescription('Executes an event') ->addArgument( 'event_name', InputArgument::REQUIRED, 'Event name (e.g. "adm:OnDoSomething")' ); } /** * Executes the current command. * * @param InputInterface $input An InputInterface instance. * @param OutputInterface $output An OutputInterface instance. * * @return null|integer */ protected function execute(InputInterface $input, OutputInterface $output) { $event_name = $this->io->getArgument('event_name'); $run_event = new \kEvent($event_name); $this->Application->HandleEvent($run_event); return $run_event->status == \kEvent::erSUCCESS ? 0 : 64; } /** * Return possible values for the named argument. * * @param string $argumentName Argument name. * @param CompletionContext $context Completion context. * * @return array */ public function completeArgumentValues($argumentName, CompletionContext $context) { $suggestions = parent::completeArgumentValues($argumentName, $context); if ( $argumentName === 'event_name' ) { $event_name = $context->getCurrentWord(); // Suggest unit config prefixes. if ( strpos($event_name, ':') === false ) { return $this->Application->UnitConfigReader->getPrefixes(false); } try { $event = new \kEvent($event_name); } catch ( \InvalidArgumentException $e ) { // Invalid event name. return array(); } // Unknown unit. if ( !$this->Application->prefixRegistred($event->Prefix) ) { return array(); } // Suggest event names. $suggestions = array(); $reflection = new \ReflectionClass( $this->Application->makeClass($event->Prefix . '_EventHandler') ); foreach ( $reflection->getMethods() as $method ) { if ( substr($method->getName(), 0, 2) === 'On' ) { $suggestions[] = $event->Prefix . ':' . $method->getName(); } } return $suggestions; } return $suggestions; } }