Security update for Core, with self-updated composer
[yaffs-website] / vendor / drupal / console / src / Command / Entity / DeleteCommand.php
index 895db67d6f352618ea91ca407e15c239910fe905..12ca54d0065aaff9bcf8c2219ac687b130f7313e 100644 (file)
@@ -8,17 +8,14 @@ namespace Drupal\Console\Command\Entity;
 
 use Symfony\Component\Console\Input\InputArgument;
 use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\InputOption;
 use Symfony\Component\Console\Output\OutputInterface;
-use Symfony\Component\Console\Command\Command;
+use Drupal\Console\Core\Command\Command;
 use Drupal\Core\Entity\EntityTypeRepository;
 use Drupal\Core\Entity\EntityTypeManagerInterface;
-use Drupal\Console\Core\Command\Shared\CommandTrait;
-use Drupal\Console\Core\Style\DrupalStyle;
 
 class DeleteCommand extends Command
 {
-    use CommandTrait;
-
     /**
      * @var EntityTypeRepository
      */
@@ -51,6 +48,12 @@ class DeleteCommand extends Command
         $this
             ->setName('entity:delete')
             ->setDescription($this->trans('commands.entity.delete.description'))
+            ->addOption(
+                'all',
+                null,
+                InputOption::VALUE_NONE,
+                $this->trans('commands.entity.delete.options.all')
+            )
             ->addArgument(
                 'entity-definition-id',
                 InputArgument::REQUIRED,
@@ -60,7 +63,7 @@ class DeleteCommand extends Command
                 'entity-id',
                 InputArgument::REQUIRED,
                 $this->trans('commands.entity.delete.arguments.entity-id')
-            );
+            )->setAliases(['ed']);
     }
 
     /**
@@ -68,19 +71,19 @@ class DeleteCommand extends Command
      */
     protected function interact(InputInterface $input, OutputInterface $output)
     {
-        $io = new DrupalStyle($input, $output);
         $entityDefinitionID = $input->getArgument('entity-definition-id');
         $entityID = $input->getArgument('entity-id');
+        $all = $input->getOption('all');
 
         if (!$entityDefinitionID) {
             $entityTypes = $this->entityTypeRepository->getEntityTypeLabels(true);
 
-            $entityType = $io->choice(
+            $entityType = $this->getIo()->choice(
                 $this->trans('commands.entity.delete.questions.entity-type'),
                 array_keys($entityTypes)
             );
 
-            $entityDefinitionID = $io->choice(
+            $entityDefinitionID = $this->getIo()->choice(
                 $this->trans('commands.entity.delete.questions.entity-definition-id'),
                 array_keys($entityTypes[$entityType])
             );
@@ -88,8 +91,10 @@ class DeleteCommand extends Command
             $input->setArgument('entity-definition-id', $entityDefinitionID);
         }
 
-        if (!$entityID) {
-            $entityID = $io->ask(
+        if ($all) {
+            $input->setArgument('entity-id', '-');
+        } elseif (!$entityID) {
+            $entityID = $this->getIo()->ask(
                 $this->trans('commands.entity.delete.questions.entity-id')
             );
             $input->setArgument('entity-id', $entityID);
@@ -101,25 +106,45 @@ class DeleteCommand extends Command
      */
     protected function execute(InputInterface $input, OutputInterface $output)
     {
-        $io = new DrupalStyle($input, $output);
-
         $entityDefinitionID = $input->getArgument('entity-definition-id');
-        $entityID = $input->getArgument('entity-id');
 
         try {
-            $this->entityTypeManager->getStorage($entityDefinitionID)->load($entityID)->delete();
+            $storage = $this->entityTypeManager->getStorage($entityDefinitionID);
+
+            if ($input->getOption('all')) {
+                $entities = $storage->loadMultiple();
+                if ($this->getIo()->confirm(
+                    sprintf(
+                        $this->trans('commands.entity.delete.messages.confirm-delete-all'),
+                        $entityDefinitionID,
+                        count($entities)
+                    )
+                )
+                ) {
+                    $storage->delete($entities);
+                    $this->getIo()->success(
+                        sprintf(
+                            $this->trans('commands.entity.delete.messages.deleted-all'),
+                            $entityDefinitionID,
+                            count($entities)
+                        )
+                    );
+                }
+            } else {
+                $entityID = $input->getArgument('entity-id');
+                $storage->load($entityID)->delete();
+                $this->getIo()->success(
+                    sprintf(
+                        $this->trans('commands.entity.delete.messages.deleted'),
+                        $entityDefinitionID,
+                        $entityID
+                    )
+                );
+            }
         } catch (\Exception $e) {
-            $io->error($e->getMessage());
+            $this->getIo()->error($e->getMessage());
 
             return 1;
         }
-
-        $io->success(
-            sprintf(
-                $this->trans('commands.entity.delete.messages.deleted'),
-                $entityDefinitionID,
-                $entityID
-            )
-        );
     }
 }