Changeset View
Changeset View
Standalone View
Standalone View
branches/5.3.x/core/kernel/Console/Command/RunEventCommand.php
Property | Old Value | New Value |
---|---|---|
svn:eol-style | null | LF |
<?php | |||||
/** | |||||
* @version $Id$ | |||||
* @package In-Portal | |||||
* @copyright Copyright (C) 1997 - 2015 Intechnic. All rights reserved. | |||||
* @license GNU/GPL | |||||
* In-Portal is Open Source software. | |||||
* This means that this software may have been modified pursuant | |||||
* the GNU General Public License, and as distributed it includes | |||||
* or is derivative of works licensed under the GNU General Public License | |||||
* or other free or open source software licenses. | |||||
* See http://www.in-portal.org/license for copyright notices and details. | |||||
*/ | |||||
namespace Intechnic\InPortal\Core\kernel\Console\Command; | |||||
use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface; | |||||
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext; | |||||
use Symfony\Component\Console\Input\InputArgument; | |||||
use Symfony\Component\Console\Input\InputInterface; | |||||
use Symfony\Component\Console\Output\OutputInterface; | |||||
defined('FULL_PATH') or die('restricted access!'); | |||||
class RunEventCommand extends AbstractCommand implements CompletionAwareInterface | |||||
{ | |||||
/** | |||||
* Configures the current command. | |||||
* | |||||
* @return void | |||||
*/ | |||||
protected function configure() | |||||
{ | |||||
$this | |||||
->setName('event:run') | |||||
->setDescription('Executes an event') | |||||
->addArgument( | |||||
'event_name', | |||||
InputArgument::REQUIRED, | |||||
'Event name (e.g. "<info>adm:OnDoSomething</info>")' | |||||
); | |||||
} | |||||
/** | |||||
* 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 = $input->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 option | |||||
* | |||||
* @param string $optionName Option name. | |||||
* @param CompletionContext $context Completion context. | |||||
* | |||||
* @return array | |||||
*/ | |||||
public function completeOptionValues($optionName, CompletionContext $context) | |||||
{ | |||||
return array(); | |||||
} | |||||
/** | |||||
* 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) | |||||
{ | |||||
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 array(); | |||||
} | |||||
} |