Skip to content

Commit

Permalink
Factor global option handling into an event listener.
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-1-anderson committed Mar 23, 2016
1 parent faf89aa commit efae2f5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
4 changes: 0 additions & 4 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ public function addCommandsFromClass($className, $passThrough = null)
$args[key(array_slice($args, -1, 1, true))] = $passThrough;
}
$args[] = $input->getOptions();
// Need a better way to handle global options
// Also, this is not necessarily the best place to do this
Config::setGlobalOptions($input);
$container->setSimulated(Config::isSimulated());

$res = call_user_func_array([$roboTasks, $commandName], $args);
if (is_int($res)) {
Expand Down
36 changes: 36 additions & 0 deletions src/GlobalOptionsEventListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
namespace Robo;

use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class GlobalOptionsEventListener implements EventSubscriberInterface
{
/**
* @{@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [ConsoleEvents::COMMAND => 'setGlobalOptions'];
}

/**
* Before a Console command runs, examine the global
* commandline options from the event Input, and set
* configuration values as appropriate.
*
* @param ConsoleCommandEvent $event
*/
public function setGlobalOptions(ConsoleCommandEvent $event)
{
/* @var Input $input */
$input = $event->getInput();

// Need a better way to handle global options.
// This is slightly improved from before.
Config::setGlobalOptions($input);
Config::getContainer()->setSimulated(Config::isSimulated());
}
}
8 changes: 7 additions & 1 deletion src/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\EventDispatcher\EventDispatcher;

class Runner
{
Expand Down Expand Up @@ -95,7 +96,8 @@ public function execute($input = null)
static::addServiceProviders($container);
$container->share('application', \Robo\Application::class)
->withArgument('Robo')
->withArgument(self::VERSION);
->withArgument(self::VERSION)
->withMethodCall('setDispatcher', ['eventDispatcher']);
Config::setContainer($container);
}

Expand All @@ -108,6 +110,7 @@ public function execute($input = null)
$app->run(Config::input(), Config::output());
return;
}

$app->addCommandsFromClass($this->roboClass, $this->passThroughArgs);
$app->run($container->get('input'), $container->get('output'));
}
Expand Down Expand Up @@ -137,6 +140,9 @@ public static function configureContainer($container, $input = null, $output = n
->withMethodCall('setLogOutputStyler', ['logStyler']);
$container->share('resultPrinter', \Robo\Log\ResultPrinter::class);
$container->add('simulator', \Robo\Task\Simulator::class);
$container->share('globalOptionsEventListener', \Robo\GlobalOptionsEventListener::class);
$container->share('eventDispatcher', \Symfony\Component\EventDispatcher\EventDispatcher::class)
->withMethodCall('addSubscriber', ['globalOptionsEventListener']);

// Register our various inflectors.
$container->inflector(\Psr\Log\LoggerAwareInterface::class)
Expand Down

0 comments on commit efae2f5

Please sign in to comment.