25
25
*/
26
26
class FileManager
27
27
{
28
- private $ fs ;
29
- private $ autoloaderUtil ;
30
- private $ makerFileLinkFormatter ;
31
- private $ rootDirectory ;
32
- /** @var SymfonyStyle */
33
- private $ io ;
34
- private $ twigDefaultPath ;
28
+ private ?SymfonyStyle $ io = null ;
35
29
36
30
public function __construct (
37
- Filesystem $ fs ,
38
- AutoloaderUtil $ autoloaderUtil ,
39
- MakerFileLinkFormatter $ makerFileLinkFormatter ,
40
- string $ rootDirectory ,
41
- string $ twigDefaultPath = null ,
31
+ private Filesystem $ fs ,
32
+ private AutoloaderUtil $ autoloaderUtil ,
33
+ private MakerFileLinkFormatter $ makerFileLinkFormatter ,
34
+ private string $ rootDirectory ,
35
+ private ? string $ twigDefaultPath = null ,
42
36
) {
43
- // move FileManagerTest stuff
44
- // update EntityRegeneratorTest to mock the autoloader
45
- $ this ->fs = $ fs ;
46
- $ this ->autoloaderUtil = $ autoloaderUtil ;
47
- $ this ->makerFileLinkFormatter = $ makerFileLinkFormatter ;
48
37
$ this ->rootDirectory = rtrim ($ this ->realPath ($ this ->normalizeSlashes ($ rootDirectory )), '/ ' );
49
38
$ this ->twigDefaultPath = $ twigDefaultPath ? rtrim ($ this ->relativizePath ($ twigDefaultPath ), '/ ' ) : null ;
50
39
}
@@ -77,13 +66,11 @@ public function dumpFile(string $filename, string $content): void
77
66
$ this ->fs ->dumpFile ($ absolutePath , $ content );
78
67
$ relativePath = $ this ->relativizePath ($ filename );
79
68
80
- if ($ this ->io ) {
81
- $ this ->io ->comment (sprintf (
82
- '%s: %s ' ,
83
- $ comment ,
84
- $ this ->makerFileLinkFormatter ->makeLinkedPath ($ absolutePath , $ relativePath )
85
- ));
86
- }
69
+ $ this ->io ?->comment(sprintf (
70
+ '%s: %s ' ,
71
+ $ comment ,
72
+ $ this ->makerFileLinkFormatter ->makeLinkedPath ($ absolutePath , $ relativePath )
73
+ ));
87
74
}
88
75
89
76
public function fileExists ($ path ): bool
@@ -94,24 +81,22 @@ public function fileExists($path): bool
94
81
/**
95
82
* Attempts to make the path relative to the root directory.
96
83
*
97
- * @param string $absolutePath
98
- *
99
84
* @throws \Exception
100
85
*/
101
- public function relativizePath ($ absolutePath ): string
86
+ public function relativizePath (string $ absolutePath ): string
102
87
{
103
88
$ absolutePath = $ this ->normalizeSlashes ($ absolutePath );
104
89
105
90
// see if the path is even in the root
106
- if (false === strpos ($ absolutePath , $ this ->rootDirectory )) {
91
+ if (! str_contains ($ absolutePath , $ this ->rootDirectory )) {
107
92
return $ absolutePath ;
108
93
}
109
94
110
95
$ absolutePath = $ this ->realPath ($ absolutePath );
111
96
112
97
// str_replace but only the first occurrence
113
98
$ relativePath = ltrim (implode ('' , explode ($ this ->rootDirectory , $ absolutePath , 2 )), '/ ' );
114
- if (0 === strpos ($ relativePath , './ ' )) {
99
+ if (str_starts_with ($ relativePath , './ ' )) {
115
100
$ relativePath = substr ($ relativePath , 2 );
116
101
}
117
102
@@ -127,22 +112,17 @@ public function getFileContents(string $path): string
127
112
return file_get_contents ($ this ->absolutizePath ($ path ));
128
113
}
129
114
130
- public function createFinder (string $ in ): Finder
131
- {
132
- $ finder = new Finder ();
133
- $ finder ->in ($ this ->absolutizePath ($ in ));
134
-
135
- return $ finder ;
136
- }
137
-
138
115
public function isPathInVendor (string $ path ): bool
139
116
{
140
- return 0 === strpos ($ this ->normalizeSlashes ($ path ), $ this ->normalizeSlashes ($ this ->rootDirectory .'/vendor/ ' ));
117
+ return str_starts_with (
118
+ $ this ->normalizeSlashes ($ path ),
119
+ $ this ->normalizeSlashes ($ this ->rootDirectory .'/vendor/ ' )
120
+ );
141
121
}
142
122
143
123
public function absolutizePath ($ path ): string
144
124
{
145
- if (0 === strpos ($ path , '/ ' )) {
125
+ if (str_starts_with ($ path , '/ ' )) {
146
126
return $ path ;
147
127
}
148
128
@@ -190,10 +170,8 @@ public function getPathForTemplate(string $filename): string
190
170
191
171
/**
192
172
* Resolve '../' in paths (like real_path), but for non-existent files.
193
- *
194
- * @param string $absolutePath
195
173
*/
196
- private function realPath ($ absolutePath ): string
174
+ private function realPath (string $ absolutePath ): string
197
175
{
198
176
$ finalParts = [];
199
177
$ currentIndex = -1 ;
@@ -219,12 +197,10 @@ private function realPath($absolutePath): string
219
197
$ finalPath = implode ('/ ' , $ finalParts );
220
198
// Normalize: // => /
221
199
// Normalize: /./ => /
222
- $ finalPath = str_replace (['// ' , '/./ ' ], '/ ' , $ finalPath );
223
-
224
- return $ finalPath ;
200
+ return str_replace (['// ' , '/./ ' ], '/ ' , $ finalPath );
225
201
}
226
202
227
- private function normalizeSlashes (string $ path )
203
+ private function normalizeSlashes (string $ path ): string
228
204
{
229
205
return str_replace ('\\' , '/ ' , $ path );
230
206
}
0 commit comments