-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
SanitizeSessionsCommands.php
51 lines (43 loc) · 1.42 KB
/
SanitizeSessionsCommands.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
declare(strict_types=1);
namespace Drush\Commands\sql\sanitize;
use Consolidation\AnnotatedCommand\CommandData;
use Consolidation\AnnotatedCommand\Hooks\HookManager;
use Drupal\Core\Database\Connection;
use Drush\Attributes as CLI;
use Drush\Commands\AutowireTrait;
use Drush\Commands\DrushCommands;
use Symfony\Component\Console\Input\InputInterface;
/**
* This class is a good example of how to build a sql:sanitize plugin.
*/
final class SanitizeSessionsCommands extends DrushCommands implements SanitizePluginInterface
{
use AutowireTrait;
public function __construct(protected Connection $database)
{
parent::__construct();
}
/**
* Sanitize sessions from the DB.
*/
#[CLI\Hook(type: HookManager::POST_COMMAND_HOOK, target: SanitizeCommands::SANITIZE)]
public function sanitize($result, CommandData $commandData): void
{
if ($this->applies()) {
$this->database->truncate('sessions')->execute();
$this->logger()->success(dt('Sessions table truncated.'));
}
}
#[CLI\Hook(type: HookManager::ON_EVENT, target: SanitizeCommands::CONFIRMS)]
public function messages(&$messages, InputInterface $input): void
{
if ($this->applies()) {
$messages[] = dt('Truncate sessions table.');
}
}
private function applies(): bool
{
return $this->database->schema()->tableExists('sessions');
}
}