Skip to content

Commit

Permalink
Catch exceptions for inaccessible and non-existent dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
untone-survive committed Sep 28, 2023
1 parent cffc5c3 commit 842881c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
29 changes: 17 additions & 12 deletions src/WappMatchers/PhpMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use League\Flysystem\Filesystem;
use League\Flysystem\FilesystemException;
use League\Flysystem\StorageAttributes;
use League\Flysystem\UnableToListContents;
use Plesk\Wappspector\Matchers;

class PhpMatcher implements WappMatcherInterface
Expand All @@ -14,20 +15,24 @@ class PhpMatcher implements WappMatcherInterface
*/
public function match(Filesystem $fs, string $path): iterable
{
$list = $fs->listContents($path);
foreach ($list as $item) {
/** @var StorageAttributes $item */
if ($item->isFile() && str_ends_with($item->path(), '.php')) {
return [
'matcher' => Matchers::PHP,
'path' => $path,
'version' => null,
];
}
try {
$list = $fs->listContents($path);
foreach ($list as $item) {
/** @var StorageAttributes $item */
if ($item->isFile() && str_ends_with($item->path(), '.php')) {
return [
'matcher' => Matchers::PHP,
'path' => $path,
'version' => null,
];
}

if ($item->isDir() && $item->path() === ltrim(rtrim($path, '/') . '/src', '/')) {
return $this->match($fs, rtrim($path, '/') . '/src');
if ($item->isDir() && $item->path() === ltrim(rtrim($path, '/') . '/src', '/')) {
return $this->match($fs, rtrim($path, '/') . '/src');
}
}
} catch (UnableToListContents) {
// skip dir if it is inaccessible
}

return [];
Expand Down
20 changes: 10 additions & 10 deletions src/WappMatchers/UpLevelMatcherTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@
namespace Plesk\Wappspector\WappMatchers;

use League\Flysystem\Filesystem;
use League\Flysystem\FilesystemException;
use League\Flysystem\UnableToListContents;

trait UpLevelMatcherTrait
{
abstract protected function doMatch(Filesystem $fs, string $path): array;

/**
* @throws FilesystemException
*/
public function match(Filesystem $fs, string $path): array
{
if (!$result = $this->doMatch($fs, $path)) {
try {
$result = $this->doMatch($fs, rtrim($path) . '/../');
} catch (UnableToListContents $e) {
// skip parent dir if it is inaccessible
}
return $this->safeScanDir($fs, $path) ?: $this->safeScanDir($fs, rtrim($path) . '/../');
}

private function safeScanDir(Filesystem $fs, string $path): array
{
$result = [];
try {
$result = $this->doMatch($fs, $path);
} catch (UnableToListContents) {
// skip dir if it is inaccessible
}

return $result;
Expand Down

0 comments on commit 842881c

Please sign in to comment.