DevelEntityUpdatesCommands.php in Devel Entity Updates 3.x
File
src/Commands/DevelEntityUpdatesCommands.php
View source
<?php
namespace Drupal\devel_entity_updates\Commands;
use Consolidation\SiteAlias\SiteAliasManagerAwareInterface;
use Consolidation\SiteAlias\SiteAliasManagerAwareTrait;
use Drupal\Core\DependencyInjection\ClassResolverInterface;
use Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface;
use Drupal\devel_entity_updates\DevelEntityDefinitionUpdateManager;
use Drush\Commands\DrushCommands;
use Drush\Drush;
use Drush\Exceptions\UserAbortException;
class DevelEntityUpdatesCommands extends DrushCommands implements SiteAliasManagerAwareInterface {
use SiteAliasManagerAwareTrait;
protected $classResolver;
protected $entityDefinitionUpdateManager;
public function __construct(ClassResolverInterface $class_resolver, EntityDefinitionUpdateManagerInterface $entity_definition_update_manager) {
parent::__construct();
$this->classResolver = $class_resolver;
$this->entityDefinitionUpdateManager = $entity_definition_update_manager;
}
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.'));
}
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;
}
public function doLegacyEntityUpdates($options = [
'cache-clear' => TRUE,
]) {
$this
->entityUpdates($options);
}
}