66
77use Composer \InstalledVersions ;
88use MakinaCorpus \DbToolsBundle \Anonymization \Config \AnonymizerConfig ;
9+ use MakinaCorpus \DbToolsBundle \Anonymization \Pack \PackRegistry ;
910use MakinaCorpus \DbToolsBundle \Attribute \AsAnonymizer ;
1011use MakinaCorpus \QueryBuilder \DatabaseSession ;
12+ use MakinaCorpus \DbToolsBundle \Anonymization \Pack \PackColumnAnonymizer ;
13+ use MakinaCorpus \DbToolsBundle \Anonymization \Pack \PackColumnGeneratedAnonymizer ;
14+ use MakinaCorpus \DbToolsBundle \Anonymization \Pack \PackEnumAnonymizer ;
15+ use MakinaCorpus \DbToolsBundle \Anonymization \Pack \PackEnumGeneratedAnonymizer ;
1116
1217class AnonymizerRegistry
1318{
@@ -30,15 +35,33 @@ class AnonymizerRegistry
3035 Core \StringPatternAnonymizer::class,
3136 ];
3237
38+ private PackRegistry $ packRegistry ;
39+
3340 /** @var array<string, string> */
3441 private ?array $ classes = null ;
42+
3543 /** @var array<string, AsAnonymizer> */
3644 private ?array $ metadata = null ;
45+
46+ /**
47+ * Paths where to lookup for custom anonymizers.
48+ *
49+ * @var array<string>
50+ */
3751 private array $ paths = [];
3852
39- public function __construct (?array $ paths = null )
53+ /**
54+ * Pack filenames where to lookup for PHP-less packs.
55+ *
56+ * @var array<string>
57+ */
58+ private array $ packs = [];
59+
60+ public function __construct (?array $ paths = null , ?array $ packs = null )
4061 {
4162 $ this ->addPath ($ paths ?? []);
63+ $ this ->addPack ($ packs ?? []);
64+ $ this ->packRegistry = new PackRegistry ();
4265 }
4366
4467 /**
@@ -49,6 +72,14 @@ public function addPath(array $paths): void
4972 $ this ->paths = \array_unique (\array_merge ($ this ->paths , $ paths ));
5073 }
5174
75+ /**
76+ * Add PHP-less configuration file pack.
77+ */
78+ public function addPack (array $ packs ): void
79+ {
80+ $ this ->packs = \array_unique (\array_merge ($ this ->packs , $ packs ));
81+ }
82+
5283 /**
5384 * Get all registered anonymizers classe names.
5485 *
@@ -70,6 +101,23 @@ public function createAnonymizer(
70101 Options $ options ,
71102 DatabaseSession $ databaseSession ,
72103 ): AbstractAnonymizer {
104+ if ($ this ->packRegistry ->hasPack ($ name )) {
105+ $ packAnonymizer = $ this ->packRegistry ->getPackAnonymizer ($ name );
106+
107+ // Anonymizer from pack factory. Hardcoded for now.
108+ if ($ packAnonymizer instanceof PackColumnAnonymizer) {
109+ throw new \LogicException ("Not implemented yet. " );
110+ } elseif ($ packAnonymizer instanceof PackColumnGeneratedAnonymizer) {
111+ throw new \LogicException ("Not implemented yet. " );
112+ } elseif ($ packAnonymizer instanceof PackEnumAnonymizer) {
113+ throw new \LogicException ("Not implemented yet. " );
114+ } elseif ($ packAnonymizer instanceof PackEnumGeneratedAnonymizer) {
115+ throw new \LogicException ("Not implemented yet. " );
116+ } else {
117+ throw new \LogicException (\sprintf ("Pack anonymizer with class '%s' is not implement yet. " , \get_class ($ packAnonymizer )));
118+ }
119+ }
120+
73121 $ className = $ this ->getAnonymizerClass ($ name );
74122
75123 $ ret = new $ className ($ config ->table , $ config ->targetName , $ databaseSession , $ options );
@@ -171,6 +219,12 @@ private function initialize(): void
171219 }
172220 }
173221 }
222+
223+ if ($ this ->packs ) {
224+ foreach ($ this ->packs as $ filename ) {
225+ $ this ->packRegistry ->addPack ($ filename );
226+ }
227+ }
174228 }
175229
176230 /**
@@ -212,8 +266,10 @@ private function locatePacks(): void
212266 $ path = $ directory . '/src/Anonymizer/ ' ;
213267 if (\is_dir ($ path )) {
214268 $ this ->addPath ([$ path ]);
269+ } elseif (\file_exists ($ path . '/db_tools.pack.yaml ' )) {
270+ $ this ->addPack ([$ path . '/db_tools.pack.yaml ' ]);
215271 } else {
216- \trigger_error (\sprintf ("Anonymizers pack '%s' in '%s' as no 'src/Anonymizer/' directory and is thus not usable. " , $ package , $ directory ), \E_USER_ERROR );
272+ \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 );
217273 }
218274 }
219275 }
0 commit comments