You are here

public function FeaturesCommands::export in Features 8.4

Same name and namespace in other branches
  1. 8.3 src/Commands/FeaturesCommands.php \Drupal\features\Commands\FeaturesCommands::export()

Export the configuration on your site into a custom module.

@command features:export

@option add-profile Package features into an install profile. @option bundle Use a specific bundle namespace.

@usage drush features-export Export all available packages. @usage drush features-export example_article example_page Export the example_article and example_page packages. @usage drush features-export --add-profile Export all available packages and add them to an install profile.

@aliases fex,fu,fua,fu-all,features-export

Parameters

array $packages: A list of features to export.

Throws

\Drupal\features\Exception\DomainException

\Drupal\features\Exception\InvalidArgumentException

\Drush\Exceptions\UserAbortException

\Exception

File

src/Commands/FeaturesCommands.php, line 362

Class

FeaturesCommands
Drush commands for Features.

Namespace

Drupal\features\Commands

Code

public function export(array $packages, $options = self::OPTIONS_EXPORT) {
  $assigner = $this
    ->featuresOptions($options);
  $manager = $this->manager;
  $generator = $this->generator;
  $current_bundle = $assigner
    ->getBundle();
  if ($options['add-profile']) {
    if ($current_bundle->isDefault) {
      throw new InvalidArgumentException(dt("Must specify a profile name with --name"));
    }
    $current_bundle
      ->setIsProfile(TRUE);
  }
  $all_packages = $manager
    ->getPackages();
  foreach ($packages as $name) {
    if (!isset($all_packages[$name])) {
      throw new DomainException(dt("The package @name does not exist.", [
        '@name' => $name,
      ]));
    }
  }
  if (empty($packages)) {
    $packages = $all_packages;
    $dt_args = [
      '@modules' => implode(', ', array_keys($packages)),
    ];
    $this
      ->output()
      ->writeln(dt('The following extensions will be exported: @modules', $dt_args));
    if (!$this
      ->io()
      ->confirm('Do you really want to continue?')) {
      throw new UserAbortException();
    }
  }
  else {
    $packages = array_combine($packages, $packages);
  }

  // If any packages exist, confirm before overwriting.
  if ($existing_packages = $manager
    ->listPackageDirectories($packages, $current_bundle)) {
    foreach ($existing_packages as $name => $directory) {
      $this
        ->output()
        ->writeln(dt("The extension @name already exists at @directory.", [
        '@name' => $name,
        '@directory' => $directory,
      ]));
    }

    // Apparently, format_plural is not always available.
    if (count($existing_packages) == 1) {
      $message = dt('Would you like to overwrite it?');
    }
    else {
      $message = dt('Would you like to overwrite them?');
    }
    if (!$this
      ->io()
      ->confirm($message)) {
      throw new UserAbortException();
    }
  }

  // Use the write generation method.
  $method_id = FeaturesGenerationWrite::METHOD_ID;
  $result = $generator
    ->generatePackages($method_id, $current_bundle, array_keys($packages));
  foreach ($result as $message) {
    $method = $message['success'] ? 'success' : 'error';
    $this
      ->logger()
      ->{$method}(dt($message['message'], $message['variables']));
  }
}