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 Options $ options ,
73107 DatabaseSession $ databaseSession ,
74108 ): AbstractAnonymizer {
75- $ className = $ this ->getAnonymizerClass ($ name );
76-
77- $ ret = new $ className ($ config ->table , $ config ->targetName , $ databaseSession , $ options );
78- \assert ($ ret instanceof AbstractAnonymizer);
109+ if ($ this ->packRegistry ->hasPack ($ name )) {
110+ $ ret = $ this ->createAnonymizerFromPack (
111+ $ this ->packRegistry ->getPackAnonymizer ($ name ),
112+ $ config ,
113+ $ options ,
114+ $ databaseSession ,
115+ );
116+ } else {
117+ $ className = $ this ->getAnonymizerClass ($ name );
118+
119+ $ ret = new $ className ($ config ->table , $ config ->targetName , $ databaseSession , $ options );
120+ \assert ($ ret instanceof AbstractAnonymizer);
121+ }
79122
80123 if ($ ret instanceof WithAnonymizerRegistry) {
81124 $ ret ->setAnonymizerRegistry ($ this );
@@ -84,6 +127,84 @@ 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+ Options $ options ,
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 = $ 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+ // @todo Convert data to an array if an iterable was
150+ // here. Later, change getSample() signature of
151+ // AbstractEnumAnonymizer to accept any iterable.
152+ $ options ->with ([
153+ 'sample ' => \is_array ($ packAnonymizer ->data ) ? $ packAnonymizer ->data : \iterator_to_array ($ packAnonymizer ->data ),
154+ ]),
155+ );
156+ }
157+
158+ if ($ packAnonymizer instanceof PackMultipleColumnAnonymizer) {
159+ // @todo
160+ throw new \LogicException ("Not implemented yet: missing arbitrary multiple column anonymizer. " );
161+ }
162+
163+ if ($ packAnonymizer instanceof PackEnumGeneratedAnonymizer) {
164+ if (1 !== \count ($ packAnonymizer ->pattern )) {
165+ // @todo
166+ throw new \LogicException ("Not implemented yet: pattern anonymizer does not support multiple patterns yet. " );
167+ }
168+
169+ return new Core \StringPatternAnonymizer (
170+ $ config ->table ,
171+ $ config ->targetName ,
172+ $ databaseSession ,
173+ $ options ->with ([
174+ 'pattern ' => $ packAnonymizer ->pattern [0 ],
175+ ]),
176+ );
177+ }
178+
179+ if ($ packAnonymizer instanceof PackMultipleColumnGeneratedAnonymizer) {
180+ // @todo
181+ throw new \LogicException ("Not implemented yet: missing arbitrary column generator anonymizer. " );
182+ }
183+
184+ if ($ packAnonymizer instanceof PackFileEnumAnonymizer) {
185+ return new Core \FileEnumAnonymizer (
186+ $ config ->table ,
187+ $ config ->targetName ,
188+ $ databaseSession ,
189+ $ options ->with (['source ' => $ packAnonymizer ->filename ]),
190+ );
191+ }
192+
193+ if ($ packAnonymizer instanceof PackFileMultipleColumnAnonymizer) {
194+ return new Core \FileMultipleColumnAnonymizer (
195+ $ config ->table ,
196+ $ config ->targetName ,
197+ $ databaseSession ,
198+ $ options ->with ([
199+ 'columns ' => $ packAnonymizer ->columns ,
200+ 'source ' => $ packAnonymizer ->filename ,
201+ ]),
202+ );
203+ }
204+
205+ throw new \LogicException (\sprintf ("Pack anonymizer with class '%s' is not implement yet. " , \get_class ($ packAnonymizer )));
206+ }
207+
87208 /**
88209 * Get anonymizer metadata.
89210 */
@@ -173,6 +294,12 @@ private function initialize(): void
173294 }
174295 }
175296 }
297+
298+ if ($ this ->packs ) {
299+ foreach ($ this ->packs as $ filename ) {
300+ $ this ->packRegistry ->addPack ($ filename );
301+ }
302+ }
176303 }
177304
178305 /**
@@ -214,8 +341,10 @@ private function locatePacks(): void
214341 $ path = $ directory . '/src/Anonymizer/ ' ;
215342 if (\is_dir ($ path )) {
216343 $ this ->addPath ([$ path ]);
344+ } elseif (\file_exists ($ path . '/db_tools.pack.yaml ' )) {
345+ $ this ->addPack ([$ path . '/db_tools.pack.yaml ' ]);
217346 } 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 );
347+ \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 );
219348 }
220349 }
221350 }
0 commit comments