-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
10 changed files
with
283 additions
and
23 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
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,80 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Panaly\Provider; | ||
|
||
use Panaly\Provider\FileProvider\InvalidFileAccess; | ||
|
||
use function file_exists; | ||
use function file_get_contents; | ||
use function file_put_contents; | ||
use function is_dir; | ||
use function is_readable; | ||
use function unlink; | ||
|
||
/** | ||
* The class delivers a centralized toolset to work with files. Through the Runtime it will also be available | ||
* to plugins to be utilized - but because of a static storage it can also be instantiated as often as needed. | ||
* A centralized usage can be beneficial to a longer running job with identical files opened by multiple methods. | ||
* In this case every running process would access the filesystem and bring a file to the memory while this could | ||
* be done once. | ||
* It has to be ensured that during the process read and write should be handled with this process because the write | ||
* process will overwrite the cached content which would otherwise be invalid. | ||
* Another advantage of this provider class is that the error handling is centralized and must not be implement again | ||
* and again. | ||
*/ | ||
class FileProvider | ||
{ | ||
/** @var array<string, string> */ | ||
protected static array $files = []; | ||
|
||
public function read(string $path): string | ||
{ | ||
if (isset(self::$files[$path])) { | ||
return self::$files[$path]; | ||
} | ||
|
||
if ($this->isDirectory($path)) { | ||
throw InvalidFileAccess::fileIsADirectory($path); | ||
} | ||
|
||
if (! $this->isFile($path)) { | ||
throw InvalidFileAccess::fileNotAccessible($path); | ||
} | ||
|
||
$fileContent = @file_get_contents($path); | ||
if ($fileContent === false) { | ||
throw InvalidFileAccess::fileNotReadable($path); | ||
} | ||
|
||
return self::$files[$path] = $fileContent; | ||
} | ||
|
||
public function write(string $path, string $content): void | ||
{ | ||
self::$files[$path] = $content; | ||
|
||
file_put_contents($path, $content); | ||
} | ||
|
||
public function remove(string $path): void | ||
{ | ||
if (! isset(self::$files[$path])) { | ||
throw InvalidFileAccess::onlyProvidedFilesAreRemovable($path); | ||
} | ||
|
||
unset(self::$files[$path]); | ||
@unlink($path); | ||
} | ||
|
||
public function isFile(string $path): bool | ||
{ | ||
return file_exists($path) && is_readable($path); | ||
} | ||
|
||
public function isDirectory(string $path): bool | ||
{ | ||
return is_dir($path) && is_readable($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,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Panaly\Provider\FileProvider; | ||
|
||
use RuntimeException; | ||
|
||
final class InvalidFileAccess extends RuntimeException | ||
{ | ||
public static function fileIsADirectory(string $file): InvalidFileAccess | ||
{ | ||
return new self('The provided file path "' . $file . '" is a directory.'); | ||
} | ||
|
||
public static function fileNotAccessible(string $file): InvalidFileAccess | ||
{ | ||
return new self('The provided file "' . $file . '" is not accessible or does not exist.'); | ||
} | ||
|
||
public static function fileNotReadable(string $file): InvalidFileAccess | ||
{ | ||
return new self('The provided file "' . $file . '" is not readable or does not exist.'); | ||
} | ||
|
||
public static function onlyProvidedFilesAreRemovable(string $file): InvalidFileAccess | ||
{ | ||
return new self('The provided file "' . $file . '" was not read by the provider and can so not be removed.'); | ||
} | ||
} |
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,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Panaly\Test\Double; | ||
|
||
use Panaly\Provider\FileProvider; | ||
|
||
use function array_key_exists; | ||
use function file_exists; | ||
use function file_get_contents; | ||
|
||
class MemoryFileProvider extends FileProvider | ||
{ | ||
public function write(string $path, string $content): void | ||
{ | ||
self::$files[$path] = $content; | ||
} | ||
|
||
public function remove(string $path): void | ||
{ | ||
unset(self::$files[$path]); | ||
} | ||
|
||
public function isFile(string $path): bool | ||
{ | ||
return array_key_exists($path, self::$files); | ||
} | ||
|
||
public function isDirectory(string $path): bool | ||
{ | ||
return false; | ||
} | ||
|
||
public function addFixture(string $file, string|null $content = null): void | ||
{ | ||
if ($content === null && file_exists($file)) { | ||
self::$files[$file] = (string) file_get_contents($file); | ||
|
||
return; | ||
} | ||
|
||
self::$files[$file] = $content ?? ''; | ||
} | ||
|
||
public function reset(): void | ||
{ | ||
self::$files = []; | ||
} | ||
} |
Oops, something went wrong.