Passed
Pull Request — master (#56)
by Théo
02:16
created

remove()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the box project.
7
 *
8
 * (c) Kevin Herrera <[email protected]>
9
 *     Théo Fidry <[email protected]>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14
15
namespace KevinGH\Box\FileSystem;
16
17
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
18
use Symfony\Component\Filesystem\Exception\IOException;
19
use Traversable;
20
21
/**
22
 * Canonicalizes the given path.
23
 *
24
 * During normalization, all slashes are replaced by forward slashes ("/").
25
 * Furthermore, all "." and ".." segments are removed as far as possible.
26
 * ".." segments at the beginning of relative paths are not removed.
27
 *
28
 * ```php
29
 * echo Path::canonicalize("\webmozart\puli\..\css\style.css");
30
 * // => /webmozart/css/style.css
31
 *
32
 * echo Path::canonicalize("../css/./style.css");
33
 * // => ../css/style.css
34
 * ```
35
 *
36
 * This method is able to deal with both UNIX and Windows paths.
37
 *
38
 * @param string $path A path string
39
 *
40
 * @return string The canonical path
41
 */
42
function canonicalize(string $path): string
43
{
44
    static $fileSystem;
45
46
    if (null === $fileSystem) {
47
        $fileSystem = new FileSystem();
48
    }
49
50
    return $fileSystem->canonicalize($path);
51
}
52
53
/**
54
 * Normalizes the given path.
55
 *
56
 * During normalization, all slashes are replaced by forward slashes ("/").
57
 * Contrary to {@link canonicalize()}, this method does not remove invalid
58
 * or dot path segments. Consequently, it is much more efficient and should
59
 * be used whenever the given path is known to be a valid, absolute system
60
 * path.
61
 *
62
 * This method is able to deal with both UNIX and Windows paths.
63
 *
64
 * @param string $path a path string
65
 *
66
 * @return string the normalized path
67
 */
68
function normalize(string $path): string
69
{
70
    static $fileSystem;
71
72
    if (null === $fileSystem) {
73
        $fileSystem = new FileSystem();
74
    }
75
76
    return $fileSystem->normalize($path);
77
}
78
79
/**
80
 * Returns the directory part of the path.
81
 *
82
 * This method is similar to PHP's dirname(), but handles various cases
83
 * where dirname() returns a weird result:
84
 *
85
 *  - dirname() does not accept backslashes on UNIX
86
 *  - dirname("C:/webmozart") returns "C:", not "C:/"
87
 *  - dirname("C:/") returns ".", not "C:/"
88
 *  - dirname("C:") returns ".", not "C:/"
89
 *  - dirname("webmozart") returns ".", not ""
90
 *  - dirname() does not canonicalize the result
91
 *
92
 * This method fixes these shortcomings and behaves like dirname()
93
 * otherwise.
94
 *
95
 * The result is a canonical path.
96
 *
97
 * @param string $path a path string
98
 *
99
 * @return string The canonical directory part. Returns the root directory
100
 *                if the root directory is passed. Returns an empty string
101
 *                if a relative path is passed that contains no slashes.
102
 *                Returns an empty string if an empty string is passed.
103
 */
104
function directory(string $path): string
105
{
106
    static $fileSystem;
107
108
    if (null === $fileSystem) {
109
        $fileSystem = new FileSystem();
110
    }
111
112
    return $fileSystem->getDirectory($path);
113
}
114
115
/**
116
 * Returns canonical path of the user's home directory.
117
 *
118
 * Supported operating systems:
119
 *
120
 *  - UNIX
121
 *  - Windows8 and upper
122
 *
123
 * If your operation system or environment isn't supported, an exception is thrown.
124
 *
125
 * The result is a canonical path.
126
 *
127
 * @return string The canonical home directory
128
 */
129
function home_directory(): string
130
{
131
    static $fileSystem;
132
133
    if (null === $fileSystem) {
134
        $fileSystem = new FileSystem();
135
    }
136
137
    return $fileSystem->getHomeDirectory();
138
}
139
140
/**
141
 * Returns the root directory of a path.
142
 *
143
 * The result is a canonical path.
144
 *
145
 * @param string $path a path string
146
 *
147
 * @return string The canonical root directory. Returns an empty string if
148
 *                the given path is relative or empty.
149
 */
150
function root(string $path): string
151
{
152
    static $fileSystem;
153
154
    if (null === $fileSystem) {
155
        $fileSystem = new FileSystem();
156
    }
157
158
    return $fileSystem->getRoot($path);
159
}
160
161
/**
162
 * Returns the file name from a file path.
163
 *
164
 * @param string $path the path string
165
 *
166
 * @return string The file name
167
 */
168
function filename(string $path): string
169
{
170
    static $fileSystem;
171
172
    if (null === $fileSystem) {
173
        $fileSystem = new FileSystem();
174
    }
175
176
    return $fileSystem->getFilename($path);
177
}
178
179
/**
180
 * Returns the file name without the extension from a file path.
181
 *
182
 * @param string      $path      the path string
183
 * @param null|string $extension if specified, only that extension is cut
184
 *                               off (may contain leading dot)
185
 *
186
 * @return string the file name without extension
187
 */
188
function filename_without_extension($path, $extension = null)
189
{
190
    static $fileSystem;
191
192
    if (null === $fileSystem) {
193
        $fileSystem = new FileSystem();
194
    }
195
196
    return $fileSystem->getFilenameWithoutExtension($path, $extension);
197
}
198
199
/**
200
 * Returns the extension from a file path.
201
 *
202
 * @param string $path           the path string
203
 * @param bool   $forceLowerCase forces the extension to be lower-case
204
 *                               (requires mbstring extension for correct
205
 *                               multi-byte character handling in extension)
206
 *
207
 * @return string the extension of the file path (without leading dot)
208
 */
209
function extension(string $path, bool $forceLowerCase = false): string
210
{
211
    static $fileSystem;
212
213
    if (null === $fileSystem) {
214
        $fileSystem = new FileSystem();
215
    }
216
217
    return $fileSystem->getExtension($path, $forceLowerCase);
218
}
219
220
/**
221
 * Returns whether the path has an extension.
222
 *
223
 * @param string            $path       the path string
224
 * @param null|array|string $extensions if null or not provided, checks if
225
 *                                      an extension exists, otherwise
226
 *                                      checks for the specified extension
227
 *                                      or array of extensions (with or
228
 *                                      without leading dot)
229
 * @param bool              $ignoreCase whether to ignore case-sensitivity
230
 *                                      (requires mbstring extension for
231
 *                                      correct multi-byte character
232
 *                                      handling in the extension)
233
 *
234
 * @return bool returns `true` if the path has an (or the specified)
235
 *              extension and `false` otherwise
236
 */
237
function has_extension(string $path, $extensions = null, bool $ignoreCase = false): bool
238
{
239
    static $fileSystem;
240
241
    if (null === $fileSystem) {
242
        $fileSystem = new FileSystem();
243
    }
244
245
    return $fileSystem->hasExtension($path, $extensions, $ignoreCase);
246
}
247
248
/**
249
 * Changes the extension of a path string.
250
 *
251
 * @param string $path      The path string with filename.ext to change.
252
 * @param string $extension new extension (with or without leading dot)
253
 *
254
 * @return string the path string with new file extension
255
 */
256
function change_extension(string $path, string $extension): string
257
{
258
    static $fileSystem;
259
260
    if (null === $fileSystem) {
261
        $fileSystem = new FileSystem();
262
    }
263
264
    return $fileSystem->changeExtension($path, $extension);
265
}
266
267
/**
268
 * Returns whether the file path is an absolute path.
269
 *
270
 * @param string $path a path string
271
 *
272
 * @return bool
273
 */
274
function is_absolute_path(string $path): bool
275
{
276
    static $fileSystem;
277
278
    if (null === $fileSystem) {
279
        $fileSystem = new FileSystem();
280
    }
281
282
    return $fileSystem->isAbsolutePath($path);
283
}
284
285
/**
286
 * Returns whether a path is relative.
287
 *
288
 * @param string $path a path string
289
 *
290
 * @return bool returns true if the path is relative or empty, false if
291
 *              it is absolute
292
 */
293
function is_relative_path(string $path): bool
294
{
295
    static $fileSystem;
296
297
    if (null === $fileSystem) {
298
        $fileSystem = new FileSystem();
299
    }
300
301
    return $fileSystem->isRelativePath($path);
302
}
303
304
/**
305
 * Turns a relative path into an absolute path.
306
 *
307
 * Usually, the relative path is appended to the given base path. Dot
308
 * segments ("." and "..") are removed/collapsed and all slashes turned
309
 * into forward slashes.
310
 *
311
 * ```php
312
 * echo Path::makeAbsolute("../style.css", "/webmozart/puli/css");
313
 * // => /webmozart/puli/style.css
314
 * ```
315
 *
316
 * If an absolute path is passed, that path is returned unless its root
317
 * directory is different than the one of the base path. In that case, an
318
 * exception is thrown.
319
 *
320
 * ```php
321
 * Path::makeAbsolute("/style.css", "/webmozart/puli/css");
322
 * // => /style.css
323
 *
324
 * Path::makeAbsolute("C:/style.css", "C:/webmozart/puli/css");
325
 * // => C:/style.css
326
 *
327
 * Path::makeAbsolute("C:/style.css", "/webmozart/puli/css");
328
 * // InvalidArgumentException
329
 * ```
330
 *
331
 * If the base path is not an absolute path, an exception is thrown.
332
 *
333
 * The result is a canonical path.
334
 *
335
 * @param string $path     a path to make absolute
336
 * @param string $basePath an absolute base path
337
 *
338
 * @return string an absolute path in canonical form
339
 */
340
function make_path_absolute(string $path, string $basePath): string
341
{
342
    static $fileSystem;
343
344
    if (null === $fileSystem) {
345
        $fileSystem = new FileSystem();
346
    }
347
348
    return $fileSystem->makeAbsolute($path, $basePath);
349
}
350
351
/**
352
 * Turns a path into a relative path.
353
 *
354
 * The relative path is created relative to the given base path:
355
 *
356
 * ```php
357
 * echo Path::makeRelative("/webmozart/style.css", "/webmozart/puli");
358
 * // => ../style.css
359
 * ```
360
 *
361
 * If a relative path is passed and the base path is absolute, the relative
362
 * path is returned unchanged:
363
 *
364
 * ```php
365
 * Path::makeRelative("style.css", "/webmozart/puli/css");
366
 * // => style.css
367
 * ```
368
 *
369
 * If both paths are relative, the relative path is created with the
370
 * assumption that both paths are relative to the same directory:
371
 *
372
 * ```php
373
 * Path::makeRelative("style.css", "webmozart/puli/css");
374
 * // => ../../../style.css
375
 * ```
376
 *
377
 * If both paths are absolute, their root directory must be the same,
378
 * otherwise an exception is thrown:
379
 *
380
 * ```php
381
 * Path::makeRelative("C:/webmozart/style.css", "/webmozart/puli");
382
 * // InvalidArgumentException
383
 * ```
384
 *
385
 * If the passed path is absolute, but the base path is not, an exception
386
 * is thrown as well:
387
 *
388
 * ```php
389
 * Path::makeRelative("/webmozart/style.css", "webmozart/puli");
390
 * // InvalidArgumentException
391
 * ```
392
 *
393
 * If the base path is not an absolute path, an exception is thrown.
394
 *
395
 * The result is a canonical path.
396
 *
397
 * @param string $path     a path to make relative
398
 * @param string $basePath a base path
399
 *
400
 * @return string a relative path in canonical form
401
 */
402
function make_path_relative(string $path, string $basePath): string
403
{
404
    static $fileSystem;
405
406
    if (null === $fileSystem) {
407
        $fileSystem = new FileSystem();
408
    }
409
410
    return $fileSystem->makeRelative($path, $basePath);
411
}
412
413
/**
414
 * Returns whether the given path is on the local filesystem.
415
 *
416
 * @param string $path a path string
417
 *
418
 * @return bool returns true if the path is local, false for a URL
419
 */
420
function is_local(string $path): bool
421
{
422
    static $fileSystem;
423
424
    if (null === $fileSystem) {
425
        $fileSystem = new FileSystem();
426
    }
427
428
    return $fileSystem->isLocal($path);
429
}
430
431
/**
432
 * Returns the longest common base path of a set of paths.
433
 *
434
 * Dot segments ("." and "..") are removed/collapsed and all slashes turned
435
 * into forward slashes.
436
 *
437
 * ```php
438
 * $basePath = Path::getLongestCommonBasePath(array(
439
 *     '/webmozart/css/style.css',
440
 *     '/webmozart/css/..'
441
 * ));
442
 * // => /webmozart
443
 * ```
444
 *
445
 * The root is returned if no common base path can be found:
446
 *
447
 * ```php
448
 * $basePath = Path::getLongestCommonBasePath(array(
449
 *     '/webmozart/css/style.css',
450
 *     '/puli/css/..'
451
 * ));
452
 * // => /
453
 * ```
454
 *
455
 * If the paths are located on different Windows partitions, `null` is
456
 * returned.
457
 *
458
 * ```php
459
 * $basePath = Path::getLongestCommonBasePath(array(
460
 *     'C:/webmozart/css/style.css',
461
 *     'D:/webmozart/css/..'
462
 * ));
463
 * // => null
464
 * ```
465
 *
466
 * @param array $paths a list of paths
467
 *
468
 * @return null|string the longest common base path in canonical form or
469
 *                     `null` if the paths are on different Windows
470
 *                     partitions
471
 */
472
function longest_common_base_path(array $paths): ?string
473
{
474
    static $fileSystem;
475
476
    if (null === $fileSystem) {
477
        $fileSystem = new FileSystem();
478
    }
479
480
    return $fileSystem->getLongestCommonBasePath($paths);
481
}
482
483
/**
484
 * Joins two or more path strings.
485
 *
486
 * The result is a canonical path.
487
 *
488
 * @param string|string[] $paths path parts as parameters or array
489
 *
490
 * @return string the joint path
491
 */
492
function join($paths): string
493
{
494
    static $fileSystem;
495
496
    if (null === $fileSystem) {
497
        $fileSystem = new FileSystem();
498
    }
499
500
    return $fileSystem->join($paths);
501
}
502
503
/**
504
 * Returns whether a path is a base path of another path.
505
 *
506
 * Dot segments ("." and "..") are removed/collapsed and all slashes turned
507
 * into forward slashes.
508
 *
509
 * ```php
510
 * Path::isBasePath('/webmozart', '/webmozart/css');
511
 * // => true
512
 *
513
 * Path::isBasePath('/webmozart', '/webmozart');
514
 * // => true
515
 *
516
 * Path::isBasePath('/webmozart', '/webmozart/..');
517
 * // => false
518
 *
519
 * Path::isBasePath('/webmozart', '/puli');
520
 * // => false
521
 * ```
522
 *
523
 * @param string $basePath the base path to test
524
 * @param string $ofPath   the other path
525
 *
526
 * @return bool whether the base path is a base path of the other path
527
 */
528
function is_base_path(string $basePath, string $ofPath): bool
529
{
530
    static $fileSystem;
531
532
    if (null === $fileSystem) {
533
        $fileSystem = new FileSystem();
534
    }
535
536
    return $fileSystem->isBasePath($basePath, $ofPath);
537
}
538
539
function escape_path(string $path): string
540
{
541
    static $fileSystem;
542
543
    if (null === $fileSystem) {
544
        $fileSystem = new FileSystem();
545
    }
546
547
    return $fileSystem->escapePath($path);
548
}
549
550
/**
551
 * Gets the contents of a file.
552
 *
553
 * @param string $file File path
554
 *
555
 * @throws IOException If the file cannot be read
556
 *
557
 * @return string File contents
558
 */
559
function file_contents(string $file): string
560
{
561
    static $fileSystem;
562
563
    if (null === $fileSystem) {
564
        $fileSystem = new FileSystem();
565
    }
566
567
    return $fileSystem->getFileContents($file);
568
}
569
570
/**
571
 * Copies a file.
572
 *
573
 * If the target file is older than the origin file, it's always overwritten.
574
 * If the target file is newer, it is overwritten only when the
575
 * $overwriteNewerFiles option is set to true.
576
 *
577
 * @param string $originFile          The original filename
578
 * @param string $targetFile          The target filename
579
 * @param bool   $overwriteNewerFiles If true, target files newer than origin files are overwritten
580
 *
581
 * @throws FileNotFoundException When originFile doesn't exist
582
 * @throws IOException           When copy fails
583
 */
584
function copy(string $originFile, string $targetFile, bool $overwriteNewerFiles = false): void
585
{
586
    static $fileSystem;
587
588
    if (null === $fileSystem) {
589
        $fileSystem = new FileSystem();
590
    }
591
592
    $fileSystem->copy($originFile, $targetFile, $overwriteNewerFiles);
593
}
594
595
/**
596
 * Creates a directory recursively.
597
 *
598
 * @param iterable|string $dirs The directory path
599
 * @param int             $mode The directory mode
600
 *
601
 * @throws IOException On any directory creation failure
602
 */
603
function mkdir($dirs, int $mode = 0777): void
604
{
605
    static $fileSystem;
606
607
    if (null === $fileSystem) {
608
        $fileSystem = new FileSystem();
609
    }
610
611
    $fileSystem->mkdir($dirs, $mode);
612
}
613
614
/**
615
 * Removes files or directories.
616
 *
617
 * @param iterable|string $files A filename, an array of files, or a \Traversable instance to remove
618
 *
619
 * @throws IOException When removal fails
620
 */
621
function remove($files): void
622
{
623
    static $fileSystem;
624
625
    if (null === $fileSystem) {
626
        $fileSystem = new FileSystem();
627
    }
628
629
    $fileSystem->remove($files);
630
}
631
632
/**
633
 * Checks the existence of files or directories.
634
 *
635
 * @param iterable|string $files A filename, an array of files, or a \Traversable instance to check
636
 *
637
 * @return bool true if the file exists, false otherwise
638
 */
639
function exists($files): bool
640
{
641
    static $fileSystem;
642
643
    if (null === $fileSystem) {
644
        $fileSystem = new FileSystem();
645
    }
646
647
    return $fileSystem->exists($files);
648
}
649
650
/**
651
 * Sets access and modification time of file.
652
 *
653
 * @param iterable|string $files A filename, an array of files, or a \Traversable instance to create
654
 * @param int             $time  The touch time as a Unix timestamp
655
 * @param int             $atime The access time as a Unix timestamp
656
 *
657
 * @throws IOException When touch fails
658
 */
659
function touch($files, int $time = null, int $atime = null): void
660
{
661
    static $fileSystem;
662
663
    if (null === $fileSystem) {
664
        $fileSystem = new FileSystem();
665
    }
666
667
    $fileSystem->touch($files, $time, $atime);
668
}
669
670
/**
671
 * Change mode for an array of files or directories.
672
 *
673
 * @param iterable|string $files     A filename, an array of files, or a \Traversable instance to change mode
674
 * @param int             $mode      The new mode (octal)
675
 * @param int             $umask     The mode mask (octal)
676
 * @param bool            $recursive Whether change the mod recursively or not
677
 *
678
 * @throws IOException When the change fail
679
 */
680
function chmod($files, int $mode, int $umask = 0000, bool $recursive = false): void
681
{
682
    static $fileSystem;
683
684
    if (null === $fileSystem) {
685
        $fileSystem = new FileSystem();
686
    }
687
688
    $fileSystem->chmod($files, $mode, $umask, $recursive);
689
}
690
691
/**
692
 * Change the owner of an array of files or directories.
693
 *
694
 * @param iterable|string $files     A filename, an array of files, or a \Traversable instance to change owner
695
 * @param string          $user      The new owner user name
696
 * @param bool            $recursive Whether change the owner recursively or not
697
 *
698
 * @throws IOException When the change fail
699
 */
700
function chown($files, string $user, bool $recursive = false): void
701
{
702
    static $fileSystem;
703
704
    if (null === $fileSystem) {
705
        $fileSystem = new FileSystem();
706
    }
707
708
    $fileSystem->chown($files, $user, $recursive);
709
}
710
711
/**
712
 * Change the group of an array of files or directories.
713
 *
714
 * @param iterable|string $files     A filename, an array of files, or a \Traversable instance to change group
715
 * @param string          $group     The group name
716
 * @param bool            $recursive Whether change the group recursively or not
717
 *
718
 * @throws IOException When the change fail
719
 */
720
function chgrp($files, string $group, bool $recursive = false): void
721
{
722
    static $fileSystem;
723
724
    if (null === $fileSystem) {
725
        $fileSystem = new FileSystem();
726
    }
727
728
    $fileSystem->chgrp($files, $group, $recursive);
729
}
730
731
/**
732
 * Renames a file or a directory.
733
 *
734
 * @param string $origin    The origin filename or directory
735
 * @param string $target    The new filename or directory
736
 * @param bool   $overwrite Whether to overwrite the target if it already exists
737
 *
738
 * @throws IOException When target file or directory already exists
739
 * @throws IOException When origin cannot be renamed
740
 */
741
function rename(string $origin, string $target, bool $overwrite = false): void
742
{
743
    static $fileSystem;
744
745
    if (null === $fileSystem) {
746
        $fileSystem = new FileSystem();
747
    }
748
749
    $fileSystem->rename($origin, $target, $overwrite);
750
}
751
752
/**
753
 * Creates a symbolic link or copy a directory.
754
 *
755
 * @param string $originDir     The origin directory path
756
 * @param string $targetDir     The symbolic link name
757
 * @param bool   $copyOnWindows Whether to copy files if on Windows
758
 *
759
 * @throws IOException When symlink fails
760
 */
761
function symlink(string $originDir, string $targetDir, bool $copyOnWindows = false): void
762
{
763
    static $fileSystem;
764
765
    if (null === $fileSystem) {
766
        $fileSystem = new FileSystem();
767
    }
768
769
    $fileSystem->symlink($originDir, $targetDir, $copyOnWindows);
770
}
771
772
/**
773
 * Creates a hard link, or several hard links to a file.
774
 *
775
 * @param string          $originFile  The original file
776
 * @param string|string[] $targetFiles The target file(s)
777
 *
778
 * @throws FileNotFoundException When original file is missing or not a file
779
 * @throws IOException           When link fails, including if link already exists
780
 */
781
function hardlink(string $originFile, string $targetFiles): void
782
{
783
    static $fileSystem;
784
785
    if (null === $fileSystem) {
786
        $fileSystem = new FileSystem();
787
    }
788
789
    $fileSystem->hardlink($originFile, $targetFiles);
790
}
791
792
/**
793
 * Resolves links in paths.
794
 *
795
 * With $canonicalize = false (default)
796
 *      - if $path does not exist or is not a link, returns null
797
 *      - if $path is a link, returns the next direct target of the link without considering the existence of the target
798
 *
799
 * With $canonicalize = true
800
 *      - if $path does not exist, returns null
801
 *      - if $path exists, returns its absolute fully resolved final version
802
 *
803
 * @param string $path         A filesystem path
804
 * @param bool   $canonicalize Whether or not to return a canonicalized path
805
 *
806
 * @return null|string
807
 */
808
function readlink(string $path, bool $canonicalize = false): ?string
809
{
810
    static $fileSystem;
811
812
    if (null === $fileSystem) {
813
        $fileSystem = new FileSystem();
814
    }
815
816
    return $fileSystem->readlink($path, $canonicalize);
817
}
818
819
/**
820
 * Mirrors a directory to another.
821
 *
822
 * @param string      $originDir The origin directory
823
 * @param string      $targetDir The target directory
824
 * @param Traversable $iterator  A Traversable instance
825
 * @param array       $options   An array of boolean options
826
 *                               Valid options are:
827
 *                               - $options['override'] Whether to override an existing file on copy or not (see copy())
828
 *                               - $options['copy_on_windows'] Whether to copy files instead of links on Windows (see symlink())
829
 *                               - $options['delete'] Whether to delete files that are not in the source directory (defaults to false)
830
 *
831
 * @throws IOException When file type is unknown
832
 */
833
function mirror(string $originDir, string $targetDir, Traversable $iterator = null, array $options = []): void
834
{
835
    static $fileSystem;
836
837
    if (null === $fileSystem) {
838
        $fileSystem = new FileSystem();
839
    }
840
841
    $fileSystem->mirror($originDir, $targetDir, $iterator, $options);
842
}
843
844
/**
845
 * Creates a temporary directory.
846
 *
847
 * @param string $namespace the directory path in the system's temporary directory
848
 * @param string $className the name of the test class
849
 *
850
 * @return string the path to the created directory
851
 */
852
function make_tmp_dir(string $namespace, string $className): string
853
{
854
    static $fileSystem;
855
856
    if (null === $fileSystem) {
857
        $fileSystem = new FileSystem();
858
    }
859
860
    return $fileSystem->makeTmpDir($namespace, $className);
861
}
862
863
/**
864
 * Creates a temporary file with support for custom stream wrappers.
865
 *
866
 * @param string $dir    The directory where the temporary filename will be created
867
 * @param string $prefix The prefix of the generated temporary filename
868
 *                       Note: Windows uses only the first three characters of prefix
869
 *
870
 * @return string The new temporary filename (with path), or throw an exception on failure
871
 */
872
function tempnam($dir, $prefix)
873
{
874
    static $fileSystem;
875
876
    if (null === $fileSystem) {
877
        $fileSystem = new FileSystem();
878
    }
879
880
    return $fileSystem->tempnam($dir, $prefix);
881
}
882
883
/**
884
 * Atomically dumps content into a file.
885
 *
886
 * @param string $filename The file to be written to
887
 * @param string $content  The data to write into the file
888
 *
889
 * @throws IOException if the file cannot be written to
890
 */
891
function dump_file(string $filename, string $content): void
892
{
893
    static $fileSystem;
894
895
    if (null === $fileSystem) {
896
        $fileSystem = new FileSystem();
897
    }
898
899
    $fileSystem->dumpFile($filename, $content);
900
}
901
902
/**
903
 * Appends content to an existing file.
904
 *
905
 * @param string $filename The file to which to append content
906
 * @param string $content  The content to append
907
 *
908
 * @throws IOException If the file is not writable
909
 */
910
function append_to_file(string $filename, string $content): void
911
{
912
    static $fileSystem;
913
914
    if (null === $fileSystem) {
915
        $fileSystem = new FileSystem();
916
    }
917
918
    $fileSystem->appendToFile($filename, $content);
919
}
920