|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace DH\AuditorBundle\Event; |
|
6
|
|
|
|
|
7
|
|
|
use DH\Auditor\Configuration; |
|
8
|
|
|
use DH\Auditor\User\UserProviderInterface; |
|
9
|
|
|
use DH\AuditorBundle\User\ConsoleUserProvider; |
|
10
|
|
|
use Symfony\Component\Console\ConsoleEvents; |
|
11
|
|
|
use Symfony\Component\Console\Event\ConsoleCommandEvent; |
|
12
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
13
|
|
|
|
|
14
|
|
|
class ConsoleEventSubscriber implements EventSubscriberInterface |
|
15
|
|
|
{ |
|
16
|
|
|
private ConsoleUserProvider $consoleUserProvider; |
|
17
|
|
|
private Configuration $configuration; |
|
18
|
|
|
private UserProviderInterface $provider; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct(ConsoleUserProvider $consoleUserProvider, Configuration $configuration, UserProviderInterface $provider) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->consoleUserProvider = $consoleUserProvider; |
|
23
|
|
|
$this->configuration = $configuration; |
|
24
|
|
|
$this->provider = $provider; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public static function getSubscribedEvents(): array |
|
28
|
|
|
{ |
|
29
|
|
|
return [ |
|
30
|
|
|
ConsoleEvents::COMMAND => 'registerConsoleUser', |
|
31
|
|
|
ConsoleEvents::TERMINATE => 'restoreDefaultUser', |
|
32
|
|
|
]; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function registerConsoleUser(ConsoleCommandEvent $commandEvent): void |
|
36
|
|
|
{ |
|
37
|
|
|
$command = $commandEvent->getCommand(); |
|
38
|
|
|
$this->consoleUserProvider->setCurrentCommand($command); |
|
39
|
|
|
$this->configuration->setUserProvider($this->consoleUserProvider); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function restoreDefaultUser(): void |
|
43
|
|
|
{ |
|
44
|
|
|
$this->consoleUserProvider->setCurrentCommand(null); |
|
45
|
|
|
$this->configuration->setUserProvider($this->provider); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|