1
0
mirror of https://github.com/flarum/core.git synced 2025-07-28 12:10:51 +02:00

Add console extender (#2057)

* Made the console command system extender-friendly

* Added console extender

* Added ConsoleTestCase to integration tests

* Added integration tests for console extender

* Marked event-based console extension system as deprecated

* Moved trimming command output of whitespace into superclass

* Renamed 'add' to 'command'

* Added special processing for laravel commands

* Code style fixes

* More style fixes

* Fixed $this->container
This commit is contained in:
Alexander Skvortsov
2020-04-03 13:38:54 -04:00
committed by GitHub
parent ca32879e1e
commit c84dd4d5bf
8 changed files with 204 additions and 15 deletions

View File

@@ -0,0 +1,34 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Console;
use Flarum\Database\Console\GenerateMigrationCommand;
use Flarum\Database\Console\MigrateCommand;
use Flarum\Database\Console\ResetCommand;
use Flarum\Foundation\AbstractServiceProvider;
use Flarum\Foundation\Console\CacheClearCommand;
class ConsoleServiceProvider extends AbstractServiceProvider
{
/**
* {@inheritdoc}
*/
public function register()
{
$this->app->singleton('flarum.console.commands', function () {
return [
CacheClearCommand::class,
GenerateMigrationCommand::class,
MigrateCommand::class,
ResetCommand::class,
];
});
}
}

View File

@@ -14,9 +14,7 @@ use Illuminate\Console\Command;
use Symfony\Component\Console\Application as ConsoleApplication; use Symfony\Component\Console\Application as ConsoleApplication;
/** /**
* Configure the console application. * @deprecated
*
* This event is fired after the core commands are added to the application.
*/ */
class Configuring class Configuring
{ {

View File

@@ -39,11 +39,14 @@ class Server
$console->add($command); $console->add($command);
} }
$this->extend($console); $this->extend($console); // deprecated
exit($console->run()); exit($console->run());
} }
/**
* @deprecated
*/
private function extend(ConsoleApplication $console) private function extend(ConsoleApplication $console)
{ {
$app = Application::getInstance(); $app = Application::getInstance();

View File

@@ -0,0 +1,37 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Extend;
use Flarum\Extension\Extension;
use Illuminate\Contracts\Container\Container;
class Console implements ExtenderInterface
{
protected $addCommands = [];
/**
* Add a command to the console.
*
* @param string $command ::class attribute of command class, which must extend Flarum\Console\AbstractCommand
*/
public function command($command)
{
$this->addCommands[] = $command;
return $this;
}
public function extend(Container $container, Extension $extension = null)
{
$container->extend('flarum.console.commands', function ($existingCommands) {
return array_merge($existingCommands, $this->addCommands);
});
}
}

View File

@@ -9,13 +9,10 @@
namespace Flarum\Foundation; namespace Flarum\Foundation;
use Flarum\Database\Console\GenerateMigrationCommand;
use Flarum\Database\Console\MigrateCommand;
use Flarum\Database\Console\ResetCommand;
use Flarum\Foundation\Console\CacheClearCommand;
use Flarum\Foundation\Console\InfoCommand; use Flarum\Foundation\Console\InfoCommand;
use Flarum\Http\Middleware\DispatchRoute; use Flarum\Http\Middleware\DispatchRoute;
use Flarum\Settings\SettingsRepositoryInterface; use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Console\Command;
use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Container\Container;
use Laminas\Stratigility\Middleware\OriginalMessages; use Laminas\Stratigility\Middleware\OriginalMessages;
use Laminas\Stratigility\MiddlewarePipe; use Laminas\Stratigility\MiddlewarePipe;
@@ -115,12 +112,21 @@ class InstalledApp implements AppInterface
*/ */
public function getConsoleCommands() public function getConsoleCommands()
{ {
return [ $commands = [];
$this->container->make(GenerateMigrationCommand::class),
$this->container->make(InfoCommand::class, ['config' => $this->config]), // The info command is a special case, as it requires a config parameter that's only available here.
$this->container->make(MigrateCommand::class), $commands[] = $this->container->make(InfoCommand::class, ['config' => $this->config]);
$this->container->make(ResetCommand::class),
$this->container->make(CacheClearCommand::class), foreach ($this->container->make('flarum.console.commands') as $command) {
]; $newCommand = $this->container->make($command);
if ($newCommand instanceof Command) {
$newCommand->setLaravel($this->container);
}
$commands[] = $newCommand;
}
return $commands;
} }
} }

View File

@@ -12,6 +12,7 @@ namespace Flarum\Foundation;
use Flarum\Admin\AdminServiceProvider; use Flarum\Admin\AdminServiceProvider;
use Flarum\Api\ApiServiceProvider; use Flarum\Api\ApiServiceProvider;
use Flarum\Bus\BusServiceProvider; use Flarum\Bus\BusServiceProvider;
use Flarum\Console\ConsoleServiceProvider;
use Flarum\Database\DatabaseServiceProvider; use Flarum\Database\DatabaseServiceProvider;
use Flarum\Database\MigrationServiceProvider; use Flarum\Database\MigrationServiceProvider;
use Flarum\Discussion\DiscussionServiceProvider; use Flarum\Discussion\DiscussionServiceProvider;
@@ -113,6 +114,7 @@ class InstalledSite implements SiteInterface
$laravel->register(AdminServiceProvider::class); $laravel->register(AdminServiceProvider::class);
$laravel->register(ApiServiceProvider::class); $laravel->register(ApiServiceProvider::class);
$laravel->register(BusServiceProvider::class); $laravel->register(BusServiceProvider::class);
$laravel->register(ConsoleServiceProvider::class);
$laravel->register(DatabaseServiceProvider::class); $laravel->register(DatabaseServiceProvider::class);
$laravel->register(DiscussionServiceProvider::class); $laravel->register(DiscussionServiceProvider::class);
$laravel->register(ExtensionServiceProvider::class); $laravel->register(ExtensionServiceProvider::class);

View File

@@ -0,0 +1,44 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Tests\integration;
use Flarum\Foundation\Application;
use Symfony\Component\Console\Application as ConsoleApplication;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
abstract class ConsoleTestCase extends TestCase
{
protected $console;
protected function console()
{
if (is_null($this->console)) {
$this->console = new ConsoleApplication('Flarum', Application::VERSION);
$this->console->setAutoExit(false);
foreach ($this->app()->getConsoleCommands() as $command) {
$this->console->add($command);
}
}
return $this->console;
}
protected function runCommand(array $inputArray)
{
$input = new ArrayInput($inputArray);
$output = new BufferedOutput();
$this->console()->run($input, $output);
return trim($output->fetch());
}
}

View File

@@ -0,0 +1,65 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Tests\integration\extenders;
use Flarum\Console\AbstractCommand;
use Flarum\Extend;
use Flarum\Tests\integration\ConsoleTestCase;
class ConsoleTest extends ConsoleTestCase
{
/**
* @test
*/
public function custom_command_doesnt_exist_by_default()
{
$input = [
'command' => 'customTestCommand'
];
$this->assertEquals('Command "customTestCommand" is not defined.', $this->runCommand($input));
}
/**
* @test
*/
public function custom_command_exists_when_added()
{
$this->extend(
(new Extend\Console())
->command(CustomCommand::class)
);
$input = [
'command' => 'customTestCommand'
];
$this->assertEquals('Custom Command.', $this->runCommand($input));
}
}
class CustomCommand extends AbstractCommand
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this->setName('customTestCommand');
}
/**
* {@inheritdoc}
*/
protected function fire()
{
$this->info('Custom Command.');
}
}