View source  
  <?php
namespace Drupal\backup_migrate\Core\Filter;
use Drupal\backup_migrate\Core\Config\Config;
use Drupal\backup_migrate\Core\Plugin\FileProcessorInterface;
use Drupal\backup_migrate\Core\Plugin\FileProcessorTrait;
use Drupal\backup_migrate\Core\Plugin\PluginBase;
use Drupal\backup_migrate\Core\File\BackupFileReadableInterface;
class FileNamer extends PluginBase implements FileProcessorInterface {
  use FileProcessorTrait;
  
  public function configSchema(array $params = []) {
    $schema = [];
    if (\Drupal::moduleHandler()
      ->moduleExists('token')) {
      $must_match = '/^[\\w\\-_:\\[\\]]+$/';
      $must_match_err = $this
        ->t('%title must contain only letters, numbers, dashes (-) and underscores (_). And Site Tokens.');
    }
    else {
      $must_match = '/^[\\w\\-_:]+$/';
      $must_match_err = $this
        ->t('%title must contain only letters, numbers, dashes (-) and underscores (_).');
    }
    
    if ($params['operation'] == 'backup') {
      $schema['groups']['file'] = [
        'title' => 'Backup File',
      ];
      $schema['fields']['filename'] = [
        'group' => 'file',
        'type' => 'text',
        'title' => 'File Name',
        'must_match' => $must_match,
        'must_match_error' => $must_match_err,
        'min_length' => 1,
        
        'max_length' => 200,
        'required' => TRUE,
      ];
      $schema['fields']['timestamp'] = [
        'group' => 'file',
        'type' => 'boolean',
        'title' => 'Append a timestamp',
      ];
      $schema['fields']['timestamp_format'] = [
        'group' => 'file',
        'type' => 'text',
        'title' => 'Timestamp Format',
        'max_length' => 32,
        'dependencies' => [
          'timestamp' => TRUE,
        ],
        'description' => $this
          ->t('Use <a href="http://php.net/date">PHP Date formatting</a>.'),
      ];
    }
    return $schema;
  }
  
  public function configDefaults() {
    return new Config([
      'filename' => 'backup',
      'timestamp' => TRUE,
      'timestamp_format' => 'Y-m-d\\TH-i-s',
    ]);
  }
  
  public function supportedOps() {
    return [
      'afterBackup' => [],
    ];
  }
  
  public function afterBackup(BackupFileReadableInterface $file) {
    if (\Drupal::moduleHandler()
      ->moduleExists('token')) {
      $token = \Drupal::token();
      $name = $token
        ->replace($this
        ->confGet('filename'));
    }
    else {
      $name = $this
        ->confGet('filename');
    }
    if ($this
      ->confGet('timestamp')) {
      $name .= '-' . date($this
        ->confGet('timestamp_format'));
    }
    $file
      ->setName($name);
    return $file;
  }
}