Skip to content

Commit c4adec0

Browse files
committed
Fixes and improvements
1 parent 01992df commit c4adec0

File tree

1 file changed

+49
-26
lines changed

1 file changed

+49
-26
lines changed

lib/Compiler.php

+49-26
Original file line numberDiff line numberDiff line change
@@ -57,44 +57,44 @@ public function __construct($path)
5757
/**
5858
* Compiles all files into a single PHAR file.
5959
*
60-
* @param string $outputfile The full name of the file to create
60+
* @param string $outputFile The full name of the file to create
6161
* @throws \LogicException if no index files are defined.
6262
*/
63-
public function compile($outputfile)
63+
public function compile($outputFile)
6464
{
6565
if (empty($this->index)) {
6666
throw new \LogicException('Cannot compile when no index files are defined.');
6767
}
6868

69-
if (file_exists($outputfile)) {
70-
unlink($outputfile);
69+
if (file_exists($outputFile)) {
70+
unlink($outputFile);
7171
}
7272

73-
$name = basename($outputfile);
74-
$phar = new \Phar($outputfile, 0, $name);
73+
$name = basename($outputFile);
74+
$phar = new \Phar($outputFile, 0, $name);
7575
$phar->setSignatureAlgorithm(\Phar::SHA1);
7676
$phar->startBuffering();
7777

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);
8181

8282
if ($strip) {
8383
$content = $this->stripWhitespace($content);
8484
}
8585

86-
$phar->addFromString($virtualfile, $content);
86+
$phar->addFromString($virtualFile, $content);
8787
}
8888

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);
9292

9393
if ($type == 'cli') {
9494
$content = preg_replace('{^#!/usr/bin/env php\s*}', '', $content);
9595
}
9696

97-
$phar->addFromString($virtualfile, $content);
97+
$phar->addFromString($virtualFile, $content);
9898
}
9999

100100
$stub = $this->generateStub($name);
@@ -132,8 +132,8 @@ public function getFiles()
132132
*/
133133
public function addFile($file, $strip = true)
134134
{
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];
137137
}
138138

139139
/**
@@ -145,25 +145,28 @@ public function addFile($file, $strip = true)
145145
*/
146146
public function addDirectory($directory, $exclude = null, $strip = true)
147147
{
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+
);
150153

151154
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) {
153157
if ($current->isDir()) {
154158
return true;
155159
}
156160

157-
$subpath = substr($current->getPathName(), strlen($realpath) + 1);
158-
159-
return $this->filter($subpath, (array) $exclude);
161+
return $this->filter($current->getSubPathname(), $exclude);
160162
});
161163
}
162164

163165
$iterator = new \RecursiveIteratorIterator($iterator);
164166
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);
167170
}
168171
}
169172

@@ -223,7 +226,7 @@ public function supportsSapi($sapi)
223226
*/
224227
protected function generateStub($name)
225228
{
226-
$stub = array('#!/usr/bin/env php', '<?php');
229+
$stub = ['#!/usr/bin/env php', '<?php'];
227230
$stub[] = "Phar::mapPhar('$name');";
228231
$stub[] = "if (PHP_SAPI == 'cli') {";
229232

@@ -249,16 +252,36 @@ protected function generateStub($name)
249252
return join("\n", $stub);
250253
}
251254

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+
252274
/**
253275
* Filters the given path.
254276
*
277+
* @param string $path
255278
* @param array $patterns
256279
* @return bool
257280
*/
258281
protected function filter($path, array $patterns)
259282
{
260283
foreach ($patterns as $pattern) {
261-
if ($pattern[0] == '!' ? !fnmatch(substr($pattern, 1), $path) : fnmatch($pattern, $path)) {
284+
if ($this->match($path, $pattern)) {
262285
return false;
263286
}
264287
}

0 commit comments

Comments
 (0)