@@ -24,74 +24,29 @@ 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+ private array $ typeConfigs ;
31+ private TypeGeneratorOptions $ options ;
3732 private TypeBuilder $ typeBuilder ;
38- private EventDispatcherInterface $ eventDispatcher ;
33+ private EventDispatcherInterface $ dispatcher ;
3934
4035 public function __construct (
41- string $ classNamespace ,
42- ?string $ cacheDir ,
43- array $ configs ,
36+ array $ typeConfigs ,
4437 TypeBuilder $ typeBuilder ,
45- EventDispatcherInterface $ eventDispatcher ,
46- bool $ useClassMap = true ,
47- ?string $ baseCacheDir = null ,
48- ?int $ cacheDirMask = null
38+ EventDispatcherInterface $ dispatcher ,
39+ TypeGeneratorOptions $ options
4940 ) {
50- $ this ->cacheDir = $ cacheDir ;
51- $ this ->configs = $ configs ;
52- $ this ->useClassMap = $ useClassMap ;
53- $ this ->baseCacheDir = $ baseCacheDir ;
41+ $ this ->typeConfigs = $ typeConfigs ;
5442 $ 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- }
65-
66- public function getBaseCacheDir (): ?string
67- {
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 ;
43+ $ this ->dispatcher = $ dispatcher ;
44+ $ this ->options = $ options ;
9045 }
9146
9247 public function compile (int $ mode ): array
9348 {
94- $ cacheDir = $ this ->getCacheDir ();
49+ $ cacheDir = $ this ->getCacheDirOrDefault ();
9550 $ writeMode = $ mode & self ::MODE_WRITE ;
9651
9752 // Configure write mode
@@ -100,20 +55,17 @@ public function compile(int $mode): array
10055 $ fs ->remove ($ cacheDir );
10156 }
10257
103- // Process configs
104- $ configs = Processor::process ($ this ->configs );
105-
10658 // Generate classes
10759 $ classes = [];
108- foreach ($ configs as $ name => $ config ) {
60+ foreach (Processor:: process ( $ this -> typeConfigs ) as $ name => $ config ) {
10961 $ config ['config ' ]['name ' ] ??= $ name ;
11062 $ config ['config ' ]['class_name ' ] = $ config ['class_name ' ];
11163 $ classMap = $ this ->generateClass ($ config , $ cacheDir , $ mode );
11264 $ classes = array_merge ($ classes , $ classMap );
11365 }
11466
11567 // Create class map file
116- if ($ writeMode && $ this ->useClassMap && count ($ classes ) > 0 ) {
68+ if ($ writeMode && $ this ->options -> useClassMap && count ($ classes ) > 0 ) {
11769 $ content = "<?php \nreturn " .var_export ($ classes , true ).'; ' ;
11870
11971 // replaced hard-coded absolute paths by __DIR__
@@ -125,7 +77,7 @@ public function compile(int $mode): array
12577 $ this ->loadClasses (true );
12678 }
12779
128- $ this ->eventDispatcher ->dispatch (new SchemaCompiledEvent ());
80+ $ this ->dispatcher ->dispatch (new SchemaCompiledEvent ());
12981
13082 return $ classes ;
13183 }
@@ -145,12 +97,14 @@ public function generateClass(array $config, ?string $outputDirectory, int $mode
14597 }
14698 }
14799
148- return ["$ this ->classNamespace \\$ className " => $ path ];
100+ $ namespace = $ this ->options ->namespace ;
101+
102+ return ["$ namespace \\$ className " => $ path ];
149103 }
150104
151105 public function loadClasses (bool $ forceReload = false ): void
152106 {
153- if ($ this ->useClassMap && (!self ::$ classMapLoaded || $ forceReload )) {
107+ if ($ this ->options -> useClassMap && (!self ::$ classMapLoaded || $ forceReload )) {
154108 $ classMapFile = $ this ->getClassesMap ();
155109 $ classes = file_exists ($ classMapFile ) ? require $ classMapFile : [];
156110
@@ -171,8 +125,33 @@ public function loadClasses(bool $forceReload = false): void
171125 }
172126 }
173127
128+ public function getCacheDir (): ?string
129+ {
130+ return $ this ->options ->cacheDir ;
131+ }
132+
133+ public function getCacheDirMask (): int
134+ {
135+ return $ this ->options ->cacheDirMask ;
136+ }
137+
138+ public function getCacheBaseDir (): ?string
139+ {
140+ return $ this ->options ->cacheBaseDir ;
141+ }
142+
143+ public function setCacheBaseDir (string $ dir ): void
144+ {
145+ $ this ->options ->cacheBaseDir = $ dir ;
146+ }
147+
148+ public function getCacheDirOrDefault (): string
149+ {
150+ return $ this ->options ->cacheDir ?? $ this ->options ->cacheBaseDir .'/overblog/graphql-bundle/__definitions__ ' ;
151+ }
152+
174153 private function getClassesMap (): string
175154 {
176- return $ this ->getCacheDir ().'/__classes.map ' ;
155+ return $ this ->getCacheDirOrDefault ().'/__classes.map ' ;
177156 }
178157}
0 commit comments