1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
4
|
|
|
* |
5
|
|
|
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics) |
6
|
|
|
* |
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU Affero General Public License as published |
9
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
10
|
|
|
* (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU Affero General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU Affero General Public License |
18
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
declare(strict_types=1); |
22
|
|
|
|
23
|
|
|
namespace App\EventSubscriber\LogSystem; |
24
|
|
|
|
25
|
|
|
use App\Entity\LogSystem\DatabaseUpdatedLogEntry; |
26
|
|
|
use App\Services\LogSystem\EventLogger; |
27
|
|
|
use Doctrine\Common\EventSubscriber; |
28
|
|
|
use Doctrine\Migrations\DependencyFactory; |
29
|
|
|
use Doctrine\Migrations\Event\MigrationsEventArgs; |
30
|
|
|
use Doctrine\Migrations\Events; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* This subscriber logs databaseMigrations to Event log. |
34
|
|
|
*/ |
35
|
|
|
class LogDBMigrationSubscriber implements EventSubscriber |
36
|
|
|
{ |
37
|
|
|
protected ?string $old_version = null; |
38
|
|
|
protected ?string $new_version = null; |
39
|
|
|
|
40
|
|
|
protected EventLogger $eventLogger; |
41
|
|
|
protected DependencyFactory $dependencyFactory; |
42
|
|
|
|
43
|
|
|
public function __construct(EventLogger $eventLogger, DependencyFactory $dependencyFactory) |
44
|
|
|
{ |
45
|
|
|
$this->eventLogger = $eventLogger; |
46
|
|
|
$this->dependencyFactory = $dependencyFactory; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function onMigrationsMigrated(MigrationsEventArgs $args): void |
50
|
|
|
{ |
51
|
|
|
//Dont do anything if this was a dry run |
52
|
|
|
if ($args->getMigratorConfiguration()->isDryRun()) { |
53
|
|
|
return; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$aliasResolver = $this->dependencyFactory->getVersionAliasResolver(); |
57
|
|
|
|
58
|
|
|
//Save the version after the migration |
59
|
|
|
//$this->new_version = $args->getMigratorConfiguration()->getCurrentVersion(); |
60
|
|
|
$this->new_version = (string) $aliasResolver->resolveVersionAlias('current'); |
61
|
|
|
|
62
|
|
|
//After everything is done, write the results to DB log |
63
|
|
|
$this->old_version = empty($this->old_version) ? 'legacy/empty' : $this->old_version; |
64
|
|
|
$this->new_version = empty($this->new_version) ? 'unknown' : $this->new_version; |
65
|
|
|
|
66
|
|
|
try { |
67
|
|
|
$log = new DatabaseUpdatedLogEntry($this->old_version, $this->new_version); |
68
|
|
|
$this->eventLogger->logAndFlush($log); |
69
|
|
|
} catch (\Throwable $exception) { |
70
|
|
|
//Ignore any exception occuring here... |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function onMigrationsMigrating(MigrationsEventArgs $args): void |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
// Save the version before any migration |
77
|
|
|
if (null === $this->old_version) { |
78
|
|
|
$aliasResolver = $this->dependencyFactory->getVersionAliasResolver(); |
79
|
|
|
|
80
|
|
|
//$this->old_version = $args->getConfiguration()->getCurrentVersion(); |
81
|
|
|
$this->old_version = (string) $aliasResolver->resolveVersionAlias('current'); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function getSubscribedEvents(): array |
86
|
|
|
{ |
87
|
|
|
return [ |
88
|
|
|
Events::onMigrationsMigrated, |
89
|
|
|
Events::onMigrationsMigrating, |
90
|
|
|
]; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.