12
12
namespace Symfony \Component \Filesystem ;
13
13
14
14
use Symfony \Component \Filesystem \Exception \InvalidArgumentException ;
15
- use Symfony \Component \Filesystem \ExceptionInvalidArgumentException ;
16
15
17
16
/**
18
17
* Contains utility methods for handling path strings.
@@ -83,14 +82,14 @@ public static function canonicalize(string $path): string
83
82
84
83
// Replace "~" with user's home directory.
85
84
if ('~ ' === $ path [0 ]) {
86
- $ path = self ::getHomeDirectory ().\ substr ($ path , 1 );
85
+ $ path = self ::getHomeDirectory ().substr ($ path , 1 );
87
86
}
88
87
89
- $ path = \ str_replace ('\\' , '/ ' , $ path );
88
+ $ path = str_replace ('\\' , '/ ' , $ path );
90
89
91
90
[$ root , $ pathWithoutRoot ] = self ::split ($ path );
92
91
93
- $ parts = \ explode ('/ ' , $ pathWithoutRoot );
92
+ $ parts = explode ('/ ' , $ pathWithoutRoot );
94
93
$ canonicalParts = [];
95
94
96
95
// Collapse "." and "..", if possible
@@ -102,7 +101,7 @@ public static function canonicalize(string $path): string
102
101
// Collapse ".." with the previous part, if one exists
103
102
// Don't collapse ".." if the previous part is also ".."
104
103
if ('.. ' === $ part && \count ($ canonicalParts ) > 0 && '.. ' !== $ canonicalParts [\count ($ canonicalParts ) - 1 ]) {
105
- \ array_pop ($ canonicalParts );
104
+ array_pop ($ canonicalParts );
106
105
107
106
continue ;
108
107
}
@@ -114,7 +113,7 @@ public static function canonicalize(string $path): string
114
113
}
115
114
116
115
// Add the root directory again
117
- self ::$ buffer [$ path ] = $ canonicalPath = $ root .\ implode ('/ ' , $ canonicalParts );
116
+ self ::$ buffer [$ path ] = $ canonicalPath = $ root .implode ('/ ' , $ canonicalParts );
118
117
++self ::$ bufferSize ;
119
118
120
119
// Clean up regularly to prevent memory leaks
@@ -139,7 +138,7 @@ public static function canonicalize(string $path): string
139
138
*/
140
139
public static function normalize (string $ path ): string
141
140
{
142
- return \ str_replace ('\\' , '/ ' , $ path );
141
+ return str_replace ('\\' , '/ ' , $ path );
143
142
}
144
143
145
144
/**
@@ -174,9 +173,9 @@ public static function getDirectory(string $path): string
174
173
$ path = self ::canonicalize ($ path );
175
174
176
175
// Maintain scheme
177
- if (false !== ($ pos = \ strpos ($ path , ':// ' ))) {
178
- $ scheme = \ substr ($ path , 0 , $ pos + 3 );
179
- $ path = \ substr ($ path , $ pos + 3 );
176
+ if (false !== ($ pos = strpos ($ path , ':// ' ))) {
177
+ $ scheme = substr ($ path , 0 , $ pos + 3 );
178
+ $ path = substr ($ path , $ pos + 3 );
180
179
} else {
181
180
$ scheme = '' ;
182
181
}
@@ -188,11 +187,11 @@ public static function getDirectory(string $path): string
188
187
}
189
188
190
189
// Directory equals Windows root "C:/"
191
- if (2 === $ pos && \ ctype_alpha ($ path [0 ]) && ': ' === $ path [1 ]) {
192
- return $ scheme .\ substr ($ path , 0 , 3 );
190
+ if (2 === $ pos && ctype_alpha ($ path [0 ]) && ': ' === $ path [1 ]) {
191
+ return $ scheme .substr ($ path , 0 , 3 );
193
192
}
194
193
195
- return $ scheme .\ substr ($ path , 0 , $ pos );
194
+ return $ scheme .substr ($ path , 0 , $ pos );
196
195
}
197
196
198
197
return '' ;
@@ -215,13 +214,13 @@ public static function getDirectory(string $path): string
215
214
public static function getHomeDirectory (): string
216
215
{
217
216
// For UNIX support
218
- if (\ getenv ('HOME ' )) {
219
- return self ::canonicalize (\ getenv ('HOME ' ));
217
+ if (getenv ('HOME ' )) {
218
+ return self ::canonicalize (getenv ('HOME ' ));
220
219
}
221
220
222
221
// For >= Windows8 support
223
- if (\ getenv ('HOMEDRIVE ' ) && \ getenv ('HOMEPATH ' )) {
224
- return self ::canonicalize (\ getenv ('HOMEDRIVE ' ).\ getenv ('HOMEPATH ' ));
222
+ if (getenv ('HOMEDRIVE ' ) && getenv ('HOMEPATH ' )) {
223
+ return self ::canonicalize (getenv ('HOMEDRIVE ' ).getenv ('HOMEPATH ' ));
225
224
}
226
225
227
226
throw new \RuntimeException ("Cannot find the home directory path: Your environment or operation system isn't supported " );
@@ -242,9 +241,9 @@ public static function getRoot(string $path): string
242
241
}
243
242
244
243
// Maintain scheme
245
- if (false !== ($ pos = \ strpos ($ path , ':// ' ))) {
246
- $ scheme = \ substr ($ path , 0 , $ pos + 3 );
247
- $ path = \ substr ($ path , $ pos + 3 );
244
+ if (false !== ($ pos = strpos ($ path , ':// ' ))) {
245
+ $ scheme = substr ($ path , 0 , $ pos + 3 );
246
+ $ path = substr ($ path , $ pos + 3 );
248
247
} else {
249
248
$ scheme = '' ;
250
249
}
@@ -257,7 +256,7 @@ public static function getRoot(string $path): string
257
256
$ length = \strlen ($ path );
258
257
259
258
// Windows root
260
- if ($ length > 1 && \ ctype_alpha ($ path [0 ]) && ': ' === $ path [1 ]) {
259
+ if ($ length > 1 && ctype_alpha ($ path [0 ]) && ': ' === $ path [1 ]) {
261
260
// Special case: "C:"
262
261
if (2 === $ length ) {
263
262
return $ scheme .$ path .'/ ' ;
@@ -286,10 +285,10 @@ public static function getFilenameWithoutExtension(string $path, ?string $extens
286
285
287
286
if (null !== $ extension ) {
288
287
// remove extension and trailing dot
289
- return \ rtrim (\ basename ($ path , $ extension ), '. ' );
288
+ return rtrim (basename ($ path , $ extension ), '. ' );
290
289
}
291
290
292
- return \ pathinfo ($ path , PATHINFO_FILENAME );
291
+ return pathinfo ($ path , PATHINFO_FILENAME );
293
292
}
294
293
295
294
/**
@@ -303,7 +302,7 @@ public static function getExtension(string $path, bool $forceLowerCase = false):
303
302
return '' ;
304
303
}
305
304
306
- $ extension = \ pathinfo ($ path , PATHINFO_EXTENSION );
305
+ $ extension = pathinfo ($ path , PATHINFO_EXTENSION );
307
306
308
307
if ($ forceLowerCase ) {
309
308
$ extension = self ::toLower ($ extension );
@@ -346,7 +345,7 @@ public static function hasExtension(string $path, $extensions = null, bool $igno
346
345
}
347
346
348
347
// remove leading '.' in extensions array
349
- $ extensions [$ key ] = \ ltrim ($ extension , '. ' );
348
+ $ extensions [$ key ] = ltrim ($ extension , '. ' );
350
349
}
351
350
352
351
return \in_array ($ actualExtension , $ extensions , true );
@@ -367,19 +366,19 @@ public static function changeExtension(string $path, string $extension): string
367
366
}
368
367
369
368
$ actualExtension = self ::getExtension ($ path );
370
- $ extension = \ ltrim ($ extension , '. ' );
369
+ $ extension = ltrim ($ extension , '. ' );
371
370
372
371
// No extension for paths
373
- if ('/ ' === \ substr ($ path , -1 )) {
372
+ if ('/ ' === substr ($ path , -1 )) {
374
373
return $ path ;
375
374
}
376
375
377
376
// No actual extension in path
378
377
if (empty ($ actualExtension )) {
379
- return $ path .('. ' === \ substr ($ path , -1 ) ? '' : '. ' ).$ extension ;
378
+ return $ path .('. ' === substr ($ path , -1 ) ? '' : '. ' ).$ extension ;
380
379
}
381
380
382
- return \ substr ($ path , 0 , -\strlen ($ actualExtension )).$ extension ;
381
+ return substr ($ path , 0 , -\strlen ($ actualExtension )).$ extension ;
383
382
}
384
383
385
384
public static function isAbsolute (string $ path ): bool
@@ -389,8 +388,8 @@ public static function isAbsolute(string $path): bool
389
388
}
390
389
391
390
// Strip scheme
392
- if (false !== ($ pos = \ strpos ($ path , ':// ' ))) {
393
- $ path = \ substr ($ path , $ pos + 3 );
391
+ if (false !== ($ pos = strpos ($ path , ':// ' ))) {
392
+ $ path = substr ($ path , $ pos + 3 );
394
393
}
395
394
396
395
// UNIX root "/" or "\" (Windows style)
@@ -399,7 +398,7 @@ public static function isAbsolute(string $path): bool
399
398
}
400
399
401
400
// Windows root
402
- if (\strlen ($ path ) > 1 && \ ctype_alpha ($ path [0 ]) && ': ' === $ path [1 ]) {
401
+ if (\strlen ($ path ) > 1 && ctype_alpha ($ path [0 ]) && ': ' === $ path [1 ]) {
403
402
// Special case: "C:"
404
403
if (2 === \strlen ($ path )) {
405
404
return true ;
@@ -453,31 +452,31 @@ public static function isRelative(string $path): bool
453
452
* @param string $basePath an absolute base path
454
453
*
455
454
* @throws InvalidArgumentException if the base path is not absolute or if
456
- * the given path is an absolute path with
457
- * a different root than the base path
455
+ * the given path is an absolute path with
456
+ * a different root than the base path
458
457
*/
459
458
public static function makeAbsolute (string $ path , string $ basePath ): string
460
459
{
461
460
if ('' === $ basePath ) {
462
- throw new InvalidArgumentException (\ sprintf ('The base path must be a non-empty string. Got: "%s" ' , $ basePath ));
461
+ throw new InvalidArgumentException (sprintf ('The base path must be a non-empty string. Got: "%s" ' , $ basePath ));
463
462
}
464
463
465
464
if (!self ::isAbsolute ($ basePath )) {
466
- throw new InvalidArgumentException (\ sprintf ('The base path "%s" is not an absolute path. ' , $ basePath ));
465
+ throw new InvalidArgumentException (sprintf ('The base path "%s" is not an absolute path. ' , $ basePath ));
467
466
}
468
467
469
468
if (self ::isAbsolute ($ path )) {
470
469
return self ::canonicalize ($ path );
471
470
}
472
471
473
- if (false !== ($ pos = \ strpos ($ basePath , ':// ' ))) {
474
- $ scheme = \ substr ($ basePath , 0 , $ pos + 3 );
475
- $ basePath = \ substr ($ basePath , $ pos + 3 );
472
+ if (false !== ($ pos = strpos ($ basePath , ':// ' ))) {
473
+ $ scheme = substr ($ basePath , 0 , $ pos + 3 );
474
+ $ basePath = substr ($ basePath , $ pos + 3 );
476
475
} else {
477
476
$ scheme = '' ;
478
477
}
479
478
480
- return $ scheme .self ::canonicalize (\ rtrim ($ basePath , '/ \\' ).'/ ' .$ path );
479
+ return $ scheme .self ::canonicalize (rtrim ($ basePath , '/ \\' ).'/ ' .$ path );
481
480
}
482
481
483
482
/**
@@ -527,8 +526,8 @@ public static function makeAbsolute(string $path, string $basePath): string
527
526
* The result is a canonical path.
528
527
*
529
528
* @throws InvalidArgumentException if the base path is not absolute or if
530
- * the given path has a different root
531
- * than the base path
529
+ * the given path has a different root
530
+ * than the base path
532
531
*/
533
532
public static function makeRelative (string $ path , string $ basePath ): string
534
533
{
@@ -544,7 +543,7 @@ public static function makeRelative(string $path, string $basePath): string
544
543
if ('' === $ root && '' !== $ baseRoot ) {
545
544
// If base path is already in its root
546
545
if ('' === $ relativeBasePath ) {
547
- $ relativePath = \ ltrim ($ relativePath , './ \\' );
546
+ $ relativePath = ltrim ($ relativePath , './ \\' );
548
547
}
549
548
550
549
return $ relativePath ;
@@ -553,21 +552,21 @@ public static function makeRelative(string $path, string $basePath): string
553
552
// If the passed path is absolute, but the base path is not, we
554
553
// cannot generate a relative path
555
554
if ('' !== $ root && '' === $ baseRoot ) {
556
- throw new InvalidArgumentException (\ sprintf ('The absolute path "%s" cannot be made relative to the relative path "%s". You should provide an absolute base path instead. ' , $ path , $ basePath ));
555
+ throw new InvalidArgumentException (sprintf ('The absolute path "%s" cannot be made relative to the relative path "%s". You should provide an absolute base path instead. ' , $ path , $ basePath ));
557
556
}
558
557
559
558
// Fail if the roots of the two paths are different
560
559
if ($ baseRoot && $ root !== $ baseRoot ) {
561
- throw new InvalidArgumentException (\ sprintf ('The path "%s" cannot be made relative to "%s", because they have different roots ("%s" and "%s"). ' , $ path , $ basePath , $ root , $ baseRoot ));
560
+ throw new InvalidArgumentException (sprintf ('The path "%s" cannot be made relative to "%s", because they have different roots ("%s" and "%s"). ' , $ path , $ basePath , $ root , $ baseRoot ));
562
561
}
563
562
564
563
if ('' === $ relativeBasePath ) {
565
564
return $ relativePath ;
566
565
}
567
566
568
567
// Build a "../../" prefix with as many "../" parts as necessary
569
- $ parts = \ explode ('/ ' , $ relativePath );
570
- $ baseParts = \ explode ('/ ' , $ relativeBasePath );
568
+ $ parts = explode ('/ ' , $ relativePath );
569
+ $ baseParts = explode ('/ ' , $ relativeBasePath );
571
570
$ dotDotPrefix = '' ;
572
571
573
572
// Once we found a non-matching part in the prefix, we need to add
@@ -585,15 +584,15 @@ public static function makeRelative(string $path, string $basePath): string
585
584
$ dotDotPrefix .= '../ ' ;
586
585
}
587
586
588
- return \ rtrim ($ dotDotPrefix .\ implode ('/ ' , $ parts ), '/ ' );
587
+ return rtrim ($ dotDotPrefix .implode ('/ ' , $ parts ), '/ ' );
589
588
}
590
589
591
590
/**
592
591
* Returns whether the given path is on the local filesystem.
593
592
*/
594
593
public static function isLocal (string $ path ): bool
595
594
{
596
- return '' !== $ path && false === \ strpos ($ path , ':// ' );
595
+ return '' !== $ path && false === strpos ($ path , ':// ' );
597
596
}
598
597
599
598
/**
@@ -657,7 +656,7 @@ public static function getLongestCommonBasePath(string ...$paths): ?string
657
656
658
657
// Prevent false positives for common prefixes
659
658
// see isBasePath()
660
- if (0 === \ strpos ($ path .'/ ' , $ basePath .'/ ' )) {
659
+ if (0 === strpos ($ path .'/ ' , $ basePath .'/ ' )) {
661
660
// Next path
662
661
continue 2 ;
663
662
}
@@ -687,17 +686,17 @@ public static function join(string ...$paths): string
687
686
if (null === $ finalPath ) {
688
687
// For first part we keep slashes, like '/top', 'C:\' or 'phar://'
689
688
$ finalPath = $ path ;
690
- $ wasScheme = (false !== \ strpos ($ path , ':// ' ));
689
+ $ wasScheme = (false !== strpos ($ path , ':// ' ));
691
690
continue ;
692
691
}
693
692
694
693
// Only add slash if previous part didn't end with '/' or '\'
695
- if (!\in_array (\ substr ($ finalPath , -1 ), ['/ ' , '\\' ])) {
694
+ if (!\in_array (substr ($ finalPath , -1 ), ['/ ' , '\\' ])) {
696
695
$ finalPath .= '/ ' ;
697
696
}
698
697
699
698
// If first part included a scheme like 'phar://' we allow current part to start with '/', otherwise trim
700
- $ finalPath .= $ wasScheme ? $ path : \ ltrim ($ path , '/ ' );
699
+ $ finalPath .= $ wasScheme ? $ path : ltrim ($ path , '/ ' );
701
700
$ wasScheme = false ;
702
701
}
703
702
@@ -738,7 +737,7 @@ public static function isBasePath(string $basePath, string $ofPath): bool
738
737
// Don't append a slash for the root "/", because then that root
739
738
// won't be discovered as common prefix ("//" is not a prefix of
740
739
// "/foobar/").
741
- return 0 === \ strpos ($ ofPath .'/ ' , \ rtrim ($ basePath , '/ ' ).'/ ' );
740
+ return 0 === strpos ($ ofPath .'/ ' , rtrim ($ basePath , '/ ' ).'/ ' );
742
741
}
743
742
744
743
/**
@@ -766,28 +765,28 @@ private static function split(string $path): array
766
765
}
767
766
768
767
// Remember scheme as part of the root, if any
769
- if (false !== ($ pos = \ strpos ($ path , ':// ' ))) {
770
- $ root = \ substr ($ path , 0 , $ pos + 3 );
771
- $ path = \ substr ($ path , $ pos + 3 );
768
+ if (false !== ($ pos = strpos ($ path , ':// ' ))) {
769
+ $ root = substr ($ path , 0 , $ pos + 3 );
770
+ $ path = substr ($ path , $ pos + 3 );
772
771
} else {
773
772
$ root = '' ;
774
773
}
775
774
776
775
$ length = \strlen ($ path );
777
776
778
777
// Remove and remember root directory
779
- if (0 === \ strpos ($ path , '/ ' )) {
778
+ if (0 === strpos ($ path , '/ ' )) {
780
779
$ root .= '/ ' ;
781
- $ path = $ length > 1 ? \ substr ($ path , 1 ) : '' ;
782
- } elseif ($ length > 1 && \ ctype_alpha ($ path [0 ]) && ': ' === $ path [1 ]) {
780
+ $ path = $ length > 1 ? substr ($ path , 1 ) : '' ;
781
+ } elseif ($ length > 1 && ctype_alpha ($ path [0 ]) && ': ' === $ path [1 ]) {
783
782
if (2 === $ length ) {
784
783
// Windows special case: "C:"
785
784
$ root .= $ path .'/ ' ;
786
785
$ path = '' ;
787
786
} elseif ('/ ' === $ path [2 ]) {
788
787
// Windows normal case: "C:/"..
789
- $ root .= \ substr ($ path , 0 , 3 );
790
- $ path = $ length > 3 ? \ substr ($ path , 3 ) : '' ;
788
+ $ root .= substr ($ path , 0 , 3 );
789
+ $ path = $ length > 3 ? substr ($ path , 3 ) : '' ;
791
790
}
792
791
}
793
792
@@ -796,7 +795,7 @@ private static function split(string $path): array
796
795
797
796
private static function toLower (string $ str ): string
798
797
{
799
- return \ mb_strtolower ($ str , mb_detect_encoding ($ str ));
798
+ return mb_strtolower ($ str , mb_detect_encoding ($ str ));
800
799
}
801
800
802
801
private function __construct ()
0 commit comments