@@ -33,7 +33,7 @@ class FileManager
33
33
public function __construct (Filesystem $ fs , string $ rootDirectory )
34
34
{
35
35
$ this ->fs = $ fs ;
36
- $ this ->rootDirectory = rtrim ($ this ->realpath ($ rootDirectory ). '/ ' );
36
+ $ this ->rootDirectory = rtrim ($ this ->realpath ($ this -> normalizeSlashes ( $ rootDirectory )), '/ ' );
37
37
}
38
38
39
39
public function setIO (SymfonyStyle $ io )
@@ -61,8 +61,19 @@ public function fileExists($path): bool
61
61
return file_exists ($ this ->absolutizePath ($ path ));
62
62
}
63
63
64
+ /**
65
+ * Attempts to make the path relative to the root directory.
66
+ *
67
+ * @param string $absolutePath
68
+ *
69
+ * @return string
70
+ *
71
+ * @throws \Exception
72
+ */
64
73
public function relativizePath ($ absolutePath ): string
65
74
{
75
+ $ absolutePath = $ this ->normalizeSlashes ($ absolutePath );
76
+
66
77
// see if the path is even in the root
67
78
if (false === strpos ($ absolutePath , $ this ->rootDirectory )) {
68
79
return $ absolutePath ;
@@ -78,6 +89,15 @@ public function relativizePath($absolutePath): string
78
89
return is_dir ($ absolutePath ) ? rtrim ($ relativePath , '/ ' ).'/ ' : $ relativePath ;
79
90
}
80
91
92
+ /**
93
+ * Returns the relative path to where a new class should live.
94
+ *
95
+ * @param string $className
96
+ *
97
+ * @return null|string
98
+ *
99
+ * @throws \Exception
100
+ */
81
101
public function getPathForFutureClass (string $ className )
82
102
{
83
103
// lookup is obviously modeled off of Composer's autoload logic
@@ -138,12 +158,17 @@ private function getClassLoader(): ClassLoader
138
158
return self ::$ classLoader ;
139
159
}
140
160
141
- private function absolutizePath ($ path ): string
161
+ public function absolutizePath ($ path ): string
142
162
{
143
163
if (0 === strpos ($ path , '/ ' )) {
144
164
return $ path ;
145
165
}
146
166
167
+ // support windows drive paths: C:\
168
+ if (1 === strpos ($ path , ': \\' )) {
169
+ return $ path ;
170
+ }
171
+
147
172
return sprintf ('%s/%s ' , $ this ->rootDirectory , $ path );
148
173
}
149
174
@@ -159,6 +184,7 @@ private function realPath($absolutePath): string
159
184
$ finalParts = [];
160
185
$ currentIndex = -1 ;
161
186
187
+ $ absolutePath = $ this ->normalizeSlashes ($ absolutePath );
162
188
foreach (explode ('/ ' , $ absolutePath ) as $ pathPart ) {
163
189
if ('.. ' === $ pathPart ) {
164
190
// we need to remove the previous entry
@@ -184,4 +210,9 @@ private function realPath($absolutePath): string
184
210
185
211
return $ finalPath ;
186
212
}
213
+
214
+ private function normalizeSlashes (string $ path )
215
+ {
216
+ return str_replace ('\\' , '/ ' , $ path );
217
+ }
187
218
}
0 commit comments