Skip to content

Commit

Permalink
Use constants in sanitize hooks (#5800)
Browse files Browse the repository at this point in the history
  • Loading branch information
weitzman committed Oct 31, 2023
1 parent 8dc7288 commit 6154768
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 14 deletions.
7 changes: 4 additions & 3 deletions src/Drupal/Commands/sql/SanitizeCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ final class SanitizeCommands extends DrushCommands implements CustomEventAwareIn
use CustomEventAwareTrait;

const SANITIZE = 'sql:sanitize';
const CONFIRMS = 'sql-sanitize-confirms';

/**
* Sanitize the database by removing or obfuscating user data.
*
* Commandfiles may add custom operations by implementing:
*
* - `#[CLI\Hook(type: HookManager::ON_EVENT, target: 'sql-sanitize-confirms')]`. Display summary to user before confirmation.
* - `#[CLI\Hook(type: HookManager::POST_COMMAND_HOOK, target: 'sql:sanitize')]`. Run queries or call APIs to perform sanitizing
* - `#[CLI\Hook(type: HookManager::ON_EVENT, target: SanitizeCommands::CONFIRMS)]`. Display summary to user before confirmation.
* - `#[CLI\Hook(type: HookManager::POST_COMMAND_HOOK, target: SanitizeCommands::SANITIZE)]`. Run queries or call APIs to perform sanitizing
*
* Several working commandfiles may be found at https://github.com/drush-ops/drush/tree/12.x/src/Drupal/Commands/sql
*/
Expand All @@ -40,7 +41,7 @@ public function sanitize(): void
*/
$messages = [];
$input = $this->input();
$handlers = $this->getCustomEventHandlers('sql-sanitize-confirms');
$handlers = $this->getCustomEventHandlers(self::CONFIRMS);
foreach ($handlers as $handler) {
$handler($messages, $input);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Drupal/Commands/sql/SanitizeCommentsCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function sanitize($result, CommandData $commandData): void
}
}

#[CLI\Hook(type: HookManager::ON_EVENT, target: 'sql-sanitize-confirms')]
#[CLI\Hook(type: HookManager::ON_EVENT, target: SanitizeCommands::CONFIRMS)]
public function messages(&$messages, InputInterface $input): void
{
if ($this->applies()) {
Expand Down
4 changes: 2 additions & 2 deletions src/Drupal/Commands/sql/SanitizePluginInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ interface SanitizePluginInterface
* @param $result Exit code from the main operation for sql-sanitize.
* @param CommandData $commandData Information about the current request.
*
* Use `#[CLI\Hook(type: HookManager::POST_COMMAND_HOOK, target: 'sql:sanitize')]`
* Use `#[CLI\Hook(type: HookManager::POST_COMMAND_HOOK, target: SanitizeCommands::SANITIZE)]`
*/
public function sanitize($result, CommandData $commandData);

/**
* Use #[CLI\Hook(type: HookManager::ON_EVENT, target: 'sql-sanitize-confirms')]
* Use #[CLI\Hook(type: HookManager::ON_EVENT, target: SanitizeCommands::CONFIRMS)]
*
* @param array $messages An array of messages to show during confirmation.
* @param InputInterface $input The effective commandline input for this request.
Expand Down
4 changes: 2 additions & 2 deletions src/Drupal/Commands/sql/SanitizeSessionsCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ public function getDatabase()
/**
* Sanitize sessions from the DB.
*/
#[CLI\Hook(type: HookManager::POST_COMMAND_HOOK, target: 'sql-sanitize')]
#[CLI\Hook(type: HookManager::POST_COMMAND_HOOK, target: SanitizeCommands::SANITIZE)]
public function sanitize($result, CommandData $commandData): void
{
$this->getDatabase()->truncate('sessions')->execute();
$this->logger()->success(dt('Sessions table truncated.'));
}

#[CLI\Hook(type: HookManager::ON_EVENT, target: 'sql-sanitize-confirms')]
#[CLI\Hook(type: HookManager::ON_EVENT, target: SanitizeCommands::CONFIRMS)]
public function messages(&$messages, InputInterface $input): void
{
$messages[] = dt('Truncate sessions table.');
Expand Down
6 changes: 3 additions & 3 deletions src/Drupal/Commands/sql/SanitizeUserFieldsCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function getEntityFieldManager()
*
* @todo Use Drupal services to get field info.
*/
#[CLI\Hook(type: HookManager::POST_COMMAND_HOOK, target: 'sql-sanitize')]
#[CLI\Hook(type: HookManager::POST_COMMAND_HOOK, target: SanitizeCommands::SANITIZE)]
public function sanitize($result, CommandData $commandData): void
{
$options = $commandData->options();
Expand Down Expand Up @@ -119,13 +119,13 @@ public function sanitize($result, CommandData $commandData): void
}
}

#[CLI\Hook(type: HookManager::ON_EVENT, target: 'sql-sanitize-confirms')]
#[CLI\Hook(type: HookManager::ON_EVENT, target: SanitizeCommands::CONFIRMS)]
public function messages(&$messages, InputInterface $input): void
{
$messages[] = dt('Sanitize text fields associated with users.');
}

#[CLI\Hook(type: HookManager::OPTION_HOOK, target: 'sql-sanitize')]
#[CLI\Hook(type: HookManager::OPTION_HOOK, target: SanitizeCommands::SANITIZE)]
#[CLI\Option(name: 'allowlist-fields', description: 'A comma delimited list of fields exempt from sanitization.')]
public function options($options = ['allowlist-fields' => '']): void
{
Expand Down
6 changes: 3 additions & 3 deletions src/Drupal/Commands/sql/SanitizeUserTableCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __construct(
* Sanitize emails and passwords. This also an example of how to write a
* database sanitizer for sql-sync.
*/
#[CLI\Hook(type: HookManager::POST_COMMAND_HOOK, target: 'sql-sanitize')]
#[CLI\Hook(type: HookManager::POST_COMMAND_HOOK, target: SanitizeCommands::SANITIZE)]
public function sanitize($result, CommandData $commandData): void
{
$options = $commandData->options();
Expand Down Expand Up @@ -83,14 +83,14 @@ public function sanitize($result, CommandData $commandData): void
}
}

#[CLI\Hook(type: HookManager::OPTION_HOOK, target: 'sql-sanitize')]
#[CLI\Hook(type: HookManager::OPTION_HOOK, target: SanitizeCommands::SANITIZE)]
#[CLI\Option(name: 'sanitize-email', description: 'The pattern for test email addresses in the sanitization operation, or <info>no</info> to keep email addresses unchanged. May contain replacement patterns <info>%uid</info>, <info>%mail</info> or <info>%name</info>.')]
#[CLI\Option(name: 'sanitize-password', description: 'By default, passwords are randomized. Specify <info>no</info> to disable that. Specify any other value to set all passwords to that value.')]
public function options($options = ['sanitize-email' => 'user+%uid@localhost.localdomain', 'sanitize-password' => null]): void
{
}

#[CLI\Hook(type: HookManager::ON_EVENT, target: 'sql-sanitize-confirms')]
#[CLI\Hook(type: HookManager::ON_EVENT, target: SanitizeCommands::CONFIRMS)]
public function messages(&$messages, InputInterface $input): void
{
$options = $input->getOptions();
Expand Down

0 comments on commit 6154768

Please sign in to comment.