Skip to content

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).

  1. Create the activator class.

    app/Support/EnvironmentActivator.php
    <?php
    namespace 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));
    }
    }
  2. Publish config/modules.php (if you have not already), register your activator under activators, and select it in activator.

    Terminal window
    php artisan vendor:publish --provider="Nwidart\Modules\LaravelModulesServiceProvider" --tag=config
    config/modules.php
    'activators' => [
    'file' => [
    'class' => FileActivator::class,
    'statuses-file' => base_path('modules_statuses.json'),
    ],
    'environment' => [
    'class' => \App\Support\EnvironmentActivator::class,
    ],
    ],
    'activator' => 'environment',
  3. Seed the new status file. Without entries, every module starts disabled. Copy your existing file for the current APP_ENV (here local), or run php artisan module:enable --all:

    Terminal window
    cp modules_statuses.json modules_statuses.local.json
  4. Check the result.

    Terminal window
    php artisan module:disable Blog
    php artisan module:list
    cat modules_statuses.local.json

    module:list shows Blog as [Disabled], modules_statuses.local.json contains "Blog": false, and modules_statuses.json stays unchanged.

An unknown activator key, a missing class key, or a nonexistent class throws InvalidActivatorClass during registration.