Skip to content

Commit

Permalink
Merge pull request #4 from druidfi/drush-11-support
Browse files Browse the repository at this point in the history
Drush 11 support - code style
  • Loading branch information
back-2-95 authored Jan 17, 2022
2 parents ec6bd3d + 1ea4c8e commit e6f60b1
Show file tree
Hide file tree
Showing 14 changed files with 179 additions and 206 deletions.
10 changes: 7 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@
],
"require": {
"php": "^7.4 || ^8.0",
"druidfi/gdpr-mysqldump": "^1.0.0"
"druidfi/gdpr-mysqldump": "^1.0.0",
"drush/drush": "^11.0",
"drupal/core-recommended": "^9"
},
"require-dev": {
"drush/drush": "^11.0"
"config": {
"allow-plugins": {
"cweagans/composer-patches": true
}
}
}
2 changes: 1 addition & 1 deletion gdpr_dumper.info.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: GDPR dumper
description: 'A drop-in replacement for mysqldump that optionally sanitizes DB fields for better GDPR conformity.'
type: module
core_version_requirement: ^8 || ^9
core_version_requirement: ^9
60 changes: 30 additions & 30 deletions src/Commands/SqlCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,38 @@
* Class SQlCommands
* @package Drupal\gdpr_dumper\Commands
*/
class SqlCommands extends SqlCommandsBase {


/**
* Exports a GDPR compliant Drupal DB as SQL using mysqldump or equivalent.
*
* @command sql:dump-gdpr
* @aliases sql-dump-gdpr
* @optionset_sql
* @optionset_table_selection
* @option result-file Save to a file. The file should be relative to Drupal root. If --result-file is provided with the value 'auto', a date-based filename will be created under ~/drush-backups directory.
* @option create-db Omit DROP TABLE statements. Used by Postgres and Oracle only.
* @option data-only Dump data without statements to create any of the schema.
* @option ordered-dump Order by primary key and add line breaks for efficient diffs. Slows down the dump. Mysql only.
* @option gzip Compress the dump using the gzip program which must be in your $PATH.
* @option extra Add custom arguments/options when connecting to database (used internally to list tables).
* @option extra-dump Add custom arguments/options to the dumping of the database (e.g. mysqldump command).
* @usage drush sql:dump-gdpr --result-file=../18.sql
* Save SQL dump to the directory above Drupal root.
* @usage drush sql:dump-gdpr --skip-tables-key=common
* Skip standard tables. @see example.drush.yml
* @usage drush sql:dump-gdpr --extra-dump=--no-data
* Pass extra option to mysqldump command.
* @hidden-options create-db
* @bootstrap max configuration
*
* @notes
* createdb is used by sql-sync, since including the DROP TABLE statements interfere with the import when the database is created.
*/
class SqlCommands extends SqlCommandsBase
{
/**
* Exports a GDPR compliant Drupal DB as SQL using mysqldump or equivalent.
*
* @command sql:dump-gdpr
* @aliases sql-dump-gdpr
* @optionset_sql
* @optionset_table_selection
* @option result-file Save to a file. The file should be relative to Drupal root. If --result-file is provided with the value 'auto', a date-based filename will be created under ~/drush-backups directory.
* @option create-db Omit DROP TABLE statements. Used by Postgres and Oracle only.
* @option data-only Dump data without statements to create any of the schema.
* @option ordered-dump Order by primary key and add line breaks for efficient diffs. Slows down the dump. Mysql only.
* @option gzip Compress the dump using the gzip program which must be in your $PATH.
* @option extra Add custom arguments/options when connecting to database (used internally to list tables).
* @option extra-dump Add custom arguments/options to the dumping of the database (e.g. mysqldump command).
* @usage drush sql:dump-gdpr --result-file=../18.sql
* Save SQL dump to the directory above Drupal root.
* @usage drush sql:dump-gdpr --skip-tables-key=common
* Skip standard tables. @throws \Exception
* @see example.drush.yml
* @usage drush sql:dump-gdpr --extra-dump=--no-data
* Pass extra option to mysqldump command.
* @hidden-options create-db
* @bootstrap max configuration
*
* @notes
* createdb is used by sql-sync, since including the DROP TABLE statements interfere with the import when the database is created.
*/
public function dump($options = ['result-file' => self::REQ, 'create-db' => false, 'data-only' => false, 'ordered-dump' => false, 'gzip' => false, 'extra' => self::REQ, 'extra-dump' => self::REQ, 'format' => 'null']): PropertyList
{
// Create new dump of DB, GDPR compliant.
// Create new GDPR-compliant dump of DB.
$sql = GdprSqlBase::create($options);
$return = $sql->dump();

Expand Down
8 changes: 0 additions & 8 deletions src/Commands/SqlSyncCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,6 @@
*/
class SqlSyncCommands extends SqlSyncCommandsBase
{
/**
* @inheritDoc
*/
public function sqlsync($source, $target, $options = ['no-dump' => false, 'no-sync' => false, 'runner' => self::REQ, 'create-db' => false, 'db-su' => self::REQ, 'db-su-pw' => self::REQ, 'target-dump' => self::REQ, 'source-dump' => self::OPT, 'extra-dump' => self::REQ]): void
{
parent::sqlsync($source, $target, $options);
}

/**
* Perform sql-dump on source unless told otherwise.
*
Expand Down
4 changes: 2 additions & 2 deletions src/Event/GdprDumperEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
/**
* Defines events for the gdpr_dumper module.
*/
final class GdprDumperEvents {

final class GdprDumperEvents
{
/**
* Name of the event fired building the GDPR expressions.
*
Expand Down
46 changes: 19 additions & 27 deletions src/Event/GdprExpressionsEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,24 @@
* Class GdprExpressionsEvent
* @package Drupal\gdpr_dumper\Event
*/
class GdprExpressionsEvent extends Event {

protected array $expressions;

/**
* GdprExpressionsEvent constructor.
* @param $expressions
*/
public function __construct($expressions) {
$this->expressions = $expressions;
}

/**
* @return array
*/
public function getExpressions() {
return $this->expressions;
}

/**
* @param array $expressions
* @return $this
*/
public function setExpressions(array $expressions) {
$this->expressions = $expressions;
return $this;
}
class GdprExpressionsEvent extends Event
{
protected array $expressions;

public function __construct($expressions)
{
$this->expressions = $expressions;
}

public function getExpressions(): array
{
return $this->expressions;
}

public function setExpressions(array $expressions): GdprExpressionsEvent
{
$this->expressions = $expressions;
return $this;
}

}
52 changes: 20 additions & 32 deletions src/Event/GdprReplacementsEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,30 @@

namespace Drupal\gdpr_dumper\Event;

use Symfony\Component\EventDispatcher\Event;
use Symfony\Contracts\EventDispatcher\Event;

/**
* Class GdprReplacementsEvent
* @package Drupal\gdpr_dumper\Event
*/
class GdprReplacementsEvent extends Event {

/**
* @var array
*/
protected $replacements;


/**
* GdprReplacementsEvent constructor.
* @param $replacements
*/
public function __construct($replacements) {
$this->replacements = $replacements;
}

/**
* @return array
*/
public function getReplacements() {
return $this->replacements;
}

/**
* @param array $replacements
* @return $this
*/
public function setReplacements(array $replacements) {
$this->replacements = $replacements;
return $this;
}
class GdprReplacementsEvent extends Event
{
protected array $replacements;

public function __construct($replacements)
{
$this->replacements = $replacements;
}

public function getReplacements(): array
{
return $this->replacements;
}

public function setReplacements(array $replacements): GdprReplacementsEvent
{
$this->replacements = $replacements;
return $this;
}

}
130 changes: 66 additions & 64 deletions src/Sql/GdprSqlBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,76 +15,78 @@
* Class GdprSqlBase
* @package Drupal\gdpr_dumper\Sql
*/
class GdprSqlBase extends SqlBase {
class GdprSqlBase extends SqlBase
{
/**
* {@inheritdoc}
* @throws \Exception
*/
public static function create(array $options = []): SqlBase
{
// Set defaults in the unfortunate event that caller doesn't provide values.
$options += [
'database' => 'default',
'target' => 'default',
'db-url' => null,
'databases' => null,
'db-prefix' => null,
];
$database = $options['database'];
$target = $options['target'];
$event_dispatcher = \Drupal::service('event_dispatcher');

/**
* {@inheritdoc}
*/
public static function create(array $options = []): SqlBase {
// Set defaults in the unfortunate event that caller doesn't provide values.
$options += [
'database' => 'default',
'target' => 'default',
'db-url' => NULL,
'databases' => NULL,
'db-prefix' => NULL,
];
$database = $options['database'];
$target = $options['target'];
$event_dispatcher = \Drupal::service('event_dispatcher');

if ($url = $options['db-url']) {
$url = is_array($url) ? $url[$database] : $url;
$db_spec = self::dbSpecFromDbUrl($url);
$db_spec['db_prefix'] = $options['db-prefix'];
return self::getInstance($db_spec, $options, $event_dispatcher);
}
elseif (($databases = $options['databases']) && (array_key_exists($database, $databases)) && (array_key_exists($target, $databases[$database]))) {
$db_spec = $databases[$database][$target];
return self::getInstance($db_spec, $options, $event_dispatcher);
}
elseif ($info = Database::getConnectionInfo($database)) {
$db_spec = $info[$target];
return self::getInstance($db_spec, $options, $event_dispatcher);
if ($url = $options['db-url']) {
$url = is_array($url) ? $url[$database] : $url;
$db_spec = self::dbSpecFromDbUrl($url);
$db_spec['prefix'] = $options['db-prefix'];
return self::getInstance($db_spec, $options, $event_dispatcher);
}
elseif (($databases = $options['databases']) && (array_key_exists($database, $databases)) && (array_key_exists($target, $databases[$database]))) {
$db_spec = $databases[$database][$target];
return self::getInstance($db_spec, $options, $event_dispatcher);
}
elseif ($info = Database::getConnectionInfo($database)) {
$db_spec = $info[$target];
return self::getInstance($db_spec, $options, $event_dispatcher);
}
else {
throw new \Exception(dt('Unable to load Drupal settings. Check your --root, --uri, etc.'));
}
}
else {
throw new \Exception(dt('Unable to load Drupal settings. Check your --root, --uri, etc.'));
}
}

public static function getInstance($db_spec, $options, EventDispatcherInterface $event_dispatcher = NULL): SqlBase {
$driver = $db_spec['driver'];
$className = 'Drupal\gdpr_dumper\Sql\GdprSql' . ucfirst($driver);
// Fetch module settings.
$config = \Drupal::config('gdpr_dumper.settings');
public static function getInstance($db_spec, $options, EventDispatcherInterface $event_dispatcher = null): SqlBase
{
$driver = $db_spec['driver'];
$className = 'Drupal\gdpr_dumper\Sql\GdprSql' . ucfirst($driver);
// Fetch module settings.
$config = \Drupal::config('gdpr_dumper.settings');

if (empty($options['extra-dump']) || strpos($options['extra-dump'], '--gdpr-expressions') === FALSE) {
if (empty($options['extra-dump']) || strpos($options['extra-dump'], '--gdpr-expressions') === false) {
// Dispatch event so the expressions can be altered.
$event = new GdprExpressionsEvent($config->get('gdpr_expressions'));
$event_dispatcher->dispatch(GdprDumperEvents::GDPR_EXPRESSIONS, $event);
// Add the configured GDPR expressions to the command.
if ($expressions = Json::encode($event->getExpressions())){
$options['extra-dump'] .= " --gdpr-expressions='$expressions'";
}
}

// Dispatch event so the expressions can be altered.
$event = new GdprExpressionsEvent($config->get('gdpr_expressions'));
$event_dispatcher->dispatch(GdprDumperEvents::GDPR_EXPRESSIONS, $event);
// Add the configured GDPR expressions to the command.
if($expressions = Json::encode($event->getExpressions())){
$options['extra-dump'] .= " --gdpr-expressions='$expressions'";
}
}
if (empty($options['extra-dump']) || strpos($options['extra-dump'], '--gdpr-replacements') === false) {
// Dispatch event so the replacements can be altered.
$event = new GdprReplacementsEvent($config->get('gdpr_replacements'));
$event_dispatcher->dispatch(GdprDumperEvents::GDPR_REPLACEMENTS, $event);
// Add the configured GDPR replacements to the command.
if ($replacements = Json::encode($event->getReplacements())){
$options['extra-dump'] .= " --gdpr-replacements='$replacements'";
}
}

if (empty($options['extra-dump']) || strpos($options['extra-dump'], '--gdpr-replacements') === FALSE) {
// Dispatch event so the replacements can be altered.
$event = new GdprReplacementsEvent($config->get('gdpr_replacements'));
$event_dispatcher->dispatch(GdprDumperEvents::GDPR_REPLACEMENTS, $event);
// Add the configured GDPR replacements to the command.
if($replacements = Json::encode($event->getReplacements())){
$options['extra-dump'] .= " --gdpr-replacements='$replacements'";
}
$instance = new $className($db_spec, $options);
$driver_options = isset($config->get('drivers')[$driver]) ? $config->get('drivers')[$driver] : [];
// Inject config
$instance->setConfig(Drush::config());
$instance->setDriverOptions($driver_options);
return $instance;
}

$instance = new $className($db_spec, $options);
$driver_options = isset($config->get('drivers')[$driver]) ? $config->get('drivers')[$driver] : [];
// Inject config
$instance->setConfig(Drush::config());
$instance->setDriverOptions($driver_options);
return $instance;
}

}
4 changes: 2 additions & 2 deletions src/Sql/GdprSqlMysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
* Class GdprSqlMysql
* @package Drupal\gdpr_dumper\Commands
*/
class GdprSqlMysql extends SqlMysql {

class GdprSqlMysql extends SqlMysql
{
use GdprSqlTrait;

}
4 changes: 2 additions & 2 deletions src/Sql/GdprSqlOracle.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
* Class GdprSqlOracle
* @package Drupal\gdpr_dumper\Commands
*/
class GdprSqlOracle extends SqlOracle {

class GdprSqlOracle extends SqlOracle
{
use GdprSqlTrait;

}
Loading

0 comments on commit e6f60b1

Please sign in to comment.