1717use Tempest \Generation \Exceptions \FileGenerationWasAborted ;
1818use Tempest \Generation \StubFileGenerator ;
1919use Tempest \Reflection \FunctionReflector ;
20+ use Tempest \Support \Filesystem ;
21+ use Tempest \Support \Json ;
2022use Tempest \Support \Str \ImmutableString ;
2123use Tempest \Validation \Rules \EndsWith ;
2224use Tempest \Validation \Rules \NotEmpty ;
3234use function Tempest \Support \str ;
3335use function Tempest \Support \Str \class_basename ;
3436
35- use const JSON_PRETTY_PRINT ;
36- use const JSON_UNESCAPED_SLASHES ;
37-
3837/**
3938 * Provides a bunch of methods to publish and generate files and work with common user input.
4039 */
@@ -129,13 +128,13 @@ public function publish(string $source, string $destination, ?Closure $callback
129128 public function publishImports (): void
130129 {
131130 foreach ($ this ->publishedFiles as $ file ) {
132- $ contents = str (file_get_contents ($ file ));
131+ $ contents = str (Filesystem \read_file ($ file ));
133132
134133 foreach ($ this ->publishedClasses as $ old => $ new ) {
135134 $ contents = $ contents ->replace ($ old , $ new );
136135 }
137136
138- file_put_contents ($ file , $ contents );
137+ Filesystem \write_file ($ file , $ contents );
139138 }
140139 }
141140
@@ -188,7 +187,7 @@ public function promptTargetPath(string $suggestedPath, ?array $rules = null): s
188187 */
189188 public function askForOverride (string $ targetPath ): bool
190189 {
191- if (! file_exists ($ targetPath )) {
190+ if (! Filesystem \is_file ($ targetPath )) {
192191 return true ;
193192 }
194193
@@ -206,15 +205,15 @@ public function askForOverride(string $targetPath): bool
206205 */
207206 public function update (string $ path , Closure $ callback , bool $ ignoreNonExisting = false ): void
208207 {
209- if (! is_file ($ path )) {
208+ if (! Filesystem \ is_file ($ path )) {
210209 if ($ ignoreNonExisting ) {
211210 return ;
212211 }
213212
214213 throw new Exception ("The file at path [ {$ path }] does not exist. " );
215214 }
216215
217- $ contents = file_get_contents ($ path );
216+ $ contents = Filesystem \read_file ($ path );
218217
219218 $ reflector = new FunctionReflector ($ callback );
220219 $ type = $ reflector ->getParameters ()->current ()->getType ();
@@ -225,7 +224,7 @@ public function update(string $path, Closure $callback, bool $ignoreNonExisting
225224 default => throw new Exception ('The callback must accept a string or ImmutableString. ' ),
226225 };
227226
228- file_put_contents ($ path , $ contents );
227+ Filesystem \write_file ($ path , $ contents );
229228 }
230229
231230 /**
@@ -242,7 +241,7 @@ public function updateJson(string $path, Closure $callback, bool $ignoreNonExist
242241 function (string $ content ) use ($ callback ) {
243242 $ indent = $ this ->detectIndent ($ content );
244243
245- $ json = json_decode ($ content, associative: true );
244+ $ json = Json \decode ($ content );
246245 $ json = $ callback ($ json );
247246
248247 // PHP will output empty arrays for empty dependencies,
@@ -256,7 +255,7 @@ function (string $content) use ($callback) {
256255 $ content = preg_replace_callback (
257256 '/^ +/m ' ,
258257 fn ($ m ) => str_repeat ($ indent , strlen ($ m [0 ]) / 4 ),
259- json_encode ($ json , JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ),
258+ Json \encode ($ json , pretty: true ),
260259 );
261260
262261 return "{$ content }\n" ;
0 commit comments