-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add FileFinder for recursive and file-based finders (fixes #20)
- Loading branch information
Showing
19 changed files
with
884 additions
and
246 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kcs\ClassFinder\FileFinder; | ||
|
||
use Psr\Cache\CacheItemPoolInterface; | ||
use SplFileInfo; | ||
|
||
use function hash; | ||
|
||
final class CachedFileFinder implements FileFinderInterface | ||
{ | ||
public function __construct( | ||
private readonly FileFinderInterface $innerFinder, | ||
private readonly CacheItemPoolInterface $cacheItemPool, | ||
) { | ||
} | ||
|
||
/** @inheritDoc */ | ||
public function search(string $pattern): iterable | ||
{ | ||
$cacheKey = hash('sha256', $pattern); | ||
$item = $this->cacheItemPool->getItem($cacheKey); | ||
if ($item->isHit()) { | ||
$files = $item->get(); | ||
} else { | ||
$files = []; | ||
foreach ($this->innerFinder->search($pattern) as $path => $_) { // phpcs:ignore Squiz.NamingConventions.ValidVariableName.NotCamelCaps | ||
$files[] = $path; | ||
} | ||
|
||
$item->set($files); | ||
$this->cacheItemPool->save($item); | ||
} | ||
|
||
foreach ($files as $file) { | ||
yield $file => new SplFileInfo($file); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kcs\ClassFinder\FileFinder; | ||
|
||
use FilesystemIterator; | ||
use Kcs\ClassFinder\PathNormalizer; | ||
use RecursiveCallbackFilterIterator; | ||
use RecursiveDirectoryIterator; | ||
use RecursiveIteratorIterator; | ||
use SplFileInfo; | ||
|
||
use function is_dir; | ||
use function is_file; | ||
use function Safe\glob; | ||
|
||
final class DefaultFileFinder implements FileFinderInterface | ||
{ | ||
/** @inheritDoc */ | ||
public function search(string $pattern): iterable | ||
{ | ||
foreach (glob($pattern) as $path) { | ||
if (is_dir($path)) { | ||
$files = new RecursiveIteratorIterator( | ||
new RecursiveCallbackFilterIterator( | ||
new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS | FilesystemIterator::FOLLOW_SYMLINKS), | ||
static fn (SplFileInfo $file): bool => $file->getBasename()[0] !== '.', | ||
), | ||
RecursiveIteratorIterator::LEAVES_ONLY, | ||
); | ||
|
||
foreach ($files as $filepath => $info) { | ||
if (! $info->isFile()) { | ||
continue; | ||
} | ||
|
||
yield PathNormalizer::resolvePath($filepath) => $info; | ||
} | ||
} elseif (is_file($path)) { | ||
yield PathNormalizer::resolvePath($path) => new SplFileInfo($path); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kcs\ClassFinder\FileFinder; | ||
|
||
use SplFileInfo; | ||
|
||
/** | ||
* The FileFinderInterface is responsible to find files matching a given pattern. | ||
*/ | ||
interface FileFinderInterface | ||
{ | ||
/** | ||
* Searches for the given pattern and return the list of the matching files/folders. | ||
* | ||
* @return iterable<string, SplFileInfo> | ||
*/ | ||
public function search(string $pattern): iterable; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kcs\ClassFinder\Finder; | ||
|
||
use Kcs\ClassFinder\FileFinder\FileFinderInterface; | ||
|
||
trait RecursiveFinderTrait | ||
{ | ||
private FileFinderInterface $fileFinder; | ||
|
||
public function withFileFinder(FileFinderInterface $fileFinder): static | ||
{ | ||
$this->fileFinder = $fileFinder; | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.