You are here

class ExportCommand in Commerce Demo 8

Same name and namespace in other branches
  1. 8.2 src/Command/ExportCommand.php \Drupal\commerce_demo\Command\ExportCommand

Class ExportCommand.

@package Drupal\commerce_demo

@DrupalCommand ( extension="commerce_demo", extensionType="module" )

Hierarchy

  • class \Drupal\commerce_demo\Command\ExportCommand extends \Symfony\Component\Console\Command\Command uses \Drupal\Console\Core\Command\Shared\CommandTrait

Expanded class hierarchy of ExportCommand

1 string reference to 'ExportCommand'
console.services.yml in ./console.services.yml
console.services.yml
1 service uses ExportCommand
commerce_demo.export_command in ./console.services.yml
Drupal\commerce_demo\Command\ExportCommand

File

src/Command/ExportCommand.php, line 34

Namespace

Drupal\commerce_demo\Command
View source
class ExportCommand extends Command {
  use CommandTrait;

  /**
   * The content exporter.
   *
   * @var \Drupal\commerce_demo\ContentExporter
   */
  protected $contentExporter;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The entity type bundle info.
   *
   * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
   */
  protected $entityTypeBundleInfo;

  /**
   * Constructs a new ExportCommand object.
   *
   * @param \Drupal\commerce_demo\ContentExporter $content_exporter
   *   The content exporter.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
   *   The entity type bundle info.
   */
  public function __construct(ContentExporter $content_exporter, EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info) {
    parent::__construct();
    $this->contentExporter = $content_exporter;
    $this->entityTypeManager = $entity_type_manager;
    $this->entityTypeBundleInfo = $entity_type_bundle_info;
  }

  /**
   * {@inheritdoc}
   */
  protected function configure() {
    $this
      ->setName('commerce_demo:export')
      ->setDescription($this
      ->trans('commands.commerce_demo.export.description'))
      ->addOption('directory', '', InputOption::VALUE_OPTIONAL, $this
      ->trans('commands.commerce_demo.export.options.directory'))
      ->addArgument('entity_type', InputArgument::REQUIRED, $this
      ->trans('commands.commerce_demo.export.arguments.entity_type'))
      ->addArgument('bundle', InputArgument::REQUIRED, $this
      ->trans('commands.commerce_demo.export.arguments.bundle'));
  }

  /**
   * {@inheritdoc}
   */
  protected function execute(InputInterface $input, OutputInterface $output) {
    $entity_type_id = $input
      ->getArgument('entity_type');
    $bundle = $input
      ->getArgument('bundle');
    $directory = $input
      ->getOption('directory');
    if ($directory && strpos($directory, -1, 1) != '/') {
      $directory .= '/';
    }

    // Add the bundle to the filename only if the entity type has one.
    $filename = $entity_type_id;
    $entity_type = $this->entityTypeManager
      ->getDefinition($entity_type_id);
    if ($entity_type
      ->getKey('bundle')) {
      $filename .= '.' . $bundle;
    }
    $destination = $directory . $filename . '.yml';
    $export = $this->contentExporter
      ->exportAll($entity_type_id, $bundle);
    file_put_contents($destination, Yaml::encode($export));
    $io = new DrupalStyle($input, $output);
    $io
      ->writeln(sprintf($this
      ->trans('commands.commerce_demo.export.messages.success'), $destination));
  }

  /**
   * {@inheritdoc}
   */
  protected function interact(InputInterface $input, OutputInterface $output) {
    $helper = $this
      ->getHelper('question');
    $entity_types = $this->entityTypeManager
      ->getDefinitions();
    $entity_types = array_filter($entity_types, function (EntityType $entity_type) {
      return $entity_type
        ->entityClassImplements(ContentEntityInterface::class);
    });
    $entity_types = array_map(function (EntityType $entity_type) {
      return $entity_type
        ->getLabel();
    }, $entity_types);

    // --entity_type argument.
    $entity_type_id = $input
      ->getArgument('entity_type');
    if (!$entity_type_id) {
      $question = new ChoiceQuestion($this
        ->trans('commands.commerce_demo.export.questions.entity_type'), $entity_types);
      $entity_type_id = $helper
        ->ask($input, $output, $question);
    }
    $input
      ->setArgument('entity_type', $entity_type_id);

    // --bundle argument.
    $bundles = $this->entityTypeBundleInfo
      ->getBundleInfo($entity_type_id);
    $bundles = array_map(function ($bundle) {
      return $bundle['label'];
    }, $bundles);
    if (count($bundles) === 1) {
      $bundle_keys = array_keys($bundles);
      $bundle = reset($bundle_keys);
    }
    else {
      $bundle = $input
        ->getArgument('bundle');
      if (!$bundle) {
        $question = new ChoiceQuestion($this
          ->trans('commands.commerce_demo.export.questions.bundle'), $bundles);
        $bundle = $helper
          ->ask($input, $output, $question);
      }
    }
    $input
      ->setArgument('bundle', $bundle);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ExportCommand::$contentExporter protected property The content exporter.
ExportCommand::$entityTypeBundleInfo protected property The entity type bundle info.
ExportCommand::$entityTypeManager protected property The entity type manager.
ExportCommand::configure protected function
ExportCommand::execute protected function
ExportCommand::interact protected function
ExportCommand::__construct public function Constructs a new ExportCommand object.