You are here

class DevelEntityUpdatesCommands in Devel Entity Updates 8.2

Same name and namespace in other branches
  1. 8 src/Commands/DevelEntityUpdatesCommands.php \Drupal\devel_entity_updates\Commands\DevelEntityUpdatesCommands
  2. 3.x src/Commands/DevelEntityUpdatesCommands.php \Drupal\devel_entity_updates\Commands\DevelEntityUpdatesCommands
  3. 3.0.x src/Commands/DevelEntityUpdatesCommands.php \Drupal\devel_entity_updates\Commands\DevelEntityUpdatesCommands

Drush9 commands definitions.

Hierarchy

  • class \Drupal\devel_entity_updates\Commands\DevelEntityUpdatesCommands extends \Drush\Commands\DrushCommands implements \Consolidation\SiteAlias\SiteAliasManagerAwareInterface uses \Consolidation\SiteAlias\SiteAliasManagerAwareTrait

Expanded class hierarchy of DevelEntityUpdatesCommands

1 string reference to 'DevelEntityUpdatesCommands'
drush.services.yml in ./drush.services.yml
drush.services.yml
1 service uses DevelEntityUpdatesCommands
devel_entity_updates.command in ./drush.services.yml
Drupal\devel_entity_updates\Commands\DevelEntityUpdatesCommands

File

src/Commands/DevelEntityUpdatesCommands.php, line 16

Namespace

Drupal\devel_entity_updates\Commands
View source
class DevelEntityUpdatesCommands extends DrushCommands implements SiteAliasManagerAwareInterface {
  use SiteAliasManagerAwareTrait;

  /**
   * The class resolver.
   *
   * @var \Drupal\Core\DependencyInjection\ClassResolverInterface
   */
  protected $classResolver;

  /**
   * The entity definition update manager.
   *
   * @var \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface
   */
  protected $entityDefinitionUpdateManager;

  /**
   * DevelEntityUpdatesCommands constructor.
   *
   * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver
   *   The class resolver.
   * @param \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface $entity_definition_update_manager
   *   The entity definition update manager.
   */
  public function __construct(ClassResolverInterface $class_resolver, EntityDefinitionUpdateManagerInterface $entity_definition_update_manager) {
    parent::__construct();
    $this->classResolver = $class_resolver;
    $this->entityDefinitionUpdateManager = $entity_definition_update_manager;
  }

  /**
   * Apply pending entity schema updates.
   *
   * @command devel-entity-updates
   * @aliases dentup, entup, entity-updates
   * @option cache-clear Set to 0 to suppress normal cache clearing; the caller should then clear if needed.
   * @bootstrap full
   *
   * @param array $options
   *   Array of options.
   */
  public function entityUpdates($options = [
    'cache-clear' => TRUE,
  ]) {
    if (Drush::simulate()) {
      throw new \Exception(dt('entity-updates command does not support --simulate option.'));
    }
    if ($this
      ->doEntityUpdates() === FALSE) {
      return;
    }
    if (!empty($options['cache-clear'])) {
      $process = Drush::drush($this
        ->siteAliasManager()
        ->getSelf(), 'cache-rebuild');
      $process
        ->mustrun();
    }
    $this
      ->logger()
      ->success(dt('Finished performing updates.'));
  }

  /**
   * Actually performs entity schema updates.
   *
   * @return bool
   *   TRUE if updates were applied, FALSE otherwise.
   */
  protected function doEntityUpdates() {
    $result = TRUE;
    $change_summary = $this->entityDefinitionUpdateManager
      ->getChangeSummary();
    if (!empty($change_summary)) {
      $this
        ->output()
        ->writeln(dt('The following updates are pending:'));
      $this
        ->io()
        ->newLine();
      foreach ($change_summary as $entity_type_id => $changes) {
        $this
          ->output()
          ->writeln($entity_type_id . ' entity type : ');
        foreach ($changes as $change) {
          $this
            ->output()
            ->writeln(strip_tags($change), 2);
        }
      }
      if (!$this
        ->io()
        ->confirm(dt('Do you wish to run all pending updates?'))) {
        throw new UserAbortException();
      }
      $this->classResolver
        ->getInstanceFromDefinition(DevelEntityDefinitionUpdateManager::class)
        ->applyUpdates();
    }
    else {
      $this
        ->logger()
        ->success(dt("No entity schema updates required"));
      $result = FALSE;
    }
    return $result;
  }

  /**
   * Replaces the "entity-updates" command.
   *
   * @hook replace-command entity:updates
   */
  public function doLegacyEntityUpdates($options = [
    'cache-clear' => TRUE,
  ]) {
    $this
      ->entityUpdates($options);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DevelEntityUpdatesCommands::$classResolver protected property The class resolver.
DevelEntityUpdatesCommands::$entityDefinitionUpdateManager protected property The entity definition update manager.
DevelEntityUpdatesCommands::doEntityUpdates protected function Actually performs entity schema updates.
DevelEntityUpdatesCommands::doLegacyEntityUpdates public function Replaces the "entity-updates" command.
DevelEntityUpdatesCommands::entityUpdates public function Apply pending entity schema updates.
DevelEntityUpdatesCommands::__construct public function DevelEntityUpdatesCommands constructor.