@@ -57,44 +57,44 @@ public function __construct($path)
57
57
/**
58
58
* Compiles all files into a single PHAR file.
59
59
*
60
- * @param string $outputfile The full name of the file to create
60
+ * @param string $outputFile The full name of the file to create
61
61
* @throws \LogicException if no index files are defined.
62
62
*/
63
- public function compile ($ outputfile )
63
+ public function compile ($ outputFile )
64
64
{
65
65
if (empty ($ this ->index )) {
66
66
throw new \LogicException ('Cannot compile when no index files are defined. ' );
67
67
}
68
68
69
- if (file_exists ($ outputfile )) {
70
- unlink ($ outputfile );
69
+ if (file_exists ($ outputFile )) {
70
+ unlink ($ outputFile );
71
71
}
72
72
73
- $ name = basename ($ outputfile );
74
- $ phar = new \Phar ($ outputfile , 0 , $ name );
73
+ $ name = basename ($ outputFile );
74
+ $ phar = new \Phar ($ outputFile , 0 , $ name );
75
75
$ phar ->setSignatureAlgorithm (\Phar::SHA1 );
76
76
$ phar ->startBuffering ();
77
77
78
- foreach ($ this ->files as $ virtualfile => $ fileinfo ) {
79
- list ($ realfile , $ strip ) = $ fileinfo ;
80
- $ content = file_get_contents ($ realfile );
78
+ foreach ($ this ->files as $ virtualFile => $ fileInfo ) {
79
+ list ($ realFile , $ strip ) = $ fileInfo ;
80
+ $ content = file_get_contents ($ realFile );
81
81
82
82
if ($ strip ) {
83
83
$ content = $ this ->stripWhitespace ($ content );
84
84
}
85
85
86
- $ phar ->addFromString ($ virtualfile , $ content );
86
+ $ phar ->addFromString ($ virtualFile , $ content );
87
87
}
88
88
89
- foreach ($ this ->index as $ type => $ fileinfo ) {
90
- list ($ virtualfile , $ realfile ) = $ fileinfo ;
91
- $ content = file_get_contents ($ realfile );
89
+ foreach ($ this ->index as $ type => $ fileInfo ) {
90
+ list ($ virtualFile , $ realFile ) = $ fileInfo ;
91
+ $ content = file_get_contents ($ realFile );
92
92
93
93
if ($ type == 'cli ' ) {
94
94
$ content = preg_replace ('{^#!/usr/bin/env php\s*} ' , '' , $ content );
95
95
}
96
96
97
- $ phar ->addFromString ($ virtualfile , $ content );
97
+ $ phar ->addFromString ($ virtualFile , $ content );
98
98
}
99
99
100
100
$ stub = $ this ->generateStub ($ name );
@@ -132,8 +132,8 @@ public function getFiles()
132
132
*/
133
133
public function addFile ($ file , $ strip = true )
134
134
{
135
- $ realfile = realpath ($ this ->path . DIRECTORY_SEPARATOR . $ file );
136
- $ this ->files [$ file ] = [$ realfile , (bool ) $ strip ];
135
+ $ realFile = realpath ($ this ->path . DIRECTORY_SEPARATOR . $ file );
136
+ $ this ->files [$ file ] = [$ realFile , (bool ) $ strip ];
137
137
}
138
138
139
139
/**
@@ -145,25 +145,28 @@ public function addFile($file, $strip = true)
145
145
*/
146
146
public function addDirectory ($ directory , $ exclude = null , $ strip = true )
147
147
{
148
- $ realpath = realpath ($ this ->path . DIRECTORY_SEPARATOR . $ directory );
149
- $ iterator = new \RecursiveDirectoryIterator ($ realpath , \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::UNIX_PATHS );
148
+ $ realPath = realpath ($ this ->path . DIRECTORY_SEPARATOR . $ directory );
149
+ $ iterator = new \RecursiveDirectoryIterator (
150
+ $ realPath ,
151
+ \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::UNIX_PATHS | \FilesystemIterator::CURRENT_AS_SELF
152
+ );
150
153
151
154
if ((is_string ($ exclude ) || is_array ($ exclude )) && !empty ($ exclude )) {
152
- $ iterator = new \RecursiveCallbackFilterIterator ($ iterator , function ($ current ) use ($ exclude , $ realpath ) {
155
+ $ exclude = (array ) $ exclude ;
156
+ $ iterator = new \RecursiveCallbackFilterIterator ($ iterator , function (\RecursiveDirectoryIterator $ current ) use ($ exclude ) {
153
157
if ($ current ->isDir ()) {
154
158
return true ;
155
159
}
156
160
157
- $ subpath = substr ($ current ->getPathName (), strlen ($ realpath ) + 1 );
158
-
159
- return $ this ->filter ($ subpath , (array ) $ exclude );
161
+ return $ this ->filter ($ current ->getSubPathname (), $ exclude );
160
162
});
161
163
}
162
164
163
165
$ iterator = new \RecursiveIteratorIterator ($ iterator );
164
166
foreach ($ iterator as $ file ) {
165
- $ virtualfile = substr ($ file ->getPathName (), strlen ($ this ->path ) + 1 );
166
- $ this ->addFile ($ virtualfile , $ strip );
167
+ /** @var \SplFileInfo $file */
168
+ $ virtualFile = substr ($ file ->getPathName (), strlen ($ this ->path ) + 1 );
169
+ $ this ->addFile ($ virtualFile , $ strip );
167
170
}
168
171
}
169
172
@@ -223,7 +226,7 @@ public function supportsSapi($sapi)
223
226
*/
224
227
protected function generateStub ($ name )
225
228
{
226
- $ stub = array ( '#!/usr/bin/env php ' , '<?php ' ) ;
229
+ $ stub = [ '#!/usr/bin/env php ' , '<?php ' ] ;
227
230
$ stub [] = "Phar::mapPhar(' $ name'); " ;
228
231
$ stub [] = "if (PHP_SAPI == 'cli') { " ;
229
232
@@ -249,16 +252,36 @@ protected function generateStub($name)
249
252
return join ("\n" , $ stub );
250
253
}
251
254
255
+ /**
256
+ * Matches the given path.
257
+ *
258
+ * @param string $path
259
+ * @param string $pattern
260
+ * @return bool
261
+ */
262
+ protected function match ($ path , $ pattern )
263
+ {
264
+ $ inverted = false ;
265
+
266
+ if ($ pattern [0 ] == '! ' ) {
267
+ $ pattern = substr ($ pattern , 1 );
268
+ $ inverted = true ;
269
+ }
270
+
271
+ return fnmatch ($ pattern , $ path ) == ($ inverted ? false : true );
272
+ }
273
+
252
274
/**
253
275
* Filters the given path.
254
276
*
277
+ * @param string $path
255
278
* @param array $patterns
256
279
* @return bool
257
280
*/
258
281
protected function filter ($ path , array $ patterns )
259
282
{
260
283
foreach ($ patterns as $ pattern ) {
261
- if ($ pattern [ 0 ] == ' ! ' ? ! fnmatch ( substr ( $ pattern , 1 ), $ path) : fnmatch ( $ pattern , $ path )) {
284
+ if ($ this -> match ( $ path , $ pattern )) {
262
285
return false ;
263
286
}
264
287
}
0 commit comments