You are here

public static function Rules::export in Hook Update Deploy Tools 8

Same name and namespace in other branches
  1. 7 src/Rules.php \HookUpdateDeployTools\Rules::export()

Exports a single Rule when called (typically called from Drush).

Parameters

string $rule_machine_name: The machine name of the Rule to export.

Return value

string The URI of the item exported, or a failure message.

Overrides ExportInterface::export

File

src/Rules.php, line 144

Class

Rules
Public method for importing Rules.

Namespace

HookUpdateDeployTools

Code

public static function export($rule_machine_name) {
  $t = get_t();
  try {
    Check::notEmpty('rule_machine_name', $rule_machine_name, TRUE);
    $msg_return = '';
    $path = HudtInternal::getStoragePath('rules');
    $machine_name = HudtInternal::normalizeMachineName($rule_machine_name);
    $file_name = HudtInternal::normalizeFileName($rule_machine_name);
    $file_uri = DRUPAL_ROOT . '/' . $path . $file_name;
    Check::canUse('rules');
    Check::canCall('rules_config_load');

    // Load the rule if it exists.
    $rule = rules_config_load($machine_name);
    if (!empty($rule)) {

      // It exists, so export it.
      $export_contents = $rule
        ->export();

      // Save the file.
      $msg_return = HudtInternal::writeFile($file_uri, $export_contents);
    }
    else {

      // Could not be loaded, so nothing to export.  Error gracefully.
      $vars = array(
        '@machine_name' => $machine_name,
      );
      $msg_error = $t("The Rule '@machine_name' could not be loaded.  Please check the spelling of the machine name you are trying to export", $vars);
      $msg_return = $t('ERROR') . ': ';
      $msg_return .= $t("Rule not found.  Check the spelling of the machine name you are trying to export");
    }
  } catch (\Exception $e) {

    // Any errors from this command do not need to be watchdog logged.
    $e->logIt = FALSE;
    $vars = array(
      '!error' => method_exists($e, 'logMessage') ? $e
        ->logMessage() : $e
        ->getMessage(),
    );
    $msg_error = $t("Caught exception:  !error", $vars);
  }
  if (!empty($msg_error)) {
    drush_log($msg_error, 'error');
  }
  return !empty($msg_return) ? $msg_return : $msg_error;
}