Skip to content
This repository was archived by the owner on Jan 23, 2019. It is now read-only.

Commit c710b8d

Browse files
committed
up
1 parent 8a1638b commit c710b8d

File tree

3 files changed

+247
-19
lines changed

3 files changed

+247
-19
lines changed

src/Files/FileFinder.php

Lines changed: 228 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class FileFinder extends StdObject
5151
* @var array
5252
*/
5353
protected $include = [
54-
// 'file' => ['README.md'],
54+
// 'file' => ['README.md'], // file name
5555
// 'ext' => [
5656
// 'js','css',
5757
// 'ttf','svg', 'eot', 'woff', 'woff2',
@@ -135,7 +135,7 @@ public function reset()
135135
return $this;
136136
}
137137

138-
public function find($recursive = false, $path = '', $pathPrefix = '')
138+
public function find($recursive = true, $path = '', $pathPrefix = '')
139139
{
140140
return $this->findAll($recursive, $path, $pathPrefix);
141141
}
@@ -147,7 +147,7 @@ public function find($recursive = false, $path = '', $pathPrefix = '')
147147
* @return $this
148148
* @throws InvalidArgumentException
149149
*/
150-
public function findAll($recursive = false, $path = '', $pathPrefix = '')
150+
public function findAll($recursive = true, $path = '', $pathPrefix = '')
151151
{
152152
$path = $path ?: $this->sourcePath;
153153
$pathPrefix = $pathPrefix ?: $this->pathPrefix;
@@ -314,6 +314,220 @@ protected function doFilterDir($name /*, $dir*/)
314314
return true;
315315
}
316316

317+
////////////////////////////// helper method //////////////////////////////
318+
319+
/**
320+
* @param mixed $data
321+
* @param string $type
322+
* @return $this
323+
*/
324+
public function include($data, $type = 'dir')
325+
{
326+
switch ($type) {
327+
case 'dir':
328+
$this->includeDir($data);
329+
break;
330+
case 'file':
331+
$this->includeFile($data);
332+
break;
333+
case 'ext':
334+
$this->includeExt($data);
335+
break;
336+
default:
337+
break;
338+
}
339+
340+
return $this;
341+
}
342+
343+
/**
344+
* @param string|array $file
345+
* @return self
346+
*/
347+
public function includeFile($file)
348+
{
349+
if (is_string($file)) {
350+
$file = [$file];
351+
}
352+
353+
if (is_array($file)) {
354+
foreach ($file as $name) {
355+
if (in_array($name, $this->include['file'], true)) {
356+
continue;
357+
}
358+
359+
$this->include['file'][] = trim($name);
360+
}
361+
}
362+
363+
return $this;
364+
}
365+
366+
/**
367+
* @param string|array $ext
368+
* @return self
369+
*/
370+
public function includeExt($ext)
371+
{
372+
if (is_string($ext)) {
373+
$ext = [$ext];
374+
}
375+
376+
if (is_array($ext)) {
377+
foreach ($ext as $name) {
378+
if (in_array($name, $this->include['ext'], true)) {
379+
continue;
380+
}
381+
382+
$this->include['ext'][] = trim($name, '.');
383+
}
384+
}
385+
386+
return $this;
387+
}
388+
389+
/**
390+
* @param string|array $dir
391+
* @return self
392+
*/
393+
public function includeDir($dir)
394+
{
395+
if (is_string($dir)) {
396+
$dir = [$dir];
397+
}
398+
399+
if (is_array($dir)) {
400+
foreach ($dir as $name) {
401+
if (in_array($name, $this->include['dir'], true)) {
402+
continue;
403+
}
404+
405+
$this->include['dir'][] = trim($name);
406+
}
407+
}
408+
409+
return $this;
410+
}
411+
412+
/**
413+
* @return self
414+
*/
415+
public function ignoreVCS()
416+
{
417+
return $this->excludeDir(['.git', '.svn'])->excludeFile('.gitignore');
418+
}
419+
420+
/**
421+
* @param mixed $data
422+
* @param string $type
423+
* @return $this
424+
*/
425+
public function exclude($data, $type = 'dir')
426+
{
427+
switch ($type) {
428+
case 'dir':
429+
$this->excludeDir($data);
430+
break;
431+
case 'file':
432+
$this->excludeFile($data);
433+
break;
434+
case 'ext':
435+
$this->excludeExt($data);
436+
break;
437+
default:
438+
break;
439+
}
440+
441+
return $this;
442+
}
443+
444+
/**
445+
* @param string|array $file
446+
* @return self
447+
*/
448+
public function notName($file)
449+
{
450+
return $this->excludeFile($file);
451+
}
452+
453+
/**
454+
* @param string|array $file
455+
* @return self
456+
*/
457+
public function notFile($file)
458+
{
459+
return $this->excludeFile($file);
460+
}
461+
462+
/**
463+
* @param string|array $file
464+
* @return self
465+
*/
466+
public function excludeFile($file)
467+
{
468+
if (is_string($file)) {
469+
$file = [$file];
470+
}
471+
472+
if (is_array($file)) {
473+
foreach ($file as $name) {
474+
if (in_array($name, $this->exclude['file'], true)) {
475+
continue;
476+
}
477+
478+
$this->exclude['file'][] = trim($name);
479+
}
480+
}
481+
482+
return $this;
483+
}
484+
485+
/**
486+
* @param string|array $ext
487+
* @return self
488+
*/
489+
public function excludeExt($ext)
490+
{
491+
if (is_string($ext)) {
492+
$ext = [$ext];
493+
}
494+
495+
if (is_array($ext)) {
496+
foreach ($ext as $name) {
497+
if (in_array($name, $this->exclude['ext'], true)) {
498+
continue;
499+
}
500+
501+
$this->exclude['ext'][] = trim($name);
502+
}
503+
}
504+
505+
return $this;
506+
}
507+
508+
/**
509+
* @param string|array $dir
510+
* @return self
511+
*/
512+
public function excludeDir($dir)
513+
{
514+
if (is_string($dir)) {
515+
$dir = [$dir];
516+
}
517+
518+
if (is_array($dir)) {
519+
foreach ($dir as $name) {
520+
if (in_array($name, $this->exclude['dir'], true)) {
521+
continue;
522+
}
523+
524+
$this->exclude['dir'][] = trim($name);
525+
}
526+
}
527+
528+
return $this;
529+
}
530+
317531
////////////////////////////// getter/setter method //////////////////////////////
318532

319533
/**
@@ -324,6 +538,16 @@ public function getSourcePath()
324538
return $this->sourcePath;
325539
}
326540

541+
/**
542+
* @param $sourcePath
543+
* @return self
544+
* @throws InvalidArgumentException
545+
*/
546+
public function inDir($sourcePath)
547+
{
548+
return $this->setSourcePath($sourcePath);
549+
}
550+
327551
/**
328552
* @param string $sourcePath
329553
* @return $this
@@ -336,7 +560,7 @@ public function setSourcePath($sourcePath)
336560
throw new InvalidArgumentException('The source path must be an existing dir path. Input: ' . $sourcePath);
337561
}
338562

339-
$this->sourcePath = $sourcePath;
563+
$this->sourcePath = realpath($sourcePath);
340564
}
341565

342566
return $this;

src/Files/PharCompiler.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class PharCompiler
1919
private $pharName;
2020
private $pharFile;
2121

22+
public $basePath;
23+
2224
/**
2325
* Compiles psysh into a single phar file.
2426
* @param string $pharFile The full path to the file to create
@@ -40,25 +42,24 @@ public function compile($pharFile = 'your.phar', $version = '0.0.1')
4042
$phar->startBuffering();
4143

4244
$finder = FileFinder::make()
43-
->files()
44-
->ignoreVCS(true)
45-
->name('*.php')
46-
->notName('Compiler.php')
47-
->notName('Autoloader.php')
48-
->in(__DIR__ . '/..');
49-
50-
foreach ($finder as $file) {
45+
->ignoreVCS()
46+
->includeExt('php')
47+
->notName(['Compiler.php', 'Autoloader.php'])
48+
->inDir(__DIR__ . '/..')
49+
->findAll();
50+
51+
foreach ($finder->getFiles() as $file) {
5152
$this->addFile($phar, $file);
5253
}
5354

5455
$finder = FileFinder::make()
55-
->files()
56-
->ignoreVCS(true)
57-
->name('*.php')
56+
->ignoreVCS()
57+
->includeExt('php')
5858
->exclude('Tests')
59-
->in(__DIR__ . '/../../build-vendor');
59+
->inDir(__DIR__ . '/../../build-vendor')
60+
->findAll();
6061

61-
foreach ($finder as $file) {
62+
foreach ($finder->getFiles() as $file) {
6263
$this->addFile($phar, $file);
6364
}
6465

src/Helpers/Cli.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,9 @@ public static function parseOptArgs(array $noValues = [], $mergeOpts = false)
161161
$fullScript = implode(' ', $params);
162162
$script = array_shift($params);
163163

164-
while (list(, $p) = each($params)) {
164+
// each() will deprecated at 7.2 so,there use current and next instead it.
165+
// while (list(,$p) = each($params)) {
166+
while (false !== ($p = current($params))) {
165167
// is options
166168
if ($p{0} === '-') {
167169
$isLong = false;
@@ -187,7 +189,8 @@ public static function parseOptArgs(array $noValues = [], $mergeOpts = false)
187189
$nxp = current($params);
188190

189191
if ($value === true && $nxp !== false && $nxp{0} !== '-' && !in_array($opt, $noValues, true)) {
190-
list(, $value) = each($params);
192+
// list(,$value) = each($params);
193+
$value = current($params); next($params);
191194

192195
// short-opt: bool opts. like -e -abc
193196
} elseif (!$isLong && $value === true) {

0 commit comments

Comments
 (0)