Simplified console controller map

This commit is contained in:
Lucas Bartholemy 2020-09-02 18:56:25 +02:00
parent 6b41a60fa0
commit f6f81469fc
11 changed files with 48 additions and 46 deletions

View File

@ -9,6 +9,7 @@
namespace humhub\components;
use humhub\components\bootstrap\ModuleAutoLoader;
use humhub\components\console\Application as ConsoleApplication;
use humhub\libs\BaseSettingsManager;
use humhub\models\ModuleEnabled;
use Yii;
@ -99,10 +100,10 @@ class ModuleManager extends Component
/**
* Registers a module to the manager
* This is usually done by config.php in modules root folder.
* @see \humhub\components\bootstrap\ModuleAutoLoader::bootstrap
*
* @param array $configs
* @throws InvalidConfigException
* @see \humhub\components\bootstrap\ModuleAutoLoader::bootstrap
*
*/
public function registerBulk(array $configs)
{
@ -127,7 +128,7 @@ class ModuleManager extends Component
// Check mandatory config options
if (!isset($config['class']) || !isset($config['id'])) {
throw new InvalidConfigException('Module configuration requires an id and class attribute: '.$basePath);
throw new InvalidConfigException('Module configuration requires an id and class attribute: ' . $basePath);
}
$isCoreModule = (isset($config['isCoreModule']) && $config['isCoreModule']);
@ -196,6 +197,11 @@ class ModuleManager extends Component
}
}
}
// Register Console ControllerMap
if (Yii::$app instanceof ConsoleApplication && !(empty($config['consoleControllerMap']))) {
Yii::$app->controllerMap = ArrayHelper::merge(Yii::$app->controllerMap, $config['consoleControllerMap']);
}
}
/**
@ -228,7 +234,7 @@ class ModuleManager extends Component
if (isset($options['enabled']) && $options['enabled'] === true) {
if(!in_array($class, $this->coreModules) && !in_array($id, $this->enabledModules)) {
if (!in_array($class, $this->coreModules) && !in_array($id, $this->enabledModules)) {
continue;
}
}
@ -275,12 +281,12 @@ class ModuleManager extends Component
* Returns weather or not the given module id belongs to an core module.
*
* @return bool
* @since 1.3.8
* @throws Exception
* @since 1.3.8
*/
public function isCoreModule($id)
{
if(!$this->hasModule($id)) {
if (!$this->hasModule($id)) {
return false;
}

View File

@ -51,13 +51,4 @@ class Events extends BaseObject
}
}
/**
* @param $event \yii\base\Event
*/
public static function onConsoleApplicationInit($event)
{
$application = $event->sender;
$application->controllerMap['ldap'] = commands\LdapController::class;
}
}

View File

@ -15,10 +15,12 @@ return [
'id' => 'ldap',
'class' => \humhub\modules\ldap\Module::class,
'isCoreModule' => true,
'consoleControllerMap' => [
'ldap' => 'humhub\modules\ldap\commands\LdapController'
],
'events' => [
[AuthenticationMenu::class, AuthenticationMenu::EVENT_INIT, [Events::class, 'onAuthenticationMenu']],
[Collection::class, Collection::EVENT_BEFORE_CLIENTS_SET, [Events::class, 'onAuthClientCollectionSet']],
[Application::class, Application::EVENT_ON_INIT, [Events::class, 'onConsoleApplicationInit']],
]
];
?>

View File

@ -19,12 +19,28 @@ use yii\helpers\Console;
* HumHub Module Managament
*
* @property \humhub\modules\marketplace\Module $module
* @package humhub.modules_core.admin.console
* @since 0.5
*/
class MarketplaceController extends Controller
{
/**
* @inerhitdoc
*/
public function beforeAction($action)
{
/** @var \humhub\modules\marketplace\Module $module */
$module = Yii::$app->getModule('marketplace');
if ($module === null || !$module->enabled) {
print "Fatal: The module marketplace is disabled by configuration!\n\n";
return false;
}
return parent::beforeAction($action);
}
/**
* Lists all installed and enabled modules.
*

View File

@ -16,8 +16,10 @@ return [
'id' => 'marketplace',
'class' => Module::class,
'isCoreModule' => true,
'consoleControllerMap' => [
'module' => 'humhub\modules\marketplace\commands\MarketplaceController'
],
'events' => [
['humhub\components\console\Application', 'onInit', [Events::class, 'onConsoleApplicationInit']],
[ModuleMenu::class, ModuleMenu::EVENT_INIT, [Events::class, 'onAdminModuleMenuInit']],
[CronController::class, CronController::EVENT_ON_HOURLY_RUN, [Events::class, 'onHourlyCron']],
]

View File

@ -20,7 +20,6 @@ use yii\helpers\Console;
*/
class Events extends BaseObject
{
public static function onTopMenuRightInit($event)
{
$event->sender->addWidget(widgets\SearchMenu::class);
@ -35,11 +34,4 @@ class Events extends BaseObject
Yii::$app->search->optimize();
$controller->stdout('done.' . PHP_EOL, Console::FG_GREEN);
}
public static function onConsoleApplicationInit($event)
{
$application = $event->sender;
$application->controllerMap['search'] = commands\SearchController::class;
}
}

View File

@ -11,10 +11,12 @@ return [
'class' => \humhub\modules\search\Module::class,
'events' => [
['class' => TopMenuRightStack::class, 'event' => TopMenuRightStack::EVENT_INIT, 'callback' => [Events::class, 'onTopMenuRightInit']],
['class' => Application::class, 'event' => Application::EVENT_ON_INIT, 'callback' => [Events::class, 'onConsoleApplicationInit']],
['class' => CronController::class, 'event' => CronController::EVENT_ON_HOURLY_RUN, 'callback' => [Events::class, 'onHourlyCron']],
],
'consoleControllerMap' => [
'search' => '\humhub\modules\search\commands\SearchController'
],
'urlManagerRules' => [
'search' => 'search/search/index',
]
]
];

View File

@ -14,7 +14,7 @@ use humhub\modules\space\models\Membership;
use humhub\modules\space\helpers\MembershipHelper;
use Yii;
use yii\base\BaseObject;
use yii\web\HttpException;
use humhub\components\Event;
/**
* Events provides callbacks for all defined module events.
@ -27,7 +27,7 @@ class Events extends BaseObject
/**
* On rebuild of the search index, rebuild all space records
*
* @param type $event
* @param Event $event
*/
public static function onSearchRebuild($event)
{
@ -63,12 +63,6 @@ class Events extends BaseObject
}
}
public static function onConsoleApplicationInit($event)
{
$application = $event->sender;
$application->controllerMap['space'] = commands\SpaceController::class;
}
/**
* Callback to validate module database records.
*

View File

@ -19,10 +19,12 @@ return [
'class' => 'humhub\modules\space\modules\manage\Module'
],
],
'consoleControllerMap' => [
'space' => 'humhub\modules\space\commands\SpaceController'
],
'events' => [
[User::class, User::EVENT_BEFORE_SOFT_DELETE, [Events::class, 'onUserSoftDelete']],
[Search::class, Search::EVENT_ON_REBUILD, [Events::class, 'onSearchRebuild']],
[Application::class, Application::EVENT_ON_INIT, [Events::class, 'onConsoleApplicationInit']],
[IntegrityController::class, IntegrityController::EVENT_ON_RUN, [Events::class, 'onIntegrityCheck']],
],
];

View File

@ -15,10 +15,5 @@ namespace humhub\modules\ui;
*/
class Events
{
public static function onConsoleApplicationInit($event)
{
$application = $event->sender;
$application->controllerMap['theme'] = commands\ThemeController::class;
}
}

View File

@ -6,8 +6,8 @@ use humhub\components\console\Application;
return [
'id' => 'ui',
'class' => \humhub\modules\ui\Module::class,
'isCoreModule' => true,
'events' => [
[Application::class, Application::EVENT_ON_INIT, ['humhub\modules\ui\Events', 'onConsoleApplicationInit']],
]
'consoleControllerMap' => [
'theme' => '\humhub\modules\ui\commands\ThemeController'
],
'isCoreModule' => true
];