Skip to content

Commit 208438f

Browse files
committed
feat: add possibility to check a single file
1 parent a760402 commit 208438f

File tree

1 file changed

+52
-49
lines changed

1 file changed

+52
-49
lines changed

src/Scanner.php

+52-49
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313
namespace marcocesarato\amwscan;
1414

1515
use ArrayIterator;
16+
use ArrayObject;
1617
use CallbackFilterIterator;
1718
use Exception;
1819
use LimitIterator;
1920
use Phar;
2021
use RecursiveDirectoryIterator;
2122
use RecursiveIteratorIterator;
23+
use SplFileInfo;
2224

2325
/**
2426
* Class Application.
@@ -597,11 +599,6 @@ private function arguments($args = null)
597599
self::setPathScan(realpath($path));
598600
}
599601
}
600-
601-
// Check path
602-
if (!is_dir(self::$pathScan)) {
603-
self::setPathScan(pathinfo(self::$pathScan, PATHINFO_DIRNAME));
604-
}
605602
}
606603

607604
/**
@@ -687,61 +684,67 @@ private function modes()
687684
public function mapping()
688685
{
689686
// Mapping files
690-
$directory = new RecursiveDirectoryIterator(self::$pathScan);
691-
$files = new RecursiveIteratorIterator($directory);
692-
$filtered = new CallbackFilterIterator($files, function ($cur) {
693-
$ignore = false;
694-
$wildcard = '.*?'; // '[^\\\\\\/]*'
695-
// Ignore
696-
foreach (self::$ignorePaths as $ignorePath) {
697-
$ignorePath = preg_quote($ignorePath, ';');
698-
$ignorePath = str_replace('\*', $wildcard, $ignorePath);
699-
if (preg_match(';' . $ignorePath . ';i', $cur->getPath())) {
700-
$ignore = true;
687+
if (is_dir(self::$pathScan)) {
688+
$directory = new RecursiveDirectoryIterator(self::$pathScan);
689+
$files = new RecursiveIteratorIterator($directory);
690+
$filtered = new CallbackFilterIterator($files, function ($cur) {
691+
$ignore = false;
692+
$wildcard = '.*?'; // '[^\\\\\\/]*'
693+
// Ignore
694+
foreach (self::$ignorePaths as $ignorePath) {
695+
$ignorePath = preg_quote($ignorePath, ';');
696+
$ignorePath = str_replace('\*', $wildcard, $ignorePath);
697+
if (preg_match(';' . $ignorePath . ';i', $cur->getPath())) {
698+
$ignore = true;
699+
}
701700
}
702-
}
703-
// Filter
704-
foreach (self::$filterPaths as $filterPath) {
705-
$filterPath = preg_quote($filterPath, ';');
706-
$filterPath = str_replace('\*', $wildcard, $filterPath);
707-
if (!preg_match(';' . $filterPath . ';i', $cur->getPath())) {
708-
$ignore = true;
701+
// Filter
702+
foreach (self::$filterPaths as $filterPath) {
703+
$filterPath = preg_quote($filterPath, ';');
704+
$filterPath = str_replace('\*', $wildcard, $filterPath);
705+
if (!preg_match(';' . $filterPath . ';i', $cur->getPath())) {
706+
$ignore = true;
707+
}
709708
}
710-
}
711709

712-
if (!$ignore &&
713-
$cur->isDir()) {
714-
Modules::init($cur->getPath());
710+
if (!$ignore &&
711+
$cur->isDir()) {
712+
Modules::init($cur->getPath());
715713

716-
return false;
717-
}
718-
719-
return
720-
!$ignore &&
721-
$cur->isFile() &&
722-
in_array($cur->getExtension(), self::getExtensions(), true);
723-
});
714+
return false;
715+
}
724716

725-
$mapping = array();
717+
return
718+
!$ignore &&
719+
$cur->isFile() &&
720+
in_array($cur->getExtension(), self::getExtensions(), true);
721+
});
722+
$mapping = array();
726723

727-
$mapped = 0;
728-
$count = iterator_count($filtered);
724+
$mapped = 0;
725+
$count = iterator_count($filtered);
729726

730-
Console::writeBreak(1);
731-
Console::writeLine('Verifying files checksum...', 2);
727+
Console::writeBreak(1);
728+
Console::writeLine('Verifying files checksum...', 2);
732729

733-
foreach ($filtered as $cur) {
734-
Console::progress($mapped++, $count);
735-
if ($cur->isFile() && !Modules::isVerified($cur->getPathname())) {
736-
$mapping[] = $cur;
730+
foreach ($filtered as $cur) {
731+
Console::progress($mapped++, $count);
732+
if ($cur->isFile() && !Modules::isVerified($cur->getPathname())) {
733+
$mapping[] = $cur;
734+
}
735+
Console::progress($mapped, $count);
737736
}
738-
Console::progress($mapped, $count);
737+
$iterator = new \ArrayObject($mapping);
738+
739+
Console::writeBreak(1);
740+
741+
return $iterator->getIterator();
739742
}
740-
$iterator = new \ArrayObject($mapping);
741743

742-
Console::writeBreak(1);
744+
$file = new SplFileInfo(self::$pathScan);
745+
$obj = new ArrayObject(array($file));
743746

744-
return $iterator->getIterator();
747+
return $obj->getIterator();
745748
}
746749

747750
/**
@@ -1349,7 +1352,7 @@ protected function interrupt()
13491352
{
13501353
$this->interrupt = true;
13511354
if (self::isCli()) {
1352-
die();
1355+
exit();
13531356
}
13541357
}
13551358

0 commit comments

Comments
 (0)