55namespace MakinaCorpus \DbToolsBundle \Anonymization \Anonymizer ;
66
77use Composer \InstalledVersions ;
8+ use MakinaCorpus \DbToolsBundle \Anonymization \Config \AnonymizerConfig ;
9+ use MakinaCorpus \DbToolsBundle \Attribute \AsAnonymizer ;
10+ use MakinaCorpus \QueryBuilder \DatabaseSession ;
811
912class AnonymizerRegistry
1013{
@@ -26,7 +29,10 @@ class AnonymizerRegistry
2629 Core \StringAnonymizer::class,
2730 ];
2831
29- private ?array $ anonymizers = null ;
32+ /** @var array<string, string> */
33+ private ?array $ classes = null ;
34+ /** @var array<string, AsAnonymizer> */
35+ private ?array $ metadata = null ;
3036 private array $ paths = [];
3137
3238 public function __construct (?array $ paths = null )
@@ -45,42 +51,70 @@ public function addPath(array $paths): void
4551 /**
4652 * Get all registered anonymizers classe names.
4753 *
48- * @return array<string,string>
49- * Keys are names, values are class names.
54+ * @return array<string,AsAnonymizer>
5055 */
51- public function getAnonymizers (): array
56+ public function getAllAnonymizerMetadata (): array
5257 {
5358 $ this ->initialize ();
5459
55- return $ this ->anonymizers ;
60+ return $ this ->metadata ;
5661 }
5762
5863 /**
59- * Get anonymizer class name.
60- *
61- * @param string $name
62- * Anonymizer name.
63- *
64- * @return string
65- * Anonymizer class name.
64+ * Create anonymizer instance.
65+ */
66+ public function createAnonymizer (
67+ string $ name ,
68+ AnonymizerConfig $ config ,
69+ Options $ options ,
70+ DatabaseSession $ databaseSession ,
71+ ): AbstractAnonymizer {
72+ $ className = $ this ->getAnonymizerClass ($ name );
73+
74+ return new $ className ($ config ->table , $ config ->targetName , $ databaseSession , $ options );
75+ }
76+
77+ /**
78+ * Get anonymizer metadata.
79+ */
80+ public function getAnonymizerMetadata (string $ name ): AsAnonymizer
81+ {
82+ $ this ->initialize ();
83+
84+ return $ this ->metadata [$ name ] ?? $ this ->throwAnonymizerDoesNotExist ($ name );
85+ }
86+
87+ /**
88+ * @internal
89+ * For unit tests only, please do not use.
6690 */
67- public function get (string $ name ): string
91+ public function getAnonymizerClass (string $ name ): string
6892 {
6993 $ this ->initialize ();
7094
71- return $ this ->anonymizers [$ name ] ?? throw new \InvalidArgumentException (\sprintf ("Can't find Anonymizer with name : %s, check your configuration. " , $ name ));
95+ return $ this ->classes [$ name ] ?? $ this ->throwAnonymizerDoesNotExist ($ name );
96+ }
97+
98+ private function getAnonymizatorClassMetadata (string $ className ): AsAnonymizer
99+ {
100+ if ($ attributes = (new \ReflectionClass ($ className ))->getAttributes (AsAnonymizer::class)) {
101+ return $ attributes [0 ]->newInstance ();
102+ }
103+
104+ throw new \LogicException (\sprintf ("Class '%s' should have an '%s' attribute. " , $ className , AsAnonymizer::class));
72105 }
73106
74107 /**
75108 * Lazy initialization.
76109 */
77110 private function initialize (): void
78111 {
79- if (null !== $ this ->anonymizers ) {
112+ if (null !== $ this ->classes ) {
80113 return ;
81114 }
82115
83- $ this ->anonymizers = [];
116+ $ this ->classes = [];
117+ $ this ->metadata = [];
84118
85119 foreach (self ::$ coreAnonymizers as $ className ) {
86120 $ this ->addAnonymizer ($ className , true );
@@ -152,7 +186,11 @@ private function addAnonymizer(string $className, bool $failOnError = false): vo
152186 return ;
153187 }
154188
155- $ this ->anonymizers [$ className ::id ()] = $ className ;
189+ $ metadata = $ this ->getAnonymizatorClassMetadata ($ className );
190+ $ id = $ metadata ->id ();
191+
192+ $ this ->classes [$ id ] = $ className ;
193+ $ this ->metadata [$ id ] = $ metadata ;
156194 }
157195
158196 /**
@@ -172,4 +210,9 @@ private function locatePacks(): void
172210 }
173211 }
174212 }
213+
214+ private function throwAnonymizerDoesNotExist (string $ name ): never
215+ {
216+ throw new \InvalidArgumentException (\sprintf ("Can't find Anonymizer with name : %s, check your configuration. " , $ name ));
217+ }
175218}
0 commit comments