FileExcludeFilter.php in Backup and Migrate 5.0.x
File
src/Core/Filter/FileExcludeFilter.php
View source
<?php
namespace Drupal\backup_migrate\Core\Filter;
use Drupal\backup_migrate\Core\Config\Config;
use Drupal\backup_migrate\Core\Plugin\PluginBase;
class FileExcludeFilter extends PluginBase {
public $patterns;
public function beforeFileBackup($path, $params = []) {
$source = $this
->confGet('source');
if ($source && $source == $params['source']) {
$exclude = $this
->confGet('exclude_filepaths');
$exclude = $this
->compileExcludePatterns($exclude);
if ($this
->matchPath($path, $exclude, $params['base_path'])) {
return NULL;
}
}
return $path;
}
public function configDefaults() {
return new Config([
'source' => '',
'exclude_filepaths' => [],
]);
}
private function compileExcludePatterns(array $exclude) {
if ($this->patterns !== NULL) {
return $this->patterns;
}
foreach ($exclude as $pattern) {
$this->patterns[] = "#^" . strtr(preg_quote($pattern, '#'), [
'\\*' => '.*',
'\\?' => '.',
'\\[' => '[',
'\\]' => ']',
]) . "\$#i";
}
return $this->patterns;
}
private function matchPath($path, array $exclude, $base_path = '') {
$path = substr($path, strlen($base_path));
if ($exclude) {
foreach ($exclude as $pattern) {
if (preg_match($pattern, $path)) {
return TRUE;
}
}
}
return FALSE;
}
public function configSchema(array $params = []) {
$schema = [];
$source = $this
->confGet('source');
if (!empty($source) && $params['operation'] == 'backup') {
$schema['groups']['default'] = [
'title' => $this
->t('Exclude Files from %source', [
'%source' => $source
->confGet('name'),
]),
];
if ($params['operation'] == 'backup') {
$schema['fields']['exclude_filepaths'] = [
'type' => 'text',
'title' => $this
->t('Exclude these files'),
'multiple' => TRUE,
'group' => 'default',
];
}
}
return $schema;
}
}