diff --git a/bin/run b/bin/run index 4113adbc..5a20592e 100755 --- a/bin/run +++ b/bin/run @@ -3,6 +3,7 @@ use Symfony\Component\Console\Input\ArgvInput; use OpenEuropa\TaskRunner\TaskRunner; +use Symfony\Component\Console\Output\ConsoleOutput; if (file_exists(__DIR__.'/../vendor/autoload.php')) { $classLoader = require __DIR__.'/../vendor/autoload.php'; @@ -10,6 +11,5 @@ if (file_exists(__DIR__.'/../vendor/autoload.php')) { $classLoader = require __DIR__ . '/../../../autoload.php'; } -$runner = new TaskRunner(); -$runner->registerExternalCommands($classLoader); -exit($runner->run()); \ No newline at end of file +$runner = new TaskRunner(new ArgvInput(), new ConsoleOutput(), $classLoader); +exit($runner->run()); diff --git a/src/TaskRunner.php b/src/TaskRunner.php index b294ebf2..41762805 100644 --- a/src/TaskRunner.php +++ b/src/TaskRunner.php @@ -50,6 +50,11 @@ class TaskRunner */ private $input; + /** + * @var \Composer\Autoload\ClassLoader + */ + private $classLoader; + /** * @var Application */ @@ -65,11 +70,13 @@ class TaskRunner * * @param InputInterface $input * @param OutputInterface|null $output + * @param ClassLoader $classLoader */ - public function __construct(InputInterface $input = null, OutputInterface $output = null) + public function __construct(InputInterface $input = null, OutputInterface $output = null, ClassLoader $classLoader = null) { $this->input = is_null($input) ? new ArgvInput() : $input; $this->output = is_null($output) ? new ConsoleOutput() : $output; + $this->classLoader = is_null($classLoader) ? new ClassLoader() : $classLoader; $this->workingDir = $this->getWorkingDir($this->input); chdir($this->workingDir); @@ -83,6 +90,7 @@ public function __construct(InputInterface $input = null, OutputInterface $outpu $this->runner->setContainer($this->container); $this->runner->registerCommandClasses($this->application, $this->getCommandDiscovery()->discover(__DIR__, 'OpenEuropa\\TaskRunner')); + $this->registerExternalCommands(); // Register commands defined in runner.yml file. $this->registerDynamicCommands($this->application); } @@ -108,15 +116,12 @@ public function getCommands($class) return $this->getContainer()->get("{$class}Commands"); } - /** - * @param \Composer\Autoload\ClassLoader $classLoader - */ - public function registerExternalCommands(ClassLoader $classLoader) + private function registerExternalCommands() { $commands = []; $discovery = $this->getCommandDiscovery(); - foreach ($classLoader->getPrefixesPsr4() as $baseNamespace => $directoryList) { + foreach ($this->classLoader->getPrefixesPsr4() as $baseNamespace => $directoryList) { $directoryList = array_filter($directoryList, function ($path) { return is_dir($path.'/TaskRunner/Commands'); }); diff --git a/tests/AbstractTaskTest.php b/tests/AbstractTaskTest.php index 46bb41d9..30c1372e 100644 --- a/tests/AbstractTaskTest.php +++ b/tests/AbstractTaskTest.php @@ -31,7 +31,8 @@ abstract class AbstractTaskTest extends AbstractTest implements ContainerAwareIn public function setup() { $this->output = new BufferedOutput(); - $runner = new TaskRunner(new StringInput(''), $this->output); + $classLoader = require __DIR__.'/../vendor/autoload.php'; + $runner = new TaskRunner(new StringInput(''), $this->output, $classLoader); $this->setContainer($runner->getContainer()); } diff --git a/tests/CommandsTest.php b/tests/CommandsTest.php index 6b059dba..dfe5e0e5 100644 --- a/tests/CommandsTest.php +++ b/tests/CommandsTest.php @@ -35,7 +35,7 @@ public function testSimulation($command, array $config, $composer, array $expect $input = new StringInput("{$command} --simulate --working-dir=".$this->getSandboxRoot()); $output = new BufferedOutput(); - $runner = new TaskRunner($input, $output); + $runner = new TaskRunner($input, $output, $this->getClassLoader()); $runner->run(); $text = $output->fetch(); @@ -65,7 +65,7 @@ public function testSetupCommands($command, $source, $destination, array $config $input = new StringInput("{$command} --working-dir=".$this->getSandboxRoot()); $output = new BufferedOutput(); - $runner = new TaskRunner($input, $output); + $runner = new TaskRunner($input, $output, $this->getClassLoader()); $runner->run(); $actual = file_get_contents($destination); @@ -96,9 +96,7 @@ 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 = new TaskRunner($input, $output, $this->getClassLoader()); $runner->run(); $expected = [ @@ -127,7 +125,7 @@ public function testDrushSetup(array $config, array $expected) file_put_contents($configFile, Yaml::dump($config)); $input = new StringInput("drupal:drush-setup --working-dir=".$this->getSandboxRoot()); - $runner = new TaskRunner($input, new BufferedOutput()); + $runner = new TaskRunner($input, new BufferedOutput(), $this->getClassLoader()); $runner->run(); foreach ($expected as $row) { @@ -149,7 +147,7 @@ public function testSettingsSetup(array $config, array $expected) file_put_contents($configFile, Yaml::dump($config)); $input = new StringInput("drupal:settings-setup --working-dir=".$this->getSandboxRoot()); - $runner = new TaskRunner($input, new BufferedOutput()); + $runner = new TaskRunner($input, new BufferedOutput(), $this->getClassLoader()); $runner->run(); @@ -210,4 +208,12 @@ protected function assertContainsNotContains($content, array $expected) $this->assertNotContains($row['not_contains'], $content); } } + + /** + * @return \Composer\Autoload\ClassLoader + */ + protected function getClassLoader() + { + return require __DIR__.'/../vendor/autoload.php'; + } }