Skip to content

Commit

Permalink
feat: cli command for enabling or disabling an extension (#3816)
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland authored May 1, 2023
1 parent 919c3bb commit b4f3f05
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
4 changes: 3 additions & 1 deletion framework/core/src/Console/ConsoleServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Flarum\Console\Cache\Factory;
use Flarum\Database\Console\MigrateCommand;
use Flarum\Database\Console\ResetCommand;
use Flarum\Extension\Console\ToggleExtensionCommand;
use Flarum\Foundation\AbstractServiceProvider;
use Flarum\Foundation\Console\AssetsPublishCommand;
use Flarum\Foundation\Console\CacheClearCommand;
Expand Down Expand Up @@ -63,7 +64,8 @@ public function register()
MigrateCommand::class,
ResetCommand::class,
ScheduleListCommand::class,
ScheduleRunCommand::class
ScheduleRunCommand::class,
ToggleExtensionCommand::class
// Used internally to create DB dumps before major releases.
// \Flarum\Database\Console\GenerateDumpCommand::class
];
Expand Down
78 changes: 78 additions & 0 deletions framework/core/src/Extension/Console/ToggleExtensionCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?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\Extension\Console;

use Flarum\Console\AbstractCommand;
use Flarum\Extension\ExtensionManager;

class ToggleExtensionCommand extends AbstractCommand
{
/**
* @var ExtensionManager
*/
protected $extensionManager;

public function __construct(ExtensionManager $extensionManager)
{
$this->extensionManager = $extensionManager;

parent::__construct();
}

/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('extension:enable')
->setAliases(['extension:disable'])
->setDescription('Enable or disable an extension.')
->addArgument('extension-id', null, 'The ID of the extension to enable or disable.');
}

/**
* {@inheritdoc}
*/
protected function fire()
{
$name = $this->input->getArgument('extension-id');
$enabling = $this->input->getFirstArgument() === 'extension:enable';

if ($this->extensionManager->getExtension($name) === null) {
$this->error("There are no extensions by the ID of '$name'.");

return;
}

switch ($enabling) {
case true:
if ($this->extensionManager->isEnabled($name)) {
$this->info("The '$name' extension is already enabled.");

return;
} else {
$this->info("Enabling '$name' extension...");
$this->extensionManager->enable($name);
}
break;
case false:
if (! $this->extensionManager->isEnabled($name)) {
$this->info("The '$name' extension is already disabled.");

return;
} else {
$this->info("Disabling '$name' extension...");
$this->extensionManager->disable($name);
}
break;
}
}
}

0 comments on commit b4f3f05

Please sign in to comment.