ModuleHelper
Static utility class for conditional module integrations. Allows modules to execute code only when another module is loaded, without creating hard dependencies.
Usage
php
use App\Core\Module\ModuleHelper;
ModuleHelper::when('Logger', function () {
CmsLog::info('MyModule', 'my.action', 'Something happened.');
});Methods
when()
Executes a callback if the specified module is currently loaded.
php
ModuleHelper::when(string $moduleName, callable $callback): voidphp
ModuleHelper::when('Logger', function () use ($user) {
CmsLog::info('Auth', 'user.created', "User '{$user->name}' created.", [], $user);
});unless()
Executes a callback if the specified module is not loaded.
php
ModuleHelper::unless(string $moduleName, callable $callback): voidphp
ModuleHelper::unless('Permissions', function () {
// Fallback when Permissions module is disabled
});has()
Returns true if the specified module is currently loaded.
php
ModuleHelper::has(string $moduleName): boolphp
if (ModuleHelper::has('PageBuilder')) {
$registry = app(BlockRegistry::class);
$registry->register(MyBlock::class);
}Notes
- Module names are case-sensitive and must match the
namefield in the module'smodule.json ModuleHelperresolves theModuleManagerfrom the service container on every call- It only reflects the state of modules at the time of the call — a module toggled at runtime will be reflected immediately