Skip to content

Commit

Permalink
Issue #25: Make class loader an optional dependency.
Browse files Browse the repository at this point in the history
  • Loading branch information
ademarco committed Jan 11, 2018
1 parent 9208f0e commit 9268a2a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 50 deletions.
9 changes: 5 additions & 4 deletions bin/run
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ use Symfony\Component\Console\Input\ArgvInput;
use EC\OpenEuropa\TaskRunner\TaskRunner;

if (file_exists(__DIR__.'/../vendor/autoload.php')) {
require_once __DIR__.'/../vendor/autoload.php';
$classLoader = require __DIR__.'/../vendor/autoload.php';
} elseif (file_exists(__DIR__.'/../../../autoload.php')) {
require_once __DIR__ . '/../../../autoload.php';
$classLoader = require __DIR__ . '/../../../autoload.php';
}

$statusCode = (new TaskRunner())->run();
exit($statusCode);
$runner = new TaskRunner();
$runner->registerExternalCommands($classLoader);
exit($runner->run());
8 changes: 0 additions & 8 deletions src/Commands/BaseCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,15 @@

namespace EC\OpenEuropa\TaskRunner\Commands;

use Consolidation\AnnotatedCommand\AnnotatedCommand;
use Consolidation\AnnotatedCommand\AnnotationData;
use Robo\Common\ConfigAwareTrait;
use Robo\Common\IO;
use Robo\Contract\ConfigAwareInterface;
use Robo\Contract\IOAwareInterface;
use Robo\Contract\BuilderAwareInterface;
use League\Container\ContainerAwareInterface;
use League\Container\ContainerAwareTrait;
use Robo\Exception\TaskException;
use Robo\LoadAllTasks;
use Robo\Result;
use Robo\Robo;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;

/**
* Class BaseCommands.
Expand Down
56 changes: 18 additions & 38 deletions src/TaskRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace EC\OpenEuropa\TaskRunner;

use Composer\Autoload\ClassLoader;
use Consolidation\AnnotatedCommand\CommandFileDiscovery;
use EC\OpenEuropa\TaskRunner\Contract\ComposerAwareInterface;
use EC\OpenEuropa\TaskRunner\Services\Composer;
Expand Down Expand Up @@ -51,6 +52,8 @@ class TaskRunner
*/
private $application;

private $classLoader;

/**
* TaskRunner constructor.
*
Expand All @@ -69,7 +72,7 @@ public function __construct(InputInterface $input = null, OutputInterface $outpu
// Create and initialize runner.
$this->runner = new RoboRunner();
$this->runner->setContainer($this->container);
$this->runner->registerCommandClasses($this->application, $this->discoverCommandClasses());
$this->runner->registerCommandClasses($this->application, $this->getCommandDiscovery()->discover(__DIR__, 'EC\\OpenEuropa\\TaskRunner'));
}

/**
Expand All @@ -80,22 +83,6 @@ public function run()
return $this->runner->run($this->input, $this->output, $this->application);
}

/**
* @return RoboRunner
*/
public function getRunner()
{
return $this->runner;
}

/**
* @return ConsoleOutput|OutputInterface
*/
public function getOutput()
{
return $this->output;
}

/**
* @param string $class
*
Expand All @@ -110,43 +97,36 @@ public function getCommands($class)
}

/**
* @return array
* @param \Composer\Autoload\ClassLoader $classLoader
*/
private function discoverCommandClasses()
public function registerExternalCommands(ClassLoader $classLoader)
{
$commands = [];
$autoload = getcwd().'/vendor/autoload.php';

/** @var \Composer\Autoload\ClassLoader $classLoader */
$classLoader = require $autoload;
$discovery = new CommandFileDiscovery();
$discovery->setSearchPattern('*Commands.php')->setSearchLocations(['TaskRunner', 'Commands']);
$discovery = $this->getCommandDiscovery();

foreach ($classLoader->getPrefixesPsr4() as $baseNamespace => $directoryList) {
if ($this->isTaskRunnerPrefix($baseNamespace, $directoryList)) {
$directoryList = array_filter($directoryList, function ($path) {
return is_dir($path.'/TaskRunner/Commands');
});

if (!empty($directoryList)) {
$discoveredCommands = $discovery->discover($directoryList, $baseNamespace);
$commands = array_merge($commands, $discoveredCommands);
}
}

return $commands;
$this->runner->registerCommandClasses($this->application, $commands);
}

/**
* Check whereas given PSR4 prefix is an eligible TaskRunner prefix.
*
* @param string $baseNamespace
* @param array $directoryList
*
* @return bool
* @return \Consolidation\AnnotatedCommand\CommandFileDiscovery
*/
private function isTaskRunnerPrefix($baseNamespace, array $directoryList)
private function getCommandDiscovery()
{
$directoryList = array_filter($directoryList, function ($path) {
return is_dir($path.'/TaskRunner/Commands');
});
$discovery = new CommandFileDiscovery();
$discovery->setSearchPattern('*Commands.php')->setSearchLocations(['TaskRunner', 'Commands']);

return !empty($directoryList) || strstr($baseNamespace, 'OpenEuropa\\TaskRunner') !== false;
return $discovery;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/CommandsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ public function testCustomCommands()
$input = new StringInput("list");
$output = new BufferedOutput();
$runner = new TaskRunner($input, $output);
$classLoader = require __DIR__.'/../vendor/autoload.php';
$runner->registerExternalCommands($classLoader);
$runner->run();

$expected = [
Expand Down

0 comments on commit 9268a2a

Please sign in to comment.