Skip to content

Commit

Permalink
Move register command out of runner and into Robo class.
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-1-anderson committed Apr 30, 2023
1 parent 24ea81f commit 6dc7afd
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 52 deletions.
72 changes: 72 additions & 0 deletions src/Robo.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Consolidation\Config\Loader\YamlConfigLoader;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Application as SymfonyApplication;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Process\Process;

/**
Expand Down Expand Up @@ -508,4 +509,75 @@ public static function process(Process $process)
{
return ProcessExecutor::create(static::getContainer(), $process);
}

/**
* @param \Robo\Application $app
* @param string|object
*
* @return null|object
*/
public static function register($app, $handler)
{
$container = static::getContainer();
$instance = $this->instantiateCommandClass($handler);
if (!$instance) {
return;
}

// If the instance is a Symfony Command, register it with
// the application
if ($instance instanceof Command) {
$app->add($instance);
return $instance;
}

// Register commands for all of the public methods in the CommandFile.
$commandFactory = $container->get('commandFactory');
$commandList = $commandFactory->createCommandsFromClass($instance);
foreach ($commandList as $command) {
$app->add($command);
}

return $instance;
}

/**
* @param string|object
*
* @return null|object
*/
protected static function instantiate($handler)
{
$container = Robo::getContainer();

// Register the RoboFile with the container and then immediately
// fetch it; this ensures that all of the inflectors will run.
// If the command class is already an instantiated object, then
// just use it exactly as it was provided to us.
if (is_string($handler)) {
if (!class_exists($handler)) {
return;
}
$reflectionClass = new \ReflectionClass($handler);
if ($reflectionClass->isAbstract()) {
return;
}

$instanceName = "{$handler}Instance";
Robo::addShared($container, $instanceName, $handler);
$handler = $container->get($instanceName);
}
// If the command class is a Builder Aware Interface, then
// ensure that it has a builder. Every command class needs
// its own collection builder, as they have references to each other.
if ($handler instanceof BuilderAwareInterface) {
$builder = CollectionBuilder::create($container, $handler);
$handler->setBuilder($builder);
}
if ($handler instanceof ContainerAwareInterface) {
$handler->setContainer($container);
}
return $handler;
}

}
55 changes: 3 additions & 52 deletions src/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,62 +338,13 @@ protected function discoverCommandClasses($relativeNamespace)
* @param \Robo\Application $app
* @param string|BuilderAwareInterface|ContainerAwareInterface $commandClass
*
* @return null|object
*/
public function registerCommandClass($app, $commandClass)
{
$container = Robo::getContainer();
$roboCommandFileInstance = $this->instantiateCommandClass($commandClass);
if (!$roboCommandFileInstance) {
return;
}

// Register commands for all of the public methods in the RoboFile.
$commandFactory = $container->get('commandFactory');
$commandList = $commandFactory->createCommandsFromClass($roboCommandFileInstance);
foreach ($commandList as $command) {
$app->add($command);
}
return $roboCommandFileInstance;
}

/**
* @param string|\Robo\Contract\BuilderAwareInterface|\League\Container\ContainerAwareInterface $commandClass
* @deprecated Use Robo::register directly
*
* @return null|object
*/
protected function instantiateCommandClass($commandClass)
public function registerCommandClass($app, $commandClass)
{
$container = Robo::getContainer();

// Register the RoboFile with the container and then immediately
// fetch it; this ensures that all of the inflectors will run.
// If the command class is already an instantiated object, then
// just use it exactly as it was provided to us.
if (is_string($commandClass)) {
if (!class_exists($commandClass)) {
return;
}
$reflectionClass = new \ReflectionClass($commandClass);
if ($reflectionClass->isAbstract()) {
return;
}

$commandFileName = "{$commandClass}Commands";
Robo::addShared($container, $commandFileName, $commandClass);
$commandClass = $container->get($commandFileName);
}
// If the command class is a Builder Aware Interface, then
// ensure that it has a builder. Every command class needs
// its own collection builder, as they have references to each other.
if ($commandClass instanceof BuilderAwareInterface) {
$builder = CollectionBuilder::create($container, $commandClass);
$commandClass->setBuilder($builder);
}
if ($commandClass instanceof ContainerAwareInterface) {
$commandClass->setContainer($container);
}
return $commandClass;
return Robo::register($app, $commandClass);
}

public function installRoboHandlers()
Expand Down

0 comments on commit 6dc7afd

Please sign in to comment.