55namespace Safe \Generator ;
66
77use Safe \Templating \Engine ;
8+ use Safe \Templating \Filesystem ;
89use Safe \XmlDocParser \ErrorType ;
910use Safe \XmlDocParser \Scanner ;
1011use Safe \XmlDocParser \Method ;
@@ -45,10 +46,10 @@ public function generatePhpFile(
4546 \mkdir ($ path );
4647 }
4748
48- $ this -> dumpTemplate ($ path . $ lcModule . '.php ' , 'Module.php.tpl ' , [
49+ Filesystem:: dumpFile ($ path . $ lcModule . '.php ' , $ this -> engine -> generate ( 'Module.php.tpl ' , [
4950 '{{exceptionName}} ' => self ::toExceptionName ($ module ),
5051 '{{functions}} ' => \implode (PHP_EOL , $ phpFunctions ),
51- ]);
52+ ])) ;
5253 }
5354 }
5455
@@ -102,9 +103,9 @@ private function getFunctionsNameList(array $functions): array
102103 */
103104 public function generateFunctionsList (array $ functions , string $ path ): void
104105 {
105- $ this -> dumpTemplate ($ path , 'FunctionList.php.tpl ' , [
106+ Filesystem:: dumpFile ($ path , $ this -> engine -> generate ( 'FunctionList.php.tpl ' , [
106107 '{{functionNames}} ' => \implode (PHP_EOL , \array_map (static fn (string $ name ): string => \sprintf ('\'%s \', ' , $ name ), $ this ->getFunctionsNameList ($ functions ))),
107- ]);
108+ ])) ;
108109 }
109110
110111 /**
@@ -114,23 +115,29 @@ public function generateFunctionsList(array $functions, string $path): void
114115 */
115116 public function generateRectorFile (array $ functions , string $ path ): void
116117 {
117- $ this -> dumpTemplate ($ path , 'RectorConfig.php.tpl ' , [
118+ Filesystem:: dumpFile ($ path , $ this -> engine -> generate ( 'RectorConfig.php.tpl ' , [
118119 '{{functionNames}} ' => \implode (PHP_EOL , \array_map (static fn (string $ name ): string => \sprintf ('\'%1$s \' => \'Safe \\%1$s \', ' , $ name ), $ this ->getFunctionsNameList ($ functions ))),
119- ]);
120+ ])) ;
120121 }
121122
122123 public function createExceptionFile (string $ moduleName ): void
123124 {
124125 $ exceptionName = self ::toExceptionName ($ moduleName );
125126
126- $ this -> dumpTemplate (FileCreator:: getSafeRootDir () . '/generated/ Exceptions/ ' .$ exceptionName .'.php ' , 'Exception.php.tpl ' , [
127+ Filesystem:: dumpFile (Filesystem:: outputDir () . '/Exceptions/ ' .$ exceptionName .'.php ' , $ this -> engine -> generate ( 'Exception.php.tpl ' , [
127128 '{{exceptionName}} ' => $ exceptionName ,
128- ]);
129+ ])) ;
129130 }
130131
131132 public static function getSafeRootDir (): string
132133 {
133- return __DIR__ . '/../../.. ' ;
134+ $ path = realpath (__DIR__ . '/../../.. ' );
135+
136+ if (false === $ path ) {
137+ throw new \RuntimeException ('Unable to locate root directory ' );
138+ }
139+
140+ return $ path ;
134141 }
135142
136143 /**
@@ -140,16 +147,4 @@ public static function toExceptionName(string $moduleName): string
140147 {
141148 return str_replace ('- ' , '' , \ucfirst ($ moduleName )).'Exception ' ;
142149 }
143-
144- /**
145- * @param array<string, string> $context
146- */
147- private function dumpTemplate (string $ target , string $ template , array $ context = []): void
148- {
149- $ result = file_put_contents ($ target , $ this ->engine ->generate ($ template , $ context ));
150-
151- if (false === $ result ) {
152- throw new \RuntimeException (\sprintf ('Could not write to "%s". ' , $ target ));
153- }
154- }
155150}
0 commit comments