Skip to content

Commit

Permalink
Move register to 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 4351c0c
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 53 deletions.
73 changes: 73 additions & 0 deletions src/Robo.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@

use Composer\Autoload\ClassLoader;
use League\Container\Container;
use League\Container\ContainerAwareInterface;
use League\Container\Definition\DefinitionInterface;
use Psr\Container\ContainerInterface;
use Robo\Collection\CollectionBuilder;
use Robo\Common\ProcessExecutor;
use Robo\Contract\BuilderAwareInterface;
use Consolidation\Config\ConfigInterface;
use Consolidation\Config\Loader\ConfigProcessor;
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 +512,73 @@ public static function process(Process $process)
{
return ProcessExecutor::create(static::getContainer(), $process);
}

/**
* @param \Robo\Application $app
* @param string|object $handler
*
* @return null|object
*/
public static function register($app, $handler)
{
$container = Robo::getContainer();
$instance = static::instantiate($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 RoboFile.
$commandFactory = $container->get('commandFactory');
$commandList = $commandFactory->createCommandsFromClass($instance);
foreach ($commandList as $command) {
$app->add($command);
}
return $instance;
}

/**
* @param string|object $handler
*
* @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;
}

$commandFileName = "{$handler}Commands";
Robo::addShared($container, $commandFileName, $handler);
$handler = $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 ($handler instanceof BuilderAwareInterface) {
$builder = CollectionBuilder::create($container, $handler);
$handler->setBuilder($builder);
}
if ($handler instanceof ContainerAwareInterface) {
$handler->setContainer($container);
}
return $handler;
}
}
57 changes: 4 additions & 53 deletions src/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ protected function getRoboFileCommands($output)
public function registerCommandClasses($app, $commandClasses)
{
foreach ((array)$commandClasses as $commandClass) {
$this->registerCommandClass($app, $commandClass);
Robo::register($app, $commandClass);
}
}

Expand All @@ -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 4351c0c

Please sign in to comment.