-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathAnonymizerRegistry.php
More file actions
227 lines (191 loc) · 6.88 KB
/
AnonymizerRegistry.php
File metadata and controls
227 lines (191 loc) · 6.88 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
declare(strict_types=1);
namespace MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer;
use Composer\InstalledVersions;
use MakinaCorpus\DbToolsBundle\Anonymization\Config\AnonymizerConfig;
use MakinaCorpus\DbToolsBundle\Attribute\AsAnonymizer;
use MakinaCorpus\QueryBuilder\DatabaseSession;
class AnonymizerRegistry
{
// Core anonymizers list is hardcoded.
private static $coreAnonymizers = [
Core\AddressAnonymizer::class,
Core\ConstantAnonymizer::class,
Core\DateAnonymizer::class,
Core\EmailAnonymizer::class,
Core\FileEnumAnonymizer::class,
Core\FirstNameAnonymizer::class,
Core\FloatAnonymizer::class,
Core\IbanBicAnonymizer::class,
Core\IntegerAnonymizer::class,
Core\LastNameAnonymizer::class,
Core\LoremIpsumAnonymizer::class,
Core\Md5Anonymizer::class,
Core\NullAnonymizer::class,
Core\PasswordAnonymizer::class,
Core\StringAnonymizer::class,
Core\StringPatternAnonymizer::class,
];
/** @var array<string, string> */
private ?array $classes = null;
/** @var array<string, AsAnonymizer> */
private ?array $metadata = null;
private array $paths = [];
public function __construct(?array $paths = null)
{
$this->addPath($paths ?? []);
}
/**
* Add path in which to lookup for anonymizers.
*/
public function addPath(array $paths): void
{
$this->paths = \array_unique(\array_merge($this->paths, $paths));
}
/**
* Get all registered anonymizers classe names.
*
* @return array<string,AsAnonymizer>
*/
public function getAllAnonymizerMetadata(): array
{
$this->initialize();
return $this->metadata;
}
/**
* Create anonymizer instance.
*/
public function createAnonymizer(
string $name,
AnonymizerConfig $config,
Context $context,
DatabaseSession $databaseSession,
): AbstractAnonymizer {
$className = $this->getAnonymizerClass($name);
$ret = new $className($config->table, $config->targetName, $databaseSession, $context, $config->options);
\assert($ret instanceof AbstractAnonymizer);
if ($ret instanceof WithAnonymizerRegistry) {
$ret->setAnonymizerRegistry($this);
}
return $ret;
}
/**
* Get anonymizer metadata.
*/
public function getAnonymizerMetadata(string $name): AsAnonymizer
{
$this->initialize();
return $this->metadata[$name] ?? $this->throwAnonymizerDoesNotExist($name);
}
/**
* @internal
* For unit tests only, please do not use.
*/
public function getAnonymizerClass(string $name): string
{
$this->initialize();
return $this->classes[$name] ?? $this->throwAnonymizerDoesNotExist($name);
}
private function getAnonymizatorClassMetadata(string $className): AsAnonymizer
{
if ($attributes = (new \ReflectionClass($className))->getAttributes(AsAnonymizer::class)) {
return $attributes[0]->newInstance();
}
throw new \LogicException(\sprintf("Class '%s' should have an '%s' attribute.", $className, AsAnonymizer::class));
}
/**
* Lazy initialization.
*/
private function initialize(): void
{
if (null !== $this->classes) {
return;
}
$this->classes = [];
$this->metadata = [];
foreach (self::$coreAnonymizers as $className) {
$this->addAnonymizer($className, true);
}
$this->locatePacks();
if ($this->paths) {
$found = false;
foreach ($this->paths as $path) {
if (!\is_dir($path)) {
throw new \LogicException(\sprintf("Given path '%s' is not a directory.", $path));
}
// Find all PHP files in the given directory.
$iterator = new \RegexIterator(
new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator(
$path,
\FilesystemIterator::SKIP_DOTS
),
\RecursiveIteratorIterator::LEAVES_ONLY
),
'/^.+' . \preg_quote('.php') . '$/i',
\RecursiveRegexIterator::GET_MATCH,
);
foreach ($iterator as $file) {
$found = true;
$sourceFile = $file[0];
if (\preg_match('(^phar:)i', $sourceFile) === 0) {
$sourceFile = \realpath($sourceFile);
}
// Require file for its content to be present in the
// \get_declared_classes() function result.
require_once $sourceFile;
}
}
if ($found) {
foreach (\get_declared_classes() as $className) {
$this->addAnonymizer($className, false);
}
}
}
}
/**
* Add anonymizer definition.
*/
private function addAnonymizer(string $className, bool $failOnError = false): void
{
$refClass = new \ReflectionClass($className);
// Ignore nbn-concrete usable classes.
if ($refClass->isAbstract()) {
if ($failOnError) {
throw new \InvalidArgumentException(\sprintf("'%s': class is abstract.", $className));
}
return;
}
if (!$refClass->isSubclassOf(AbstractAnonymizer::class)) {
if ($failOnError) {
throw new \InvalidArgumentException(\sprintf("'%s': class does not extend '%s'.", $className, AbstractAnonymizer::class));
}
return;
}
$metadata = $this->getAnonymizatorClassMetadata($className);
$id = $metadata->id();
$this->classes[$id] = $className;
$this->metadata[$id] = $metadata;
}
/**
* Locate installed packs.
*/
private function locatePacks(): void
{
if (\class_exists(InstalledVersions::class)) {
foreach (InstalledVersions::getInstalledPackagesByType('db-tools-bundle-pack') as $package) {
$directory = InstalledVersions::getInstallPath($package);
$path = $directory . '/src/Anonymizer/';
if (\is_dir($path)) {
$this->addPath([$path]);
} else {
\trigger_error(\sprintf("Anonymizers pack '%s' in '%s' as no 'src/Anonymizer/' directory and is thus not usable.", $package, $directory), \E_USER_ERROR);
}
}
}
}
private function throwAnonymizerDoesNotExist(string $name): never
{
throw new \InvalidArgumentException(\sprintf("Can't find Anonymizer with name : %s, check your configuration.", $name));
}
}