66
77use Composer \InstalledVersions ;
88use MakinaCorpus \DbToolsBundle \Anonymization \Config \AnonymizerConfig ;
9+ use MakinaCorpus \DbToolsBundle \Anonymization \Pack \PackAnonymizer ;
10+ use MakinaCorpus \DbToolsBundle \Anonymization \Pack \PackEnumAnonymizer ;
11+ use MakinaCorpus \DbToolsBundle \Anonymization \Pack \PackEnumGeneratedAnonymizer ;
12+ use MakinaCorpus \DbToolsBundle \Anonymization \Pack \PackFileEnumAnonymizer ;
13+ use MakinaCorpus \DbToolsBundle \Anonymization \Pack \PackFileMultipleColumnAnonymizer ;
14+ use MakinaCorpus \DbToolsBundle \Anonymization \Pack \PackMultipleColumnAnonymizer ;
15+ use MakinaCorpus \DbToolsBundle \Anonymization \Pack \PackMultipleColumnGeneratedAnonymizer ;
16+ use MakinaCorpus \DbToolsBundle \Anonymization \Pack \PackRegistry ;
917use MakinaCorpus \DbToolsBundle \Attribute \AsAnonymizer ;
1018use MakinaCorpus \QueryBuilder \DatabaseSession ;
1119
@@ -32,15 +40,33 @@ class AnonymizerRegistry
3240 Core \StringPatternAnonymizer::class,
3341 ];
3442
43+ private PackRegistry $ packRegistry ;
44+
3545 /** @var array<string, string> */
3646 private ?array $ classes = null ;
47+
3748 /** @var array<string, AsAnonymizer> */
3849 private ?array $ metadata = null ;
50+
51+ /**
52+ * Paths where to lookup for custom anonymizers.
53+ *
54+ * @var array<string>
55+ */
3956 private array $ paths = [];
4057
41- public function __construct (?array $ paths = null )
58+ /**
59+ * Pack filenames where to lookup for PHP-less packs.
60+ *
61+ * @var array<string>
62+ */
63+ private array $ packs = [];
64+
65+ public function __construct (?array $ paths = null , ?array $ packs = null )
4266 {
4367 $ this ->addPath ($ paths ?? []);
68+ $ this ->addPack ($ packs ?? []);
69+ $ this ->packRegistry = new PackRegistry ();
4470 }
4571
4672 /**
@@ -51,6 +77,14 @@ public function addPath(array $paths): void
5177 $ this ->paths = \array_unique (\array_merge ($ this ->paths , $ paths ));
5278 }
5379
80+ /**
81+ * Add PHP-less configuration file pack.
82+ */
83+ public function addPack (array $ packs ): void
84+ {
85+ $ this ->packs = \array_unique (\array_merge ($ this ->packs , $ packs ));
86+ }
87+
5488 /**
5589 * Get all registered anonymizers classe names.
5690 *
@@ -72,10 +106,19 @@ public function createAnonymizer(
72106 Context $ context ,
73107 DatabaseSession $ databaseSession ,
74108 ): AbstractAnonymizer {
75- $ className = $ this ->getAnonymizerClass ($ name );
76-
77- $ ret = new $ className ($ config ->table , $ config ->targetName , $ databaseSession , $ context , $ config ->options );
78- \assert ($ ret instanceof AbstractAnonymizer);
109+ if ($ this ->packRegistry ->hasPack ($ name )) {
110+ $ ret = $ this ->createAnonymizerFromPack (
111+ $ this ->packRegistry ->getPackAnonymizer ($ name ),
112+ $ config ,
113+ $ context ,
114+ $ databaseSession ,
115+ );
116+ } else {
117+ $ className = $ this ->getAnonymizerClass ($ name );
118+
119+ $ ret = new $ className ($ config ->table , $ config ->targetName , $ databaseSession , $ context , $ config ->options );
120+ \assert ($ ret instanceof AbstractAnonymizer);
121+ }
79122
80123 if ($ ret instanceof WithAnonymizerRegistry) {
81124 $ ret ->setAnonymizerRegistry ($ this );
@@ -84,6 +127,88 @@ public function createAnonymizer(
84127 return $ ret ;
85128 }
86129
130+ /**
131+ * Create anonymizer instance from pack.
132+ */
133+ private function createAnonymizerFromPack (
134+ PackAnonymizer $ packAnonymizer ,
135+ AnonymizerConfig $ config ,
136+ Context $ context ,
137+ DatabaseSession $ databaseSession
138+ ): AbstractAnonymizer {
139+ // Merge incomming user options with options from the pack.
140+ // Pack given options will override the user one.
141+ $ options = $ config ->options ->with ($ packAnonymizer ->options ->all ());
142+
143+ // Anonymizer from pack factory. Hardcoded for now.
144+ if ($ packAnonymizer instanceof PackEnumAnonymizer) {
145+ return new Core \StringAnonymizer (
146+ $ config ->table ,
147+ $ config ->targetName ,
148+ $ databaseSession ,
149+ $ context ,
150+ // @todo Convert data to an array if an iterable was
151+ // here. Later, change getSample() signature of
152+ // AbstractEnumAnonymizer to accept any iterable.
153+ $ options ->with ([
154+ 'sample ' => \is_array ($ packAnonymizer ->data ) ? $ packAnonymizer ->data : \iterator_to_array ($ packAnonymizer ->data ),
155+ ]),
156+ );
157+ }
158+
159+ if ($ packAnonymizer instanceof PackMultipleColumnAnonymizer) {
160+ // @todo
161+ throw new \LogicException ("Not implemented yet: missing arbitrary multiple column anonymizer. " );
162+ }
163+
164+ if ($ packAnonymizer instanceof PackEnumGeneratedAnonymizer) {
165+ if (1 !== \count ($ packAnonymizer ->pattern )) {
166+ // @todo
167+ throw new \LogicException ("Not implemented yet: pattern anonymizer does not support multiple patterns yet. " );
168+ }
169+
170+ return new Core \StringPatternAnonymizer (
171+ $ config ->table ,
172+ $ config ->targetName ,
173+ $ databaseSession ,
174+ $ context ,
175+ $ options ->with ([
176+ 'pattern ' => $ packAnonymizer ->pattern [0 ],
177+ ]),
178+ );
179+ }
180+
181+ if ($ packAnonymizer instanceof PackMultipleColumnGeneratedAnonymizer) {
182+ // @todo
183+ throw new \LogicException ("Not implemented yet: missing arbitrary column generator anonymizer. " );
184+ }
185+
186+ if ($ packAnonymizer instanceof PackFileEnumAnonymizer) {
187+ return new Core \FileEnumAnonymizer (
188+ $ config ->table ,
189+ $ config ->targetName ,
190+ $ databaseSession ,
191+ $ context ,
192+ $ options ->with (['source ' => $ packAnonymizer ->filename ]),
193+ );
194+ }
195+
196+ if ($ packAnonymizer instanceof PackFileMultipleColumnAnonymizer) {
197+ return new Core \FileMultipleColumnAnonymizer (
198+ $ config ->table ,
199+ $ config ->targetName ,
200+ $ databaseSession ,
201+ $ context ,
202+ $ options ->with ([
203+ 'columns ' => $ packAnonymizer ->columns ,
204+ 'source ' => $ packAnonymizer ->filename ,
205+ ]),
206+ );
207+ }
208+
209+ throw new \LogicException (\sprintf ("Pack anonymizer with class '%s' is not implement yet. " , \get_class ($ packAnonymizer )));
210+ }
211+
87212 /**
88213 * Get anonymizer metadata.
89214 */
@@ -173,6 +298,12 @@ private function initialize(): void
173298 }
174299 }
175300 }
301+
302+ if ($ this ->packs ) {
303+ foreach ($ this ->packs as $ filename ) {
304+ $ this ->packRegistry ->addPack ($ filename );
305+ }
306+ }
176307 }
177308
178309 /**
@@ -214,8 +345,10 @@ private function locatePacks(): void
214345 $ path = $ directory . '/src/Anonymizer/ ' ;
215346 if (\is_dir ($ path )) {
216347 $ this ->addPath ([$ path ]);
348+ } elseif (\file_exists ($ path . '/db_tools.pack.yaml ' )) {
349+ $ this ->addPack ([$ path . '/db_tools.pack.yaml ' ]);
217350 } else {
218- \trigger_error (\sprintf ("Anonymizers pack '%s' in '%s' as no 'src/Anonymizer/' directory and is thus not usable. " , $ package , $ directory ), \E_USER_ERROR );
351+ \trigger_error (\sprintf ("Anonymizers pack '%s' in '%s' as no 'src/Anonymizer/' directory nor 'db_tools.pack.yaml' file and is thus not usable. " , $ package , $ directory ), \E_USER_ERROR );
219352 }
220353 }
221354 }
0 commit comments