Important memory improvement

This commit is contained in:
Joop Schilder 2021-03-23 23:10:23 +01:00
parent 8f88b626f0
commit cc2696668c
4 changed files with 65 additions and 33 deletions

View 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));
}
}

View File

@ -5,6 +5,7 @@ namespace IO\Input;
use Filter\DocumentFilter; use Filter\DocumentFilter;
use Filter\FilterFactory; use Filter\FilterFactory;
use IO\Exception\DirectoryNotFoundException; use IO\Exception\DirectoryNotFoundException;
use IO\Exception\DirectoryNotReadableException;
use IO\Exception\NotADirectoryException; use IO\Exception\NotADirectoryException;
class FinderArguments class FinderArguments
@ -14,15 +15,6 @@ class FinderArguments
private ?string $directory; private ?string $directory;
private array $filters; 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 public static function createFromGlobals(): self
{ {
$arguments = self::getArguments(); $arguments = self::getArguments();
@ -33,15 +25,17 @@ class FinderArguments
return new self($dir, $arguments); 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 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; return $this->directory;
} }
@ -52,4 +46,17 @@ class FinderArguments
{ {
return $this->filters; 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);
}
}
} }

View File

@ -11,12 +11,7 @@ class ShowInfoArguments
{ {
use ArgvAccess; use ArgvAccess;
private ?string $file; private SplFileInfo $file;
public function __construct(?string $file)
{
$this->file = $file;
}
public static function createFromGlobals(): self public static function createFromGlobals(): self
{ {
@ -24,18 +19,27 @@ class ShowInfoArguments
return new self(array_shift($arguments)); return new self(array_shift($arguments));
} }
public function __construct(?string $file)
{
$this->guardUnusableFile($file);
$this->file = new SplFileInfo($file);
}
public function getFile(): SplFileInfo 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(); throw new MissingFileArgumentException();
} }
if (!file_exists($this->file)) { if (!file_exists($file)) {
throw new FileNotFoundException($this->file); throw new FileNotFoundException($file);
} }
if (!is_readable($this->file)) { if (!is_readable($file)) {
throw new FileNotReadableException($this->file); throw new FileNotReadableException($file);
} }
return new SplFileInfo($this->file);
} }
} }

View File

@ -22,9 +22,19 @@ class RecursiveDocumentLocator
RecursiveIteratorIterator::SELF_FIRST RecursiveIteratorIterator::SELF_FIRST
); );
return collect($iterator) $documents = [];
->filter(static fn(SplFileInfo $fileInfo) => $fileInfo->isFile()) foreach ($iterator as $file) {
->filter(static fn(SplFileInfo $fileInfo) => preg_match('/.pdf$/i', $fileInfo->getBasename())) if ($this->validate($file)) {
->map(fn(SplFileInfo $fileInfo) => $this->documentFactory->createDocument($fileInfo)); $documents[] = $this->documentFactory->createDocument($file);
}
}
return collect($documents);
}
private function validate(SplFileInfo $file): bool
{
return $file->isFile()
&& preg_match('/.pdf$/i', $file->getBasename());
} }
} }