Important memory improvement
This commit is contained in:
parent
8f88b626f0
commit
cc2696668c
11
src/IO/Exception/DirectoryNotReadableException.php
Normal file
11
src/IO/Exception/DirectoryNotReadableException.php
Normal file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace IO\Exception;
|
||||
|
||||
class DirectoryNotReadableException extends IOException
|
||||
{
|
||||
public function __construct(string $directory)
|
||||
{
|
||||
parent::__construct(sprintf('Directory \'%s\' is not readable', $directory));
|
||||
}
|
||||
}
|
@ -5,6 +5,7 @@ namespace IO\Input;
|
||||
use Filter\DocumentFilter;
|
||||
use Filter\FilterFactory;
|
||||
use IO\Exception\DirectoryNotFoundException;
|
||||
use IO\Exception\DirectoryNotReadableException;
|
||||
use IO\Exception\NotADirectoryException;
|
||||
|
||||
class FinderArguments
|
||||
@ -14,15 +15,6 @@ class FinderArguments
|
||||
private ?string $directory;
|
||||
private array $filters;
|
||||
|
||||
public function __construct(?string $directory, array $filters)
|
||||
{
|
||||
$this->directory = $directory;
|
||||
$this->filters = $filters;
|
||||
|
||||
$factory = new FilterFactory();
|
||||
$this->filters = array_map([$factory, 'createFromString'], $this->filters);
|
||||
}
|
||||
|
||||
public static function createFromGlobals(): self
|
||||
{
|
||||
$arguments = self::getArguments();
|
||||
@ -33,15 +25,17 @@ class FinderArguments
|
||||
return new self($dir, $arguments);
|
||||
}
|
||||
|
||||
public function __construct(?string $directory, array $filters)
|
||||
{
|
||||
$this->guardUnusableDirectory($directory);
|
||||
$this->directory = realpath($directory);
|
||||
|
||||
$factory = new FilterFactory();
|
||||
$this->filters = array_map([$factory, 'createFromString'], $filters);
|
||||
}
|
||||
|
||||
public function getDirectory(): string
|
||||
{
|
||||
if (!file_exists($this->directory)) {
|
||||
throw new DirectoryNotFoundException($this->directory);
|
||||
}
|
||||
if (!is_dir($this->directory)) {
|
||||
throw new NotADirectoryException($this->directory);
|
||||
}
|
||||
|
||||
return $this->directory;
|
||||
}
|
||||
|
||||
@ -52,4 +46,17 @@ class FinderArguments
|
||||
{
|
||||
return $this->filters;
|
||||
}
|
||||
|
||||
private function guardUnusableDirectory(string $directory): void
|
||||
{
|
||||
if (!file_exists($directory)) {
|
||||
throw new DirectoryNotFoundException($directory);
|
||||
}
|
||||
if (!is_dir($directory)) {
|
||||
throw new NotADirectoryException($directory);
|
||||
}
|
||||
if (!is_readable($directory)) {
|
||||
throw new DirectoryNotReadableException($directory);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -11,12 +11,7 @@ class ShowInfoArguments
|
||||
{
|
||||
use ArgvAccess;
|
||||
|
||||
private ?string $file;
|
||||
|
||||
public function __construct(?string $file)
|
||||
{
|
||||
$this->file = $file;
|
||||
}
|
||||
private SplFileInfo $file;
|
||||
|
||||
public static function createFromGlobals(): self
|
||||
{
|
||||
@ -24,18 +19,27 @@ class ShowInfoArguments
|
||||
return new self(array_shift($arguments));
|
||||
}
|
||||
|
||||
public function __construct(?string $file)
|
||||
{
|
||||
$this->guardUnusableFile($file);
|
||||
$this->file = new SplFileInfo($file);
|
||||
}
|
||||
|
||||
public function getFile(): SplFileInfo
|
||||
{
|
||||
if (is_null($this->file)) {
|
||||
return $this->file;
|
||||
}
|
||||
|
||||
private function guardUnusableFile(string $file): void
|
||||
{
|
||||
if (is_null($file)) {
|
||||
throw new MissingFileArgumentException();
|
||||
}
|
||||
if (!file_exists($this->file)) {
|
||||
throw new FileNotFoundException($this->file);
|
||||
if (!file_exists($file)) {
|
||||
throw new FileNotFoundException($file);
|
||||
}
|
||||
if (!is_readable($this->file)) {
|
||||
throw new FileNotReadableException($this->file);
|
||||
if (!is_readable($file)) {
|
||||
throw new FileNotReadableException($file);
|
||||
}
|
||||
|
||||
return new SplFileInfo($this->file);
|
||||
}
|
||||
}
|
||||
|
@ -22,9 +22,19 @@ class RecursiveDocumentLocator
|
||||
RecursiveIteratorIterator::SELF_FIRST
|
||||
);
|
||||
|
||||
return collect($iterator)
|
||||
->filter(static fn(SplFileInfo $fileInfo) => $fileInfo->isFile())
|
||||
->filter(static fn(SplFileInfo $fileInfo) => preg_match('/.pdf$/i', $fileInfo->getBasename()))
|
||||
->map(fn(SplFileInfo $fileInfo) => $this->documentFactory->createDocument($fileInfo));
|
||||
$documents = [];
|
||||
foreach ($iterator as $file) {
|
||||
if ($this->validate($file)) {
|
||||
$documents[] = $this->documentFactory->createDocument($file);
|
||||
}
|
||||
}
|
||||
|
||||
return collect($documents);
|
||||
}
|
||||
|
||||
private function validate(SplFileInfo $file): bool
|
||||
{
|
||||
return $file->isFile()
|
||||
&& preg_match('/.pdf$/i', $file->getBasename());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user