Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch exceptions for inaccessible and non-existent dirs #22

Merged
merged 1 commit into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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