@@ -24,74 +24,23 @@ class TypeGenerator
2424 public const MODE_MAPPING_ONLY = 2 ;
2525 public const MODE_WRITE = 4 ;
2626 public const MODE_OVERRIDE = 8 ;
27-
2827 public const GRAPHQL_SERVICES = 'services ' ;
2928
3029 private static bool $ classMapLoaded = false ;
31- private ?string $ cacheDir ;
32- protected int $ cacheDirMask ;
33- private array $ configs ;
34- private bool $ useClassMap ;
35- private ?string $ baseCacheDir ;
36- private string $ classNamespace ;
30+ public TypeGeneratorOptions $ options ;
3731 private TypeBuilder $ typeBuilder ;
38- private EventDispatcherInterface $ eventDispatcher ;
39-
40- public function __construct (
41- string $ classNamespace ,
42- ?string $ cacheDir ,
43- array $ configs ,
44- TypeBuilder $ typeBuilder ,
45- EventDispatcherInterface $ eventDispatcher ,
46- bool $ useClassMap = true ,
47- ?string $ baseCacheDir = null ,
48- ?int $ cacheDirMask = null
49- ) {
50- $ this ->cacheDir = $ cacheDir ;
51- $ this ->configs = $ configs ;
52- $ this ->useClassMap = $ useClassMap ;
53- $ this ->baseCacheDir = $ baseCacheDir ;
54- $ this ->typeBuilder = $ typeBuilder ;
55- $ this ->eventDispatcher = $ eventDispatcher ;
56- $ this ->classNamespace = $ classNamespace ;
57-
58- if (null === $ cacheDirMask ) {
59- // Apply permission 0777 for default cache dir otherwise apply 0775.
60- $ cacheDirMask = null === $ cacheDir ? 0777 : 0775 ;
61- }
62-
63- $ this ->cacheDirMask = $ cacheDirMask ;
64- }
32+ private EventDispatcherInterface $ dispatcher ;
6533
66- public function getBaseCacheDir (): ? string
34+ public function __construct ( TypeBuilder $ typeBuilder , EventDispatcherInterface $ dispatcher , TypeGeneratorOptions $ options )
6735 {
68- return $ this ->baseCacheDir ;
69- }
70-
71- public function setBaseCacheDir (string $ baseCacheDir ): void
72- {
73- $ this ->baseCacheDir = $ baseCacheDir ;
74- }
75-
76- public function getCacheDir (bool $ useDefault = true ): ?string
77- {
78- if ($ useDefault ) {
79- return $ this ->cacheDir ?: $ this ->baseCacheDir .'/overblog/graphql-bundle/__definitions__ ' ;
80- } else {
81- return $ this ->cacheDir ;
82- }
83- }
84-
85- public function setCacheDir (?string $ cacheDir ): self
86- {
87- $ this ->cacheDir = $ cacheDir ;
88-
89- return $ this ;
36+ $ this ->typeBuilder = $ typeBuilder ;
37+ $ this ->dispatcher = $ dispatcher ;
38+ $ this ->options = $ options ;
9039 }
9140
9241 public function compile (int $ mode ): array
9342 {
94- $ cacheDir = $ this ->getCacheDir ();
43+ $ cacheDir = $ this ->options -> getCacheDirOrDefault ();
9544 $ writeMode = $ mode & self ::MODE_WRITE ;
9645
9746 // Configure write mode
@@ -101,19 +50,19 @@ public function compile(int $mode): array
10150 }
10251
10352 // Process configs
104- $ configs = Processor::process ($ this ->configs );
53+ $ types = Processor::process ($ this ->options -> types );
10554
10655 // Generate classes
10756 $ classes = [];
108- foreach ($ configs as $ name => $ config ) {
57+ foreach ($ types as $ name => $ config ) {
10958 $ config ['config ' ]['name ' ] ??= $ name ;
11059 $ config ['config ' ]['class_name ' ] = $ config ['class_name ' ];
11160 $ classMap = $ this ->generateClass ($ config , $ cacheDir , $ mode );
11261 $ classes = array_merge ($ classes , $ classMap );
11362 }
11463
11564 // Create class map file
116- if ($ writeMode && $ this ->useClassMap && count ($ classes ) > 0 ) {
65+ if ($ writeMode && $ this ->options -> useClassMap && count ($ classes ) > 0 ) {
11766 $ content = "<?php \nreturn " .var_export ($ classes , true ).'; ' ;
11867
11968 // replaced hard-coded absolute paths by __DIR__
@@ -125,7 +74,7 @@ public function compile(int $mode): array
12574 $ this ->loadClasses (true );
12675 }
12776
128- $ this ->eventDispatcher ->dispatch (new SchemaCompiledEvent ());
77+ $ this ->dispatcher ->dispatch (new SchemaCompiledEvent ());
12978
13079 return $ classes ;
13180 }
@@ -145,12 +94,14 @@ public function generateClass(array $config, ?string $outputDirectory, int $mode
14594 }
14695 }
14796
148- return ["$ this ->classNamespace \\$ className " => $ path ];
97+ $ namespace = $ this ->options ->namespace ;
98+
99+ return ["$ namespace \\$ className " => $ path ];
149100 }
150101
151102 public function loadClasses (bool $ forceReload = false ): void
152103 {
153- if ($ this ->useClassMap && (!self ::$ classMapLoaded || $ forceReload )) {
104+ if ($ this ->options -> useClassMap && (!self ::$ classMapLoaded || $ forceReload )) {
154105 $ classMapFile = $ this ->getClassesMap ();
155106 $ classes = file_exists ($ classMapFile ) ? require $ classMapFile : [];
156107
@@ -173,6 +124,6 @@ public function loadClasses(bool $forceReload = false): void
173124
174125 private function getClassesMap (): string
175126 {
176- return $ this ->getCacheDir ().'/__classes.map ' ;
127+ return $ this ->options -> getCacheDirOrDefault ().'/__classes.map ' ;
177128 }
178129}
0 commit comments