Store module status with a custom activator
To store module statuses somewhere other than modules_statuses.json, implement ActivatorInterface and select your class in config/modules.php. This guide builds an activator that stores one JSON file per environment (modules_statuses.local.json, modules_statuses.production.json, and so on).
The package constructs the activator via new $class($app) during registration, before the application boots. Your activator can use the container and filesystem, but not services that require a booted application (such as Eloquent or the database).
-
Create the activator class.
app/Support/EnvironmentActivator.php <?phpnamespace App\Support;use Illuminate\Contracts\Foundation\Application;use Nwidart\Modules\Contracts\ActivatorInterface;use Nwidart\Modules\Module;class EnvironmentActivator implements ActivatorInterface{private string $file;/** @var array<string, bool> */private array $statuses = [];public function __construct(Application $app){$this->file = $app->basePath('modules_statuses.'.$app->environment().'.json');if (is_file($this->file)) {$this->statuses = json_decode(file_get_contents($this->file), true) ?: [];}}public function enable(Module $module): void{$this->setActiveByName($module->getName(), true);}public function disable(Module $module): void{$this->setActiveByName($module->getName(), false);}public function hasStatus(Module|string $module, bool $status): bool{$name = $module instanceof Module ? $module->getName() : $module;// A module without an entry counts as disabled.return ($this->statuses[$name] ?? false) === $status;}public function setActive(Module $module, bool $active): void{$this->setActiveByName($module->getName(), $active);}public function setActiveByName(string $name, bool $active): void{$this->statuses[$name] = $active;$this->save();}public function delete(Module $module): void{unset($this->statuses[$module->getName()]);$this->save();}public function reset(): void{$this->statuses = [];if (is_file($this->file)) {unlink($this->file);}}private function save(): void{file_put_contents($this->file, json_encode($this->statuses, JSON_PRETTY_PRINT));}} -
Publish
config/modules.php(if you have not already), register your activator underactivators, and select it inactivator.Terminal window php artisan vendor:publish --provider="Nwidart\Modules\LaravelModulesServiceProvider" --tag=configconfig/modules.php 'activators' => ['file' => ['class' => FileActivator::class,'statuses-file' => base_path('modules_statuses.json'),],'environment' => ['class' => \App\Support\EnvironmentActivator::class,],],'activator' => 'environment', -
Seed the new status file. Without entries, every module starts disabled. Copy your existing file for the current
APP_ENV(herelocal), or runphp artisan module:enable --all:Terminal window cp modules_statuses.json modules_statuses.local.json -
Check the result.
Terminal window php artisan module:disable Blogphp artisan module:listcat modules_statuses.local.jsonmodule:listshowsBlogas[Disabled],modules_statuses.local.jsoncontains"Blog": false, andmodules_statuses.jsonstays unchanged.
An unknown activator key, a missing class key, or a nonexistent class throws InvalidActivatorClass during registration.
Related
Section titled “Related”activatorandactivatorsconfiguration keysActivatorInterfaceandFileActivatorInvalidActivatorClass- Enable and disable modules
- Module status and activators: when a custom activator is worth writing