-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c227348
commit b8e9276
Showing
22 changed files
with
765 additions
and
9 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
33 changes: 33 additions & 0 deletions
33
src/Retrieval/Loader/Directory/RecursiveDirectoryIterator.php
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,33 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Lugha package. | ||
* | ||
* (c) Bernard Ngandu <bernard@devscast.tech> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Devscast\Lugha\Retrieval\Loader\Directory; | ||
|
||
use RecursiveDirectoryIterator as NativeRecursiveDirectoryIterator; | ||
use RecursiveIteratorIterator; | ||
|
||
/** | ||
* Class RealRecursiveDirectoryIterator. | ||
* | ||
* @extends RecursiveIteratorIterator<NativeRecursiveDirectoryIterator> | ||
* @see https://www.php.net/manual/en/class.recursivedirectoryiterator.php | ||
* | ||
* @author bernard-ng <bernard@devscast.tech> | ||
*/ | ||
final class RecursiveDirectoryIterator extends RecursiveIteratorIterator | ||
{ | ||
public function __construct(string $path) | ||
{ | ||
parent::__construct(new NativeRecursiveDirectoryIterator($path)); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/Retrieval/Loader/Directory/WildcardDirectoryIterator.php
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,62 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Lugha package. | ||
* | ||
* (c) Bernard Ngandu <bernard@devscast.tech> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Devscast\Lugha\Retrieval\Loader\Directory; | ||
|
||
use DirectoryIterator; | ||
use FilterIterator; | ||
|
||
/** | ||
* Class WildcardDirectoryIterator. | ||
* | ||
* @extends FilterIterator<int, string, RecursiveDirectoryIterator|DirectoryIterator> | ||
* | ||
* @author bernard-ng <bernard@devscast.tech> | ||
*/ | ||
final class WildcardDirectoryIterator extends FilterIterator | ||
{ | ||
private string $regex; | ||
|
||
public function __construct(string $path) | ||
{ | ||
$recursive = false; | ||
|
||
if (str_starts_with($path, '-R ')) { | ||
$recursive = true; | ||
$path = substr($path, 3); | ||
} | ||
|
||
if (preg_match('~/?([^/]*\*[^/]*)$~', $path, $matches)) { // matched wildcards in filename | ||
$path = substr($path, 0, -strlen($matches[1]) - 1); // strip wildcards part from path | ||
$this->regex = '~^' . str_replace('*', '.*', str_replace('.', '\.', $matches[1])) . '$~'; // convert wildcards to regex | ||
|
||
if (! $path) { | ||
$path = '.'; // if no path given, we assume CWD; | ||
} | ||
} | ||
|
||
parent::__construct($recursive ? new RecursiveDirectoryIterator($path) : new DirectoryIterator($path)); | ||
} | ||
|
||
/** | ||
* Checks for regex in current filename, or matches all if no regex specified | ||
*/ | ||
#[\Override] | ||
public function accept(): bool | ||
{ | ||
/** @var RecursiveDirectoryIterator|DirectoryIterator $iterator */ | ||
$iterator = $this->getInnerIterator(); | ||
|
||
return (bool) preg_match($this->regex, $iterator->getFilename()); | ||
} | ||
} |
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,64 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Lugha package. | ||
* | ||
* (c) Bernard Ngandu <bernard@devscast.tech> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Devscast\Lugha\Retrieval\Loader\Reader; | ||
|
||
use Devscast\Lugha\Retrieval\Loader\Reader\Exception\FileNotFoundException; | ||
use Devscast\Lugha\Retrieval\Loader\Reader\Exception\UnreadableFileException; | ||
use Devscast\Lugha\Retrieval\Loader\Reader\Exception\UnsupportedFileException; | ||
use Symfony\Component\Filesystem\Path; | ||
|
||
/** | ||
* Interface AbstractReader. | ||
* | ||
* @author bernard-ng <bernard@devscast.tech> | ||
*/ | ||
abstract readonly class AbstractReader | ||
{ | ||
/** | ||
* Supported extensions regex pattern | ||
*/ | ||
public const string SUPPORTED_EXTENSIONS_PATTERN = ''; | ||
|
||
/** | ||
* @throws UnsupportedFileException If the file extension is not supported and the check is not skipped. | ||
* @throws UnreadableFileException When the content cannot be read for any other reason | ||
* @throws FileNotFoundException When the given file does not exist | ||
*/ | ||
abstract public function readContent(string $path, bool $skipExtensionCheck = false): string; | ||
|
||
final public function supports(string $path): bool | ||
{ | ||
$extension = Path::getExtension($path, forceLowerCase: true); | ||
return (bool) preg_match(static::SUPPORTED_EXTENSIONS_PATTERN, $extension); | ||
} | ||
|
||
final public function ensureSupported(string $path): void | ||
{ | ||
$extension = Path::getExtension($path, forceLowerCase: true); | ||
if ($this->supports($path) === false) { | ||
throw new UnsupportedFileException([$extension, static::SUPPORTED_EXTENSIONS_PATTERN]); | ||
} | ||
} | ||
|
||
final public function ensureFileExists(string $path): void | ||
{ | ||
if (file_exists($path) === false) { | ||
throw new FileNotFoundException($path); | ||
} | ||
|
||
if (is_readable($path) === false) { | ||
throw new UnreadableFileException($path); | ||
} | ||
} | ||
} |
Oops, something went wrong.