You are here

public function PurgerCommands::purgerAdd in Purge 8.3

Create a new purger instance.

@option if-not-exists Don't create a new purger if one of this type exists. @usage drush p:purger-add ID Add a purger of type ID. @usage drush p:purger-add --if-not-exists ID Create purger ID if it does not exist.

@command p:purger-add @aliases ppadd,p-purger-add

Parameters

string $id: The plugin ID of the purger instance to create.

array $options: Associative array of options whose values come from Drush.

File

modules/purge_drush/src/Commands/PurgerCommands.php, line 50

Class

PurgerCommands
Configure Purge Purgers from the command line.

Namespace

Drupal\purge_drush\Commands

Code

public function purgerAdd($id, array $options = [
  'format' => 'string',
  'if-not-exists' => FALSE,
]) {
  $enabled = $this->purgePurgers
    ->getPluginsEnabled();

  // Verify that the plugin exists.
  if (!isset($this->purgePurgers
    ->getPlugins()[$id])) {
    throw new \Exception(dt('The given plugin does not exist!'));
  }

  // When --if-not-exists is passed, we cancel creating double purgers.
  if ($options['if-not-exists']) {
    if (in_array($id, $enabled)) {
      if ($options['format'] == 'string') {
        $this
          ->io()
          ->success(dt('The purger already exists!'));
      }
      return;
    }
  }

  // Verify that new instances of the plugin may be created.
  if (!in_array($id, $this->purgePurgers
    ->getPluginsAvailable())) {
    throw new \Exception(dt('No more instances of this plugin can be created!'));
  }

  // Define the new instance and store it.
  $enabled[$this->purgePurgers
    ->createId()] = $id;
  $this->purgePurgers
    ->setPluginsEnabled($enabled);
  if ($options['format'] == 'string') {
    $this
      ->io()
      ->success(dt('The purger has been created!'));
  }
}