path_redirect_import-8.x-1.0-beta4/src/Drush/Commands/PathRedirectImportCommands.php

src/Drush/Commands/PathRedirectImportCommands.php
<?php

namespace Drupal\path_redirect_import\Drush\Commands;

use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Batch\BatchBuilder;
use Drupal\Core\Datetime\DateFormatter;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\File\FileExists;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationManager;
use Drupal\migrate\Plugin\MigrationPluginManager;
use Drupal\migrate_tools\Drush\Commands\MigrateToolsCommands;
use Drupal\migrate_tools\EventSubscriber\MigrationDrushCommandProgress;
use Drupal\path_redirect_import\Form\MigrateRedirectForm;
use Drupal\path_redirect_import\RedirectExport;
use Drush\Attributes as CLI;
use Symfony\Component\DependencyInjection\ContainerInterface;

// cspell:ignore commandfile prii idlist prie prie

/**
 * A Drush commandfile.
 *
 * In addition to this file, you need a drush.services.yml
 * in root of your module, and a composer.json file that provides the name
 * of the services file to use.
 *
 * See these files for an example of injecting Drupal services:
 *   - http://cgit.drupalcode.org/devel/tree/src/Commands/DevelCommands.php
 *   - http://cgit.drupalcode.org/devel/tree/drush.services.yml
 */
class PathRedirectImportCommands extends MigrateToolsCommands {

  use DependencySerializationTrait;
  use StringTranslationTrait;

  public function __construct(
    MigrationPluginManager $migrationPluginManager,
    DateFormatter $dateFormatter,
    EntityTypeManagerInterface $entityTypeManager,
    KeyValueFactoryInterface $keyValue,
    TimeInterface $time,
    TranslationManager $translation,
    MigrationDrushCommandProgress $commandProgress,
    protected FileSystemInterface $fileSystem,
    protected RedirectExport $redirectExport,
  ) {
    parent::__construct(
      $migrationPluginManager,
      $dateFormatter,
      $entityTypeManager,
      $keyValue,
      $time,
      $translation,
      $commandProgress,
    );
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container): static {
    return new static(
      $container->get('plugin.manager.migration'),
      $container->get('date.formatter'),
      $container->get('entity_type.manager'),
      $container->get('keyvalue'),
      $container->get('datetime.time'),
      $container->get('string_translation'),
      $container->get('migrate_tools.migration_drush_command_progress'),
      $container->get('file_system'),
      $container->get('path_redirect_import.redirect_export'),
    );
  }

  /**
   * Imports the redirects defined in the CSV file passed as argument.
   *
   * @param string $file
   *   The CSV file to import.
   *
   * @command path_redirect_import:import
   * @aliases prii
   */
  #[CLI\Command(name: 'path_redirect_import:import', aliases: ['prii'])]
  #[CLI\Argument(name: 'file', description: 'The CSV file to import.')]
  #[CLI\Topics(topics: ['path_redirect_import'])]
  #[CLI\ValidateModulesEnabled(modules: ['migrate_tools'])]
  public function importRedirects($file) {
    if (!file_exists($file)) {
      $this->logger()->error("File $file doesn't exist \n");
      exit;
    }

    $directory = dirname(MigrateRedirectForm::MIGRATE_FILE_PATH);
    $this->fileSystem->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
    $this->fileSystem->copy($file, MigrateRedirectForm::MIGRATE_FILE_PATH, FileExists::Replace);

    $this->resetStatus('path_redirect_import');

    $this->import('path_redirect_import', [
      'limit' => 0,
      'update' => TRUE,
      'force' => FALSE,
      'all' => FALSE,
      'group' => NULL,
      'tag' => NULL,
      'execute-dependencies' => FALSE,
      'sync' => FALSE,
      'skip-progress-bar' => FALSE,
      'continue-on-failure' => FALSE,
      'idlist' => NULL,
    ]);

    $this->logger()->success($this->t('Redirects imported.'));
  }

  /**
   * Exports the redirects.
   *
   * @command path_redirect_import:export
   * @aliases prie
   */
  #[CLI\Command(name: 'path_redirect_import:export', aliases: ['prie'])]
  #[CLI\Topics(topics: ['path_redirect_import'])]
  #[CLI\ValidateModulesEnabled(modules: ['migrate_tools'])]
  public function exportRedirects() {
    $operations = $this->redirectExport->getBatchOperations();

    $batchBuilder = new BatchBuilder();
    array_walk($operations, fn($operation) => $batchBuilder->addOperation($operation[0], $operation[1]));

    $batchBuilder
      ->setTitle($this->t('Exporting redirect entities to file'))
      ->setFinishCallback(RedirectExport::class . '::batchFinishedExport')
      ->setErrorMessage($this->t('An error occurred while exporting redirect entities.'));
    // 5. Add batch operations as new batch sets.
    batch_set($batchBuilder->toArray());
    // 6. Process the batch sets.
    drush_backend_batch_process();
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc