mirror of
https://gitlab.com/signalytic/client-external/streamline/streamline-emr.git
synced 2026-09-12 03:01:32 +00:00
resolved conflicts
This commit is contained in:
@@ -0,0 +1,290 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace League\Flysystem;
|
||||
|
||||
use DateTimeInterface;
|
||||
use Generator;
|
||||
use League\Flysystem\UrlGeneration\PrefixPublicUrlGenerator;
|
||||
use League\Flysystem\UrlGeneration\PublicUrlGenerator;
|
||||
use League\Flysystem\UrlGeneration\ShardedPrefixPublicUrlGenerator;
|
||||
use League\Flysystem\UrlGeneration\TemporaryUrlGenerator;
|
||||
use Throwable;
|
||||
|
||||
use function array_key_exists;
|
||||
use function is_array;
|
||||
|
||||
class Filesystem implements FilesystemOperator
|
||||
{
|
||||
use CalculateChecksumFromStream;
|
||||
|
||||
private Config $config;
|
||||
private PathNormalizer $pathNormalizer;
|
||||
|
||||
public function __construct(
|
||||
private FilesystemAdapter $adapter,
|
||||
array $config = [],
|
||||
?PathNormalizer $pathNormalizer = null,
|
||||
private ?PublicUrlGenerator $publicUrlGenerator = null,
|
||||
private ?TemporaryUrlGenerator $temporaryUrlGenerator = null,
|
||||
) {
|
||||
$this->config = new Config($config);
|
||||
$this->pathNormalizer = $pathNormalizer ?? new WhitespacePathNormalizer();
|
||||
}
|
||||
|
||||
public function fileExists(string $location): bool
|
||||
{
|
||||
return $this->adapter->fileExists($this->pathNormalizer->normalizePath($location));
|
||||
}
|
||||
|
||||
public function directoryExists(string $location): bool
|
||||
{
|
||||
return $this->adapter->directoryExists($this->pathNormalizer->normalizePath($location));
|
||||
}
|
||||
|
||||
public function has(string $location): bool
|
||||
{
|
||||
$path = $this->pathNormalizer->normalizePath($location);
|
||||
|
||||
return $this->adapter->fileExists($path) || $this->adapter->directoryExists($path);
|
||||
}
|
||||
|
||||
public function write(string $location, string $contents, array $config = []): void
|
||||
{
|
||||
$this->adapter->write(
|
||||
$this->pathNormalizer->normalizePath($location),
|
||||
$contents,
|
||||
$this->config->extend($config)
|
||||
);
|
||||
}
|
||||
|
||||
public function writeStream(string $location, $contents, array $config = []): void
|
||||
{
|
||||
/* @var resource $contents */
|
||||
$this->assertIsResource($contents);
|
||||
$this->rewindStream($contents);
|
||||
$this->adapter->writeStream(
|
||||
$this->pathNormalizer->normalizePath($location),
|
||||
$contents,
|
||||
$this->config->extend($config)
|
||||
);
|
||||
}
|
||||
|
||||
public function read(string $location): string
|
||||
{
|
||||
return $this->adapter->read($this->pathNormalizer->normalizePath($location));
|
||||
}
|
||||
|
||||
public function readStream(string $location)
|
||||
{
|
||||
return $this->adapter->readStream($this->pathNormalizer->normalizePath($location));
|
||||
}
|
||||
|
||||
public function delete(string $location): void
|
||||
{
|
||||
$this->adapter->delete($this->pathNormalizer->normalizePath($location));
|
||||
}
|
||||
|
||||
public function deleteDirectory(string $location): void
|
||||
{
|
||||
$this->adapter->deleteDirectory($this->pathNormalizer->normalizePath($location));
|
||||
}
|
||||
|
||||
public function createDirectory(string $location, array $config = []): void
|
||||
{
|
||||
$this->adapter->createDirectory(
|
||||
$this->pathNormalizer->normalizePath($location),
|
||||
$this->config->extend($config)
|
||||
);
|
||||
}
|
||||
|
||||
public function listContents(string $location, bool $deep = self::LIST_SHALLOW): DirectoryListing
|
||||
{
|
||||
$path = $this->pathNormalizer->normalizePath($location);
|
||||
$listing = $this->adapter->listContents($path, $deep);
|
||||
|
||||
return new DirectoryListing($this->pipeListing($location, $deep, $listing));
|
||||
}
|
||||
|
||||
private function pipeListing(string $location, bool $deep, iterable $listing): Generator
|
||||
{
|
||||
try {
|
||||
foreach ($listing as $item) {
|
||||
yield $item;
|
||||
}
|
||||
} catch (Throwable $exception) {
|
||||
throw UnableToListContents::atLocation($location, $deep, $exception);
|
||||
}
|
||||
}
|
||||
|
||||
public function move(string $source, string $destination, array $config = []): void
|
||||
{
|
||||
$config = $this->resolveConfigForMoveAndCopy($config);
|
||||
$from = $this->pathNormalizer->normalizePath($source);
|
||||
$to = $this->pathNormalizer->normalizePath($destination);
|
||||
|
||||
if ($from === $to) {
|
||||
$resolutionStrategy = $config->get(Config::OPTION_MOVE_IDENTICAL_PATH, ResolveIdenticalPathConflict::TRY);
|
||||
|
||||
if ($resolutionStrategy === ResolveIdenticalPathConflict::FAIL) {
|
||||
throw UnableToMoveFile::sourceAndDestinationAreTheSame($source, $destination);
|
||||
} elseif ($resolutionStrategy === ResolveIdenticalPathConflict::IGNORE) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$this->adapter->move($from, $to, $config);
|
||||
}
|
||||
|
||||
public function copy(string $source, string $destination, array $config = []): void
|
||||
{
|
||||
$config = $this->resolveConfigForMoveAndCopy($config);
|
||||
$from = $this->pathNormalizer->normalizePath($source);
|
||||
$to = $this->pathNormalizer->normalizePath($destination);
|
||||
|
||||
if ($from === $to) {
|
||||
$resolutionStrategy = $config->get(Config::OPTION_COPY_IDENTICAL_PATH, ResolveIdenticalPathConflict::TRY);
|
||||
|
||||
if ($resolutionStrategy === ResolveIdenticalPathConflict::FAIL) {
|
||||
throw UnableToCopyFile::sourceAndDestinationAreTheSame($source, $destination);
|
||||
} elseif ($resolutionStrategy === ResolveIdenticalPathConflict::IGNORE) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$this->adapter->copy($from, $to, $config);
|
||||
}
|
||||
|
||||
public function lastModified(string $path): int
|
||||
{
|
||||
return $this->adapter->lastModified($this->pathNormalizer->normalizePath($path))->lastModified();
|
||||
}
|
||||
|
||||
public function fileSize(string $path): int
|
||||
{
|
||||
return $this->adapter->fileSize($this->pathNormalizer->normalizePath($path))->fileSize();
|
||||
}
|
||||
|
||||
public function mimeType(string $path): string
|
||||
{
|
||||
return $this->adapter->mimeType($this->pathNormalizer->normalizePath($path))->mimeType();
|
||||
}
|
||||
|
||||
public function setVisibility(string $path, string $visibility): void
|
||||
{
|
||||
$this->adapter->setVisibility($this->pathNormalizer->normalizePath($path), $visibility);
|
||||
}
|
||||
|
||||
public function visibility(string $path): string
|
||||
{
|
||||
return $this->adapter->visibility($this->pathNormalizer->normalizePath($path))->visibility();
|
||||
}
|
||||
|
||||
public function publicUrl(string $path, array $config = []): string
|
||||
{
|
||||
$this->publicUrlGenerator ??= $this->resolvePublicUrlGenerator()
|
||||
?? throw UnableToGeneratePublicUrl::noGeneratorConfigured($path);
|
||||
$config = $this->config->extend($config);
|
||||
|
||||
return $this->publicUrlGenerator->publicUrl(
|
||||
$this->pathNormalizer->normalizePath($path),
|
||||
$config,
|
||||
);
|
||||
}
|
||||
|
||||
public function temporaryUrl(string $path, DateTimeInterface $expiresAt, array $config = []): string
|
||||
{
|
||||
$generator = $this->temporaryUrlGenerator ?? $this->adapter;
|
||||
|
||||
if ($generator instanceof TemporaryUrlGenerator) {
|
||||
return $generator->temporaryUrl(
|
||||
$this->pathNormalizer->normalizePath($path),
|
||||
$expiresAt,
|
||||
$this->config->extend($config)
|
||||
);
|
||||
}
|
||||
|
||||
throw UnableToGenerateTemporaryUrl::noGeneratorConfigured($path);
|
||||
}
|
||||
|
||||
public function checksum(string $path, array $config = []): string
|
||||
{
|
||||
$config = $this->config->extend($config);
|
||||
|
||||
if ( ! $this->adapter instanceof ChecksumProvider) {
|
||||
return $this->calculateChecksumFromStream($path, $config);
|
||||
}
|
||||
|
||||
try {
|
||||
return $this->adapter->checksum(
|
||||
$this->pathNormalizer->normalizePath($path),
|
||||
$config,
|
||||
);
|
||||
} catch (ChecksumAlgoIsNotSupported) {
|
||||
return $this->calculateChecksumFromStream(
|
||||
$this->pathNormalizer->normalizePath($path),
|
||||
$config,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private function resolvePublicUrlGenerator(): ?PublicUrlGenerator
|
||||
{
|
||||
if ($publicUrl = $this->config->get('public_url')) {
|
||||
return match (true) {
|
||||
is_array($publicUrl) => new ShardedPrefixPublicUrlGenerator($publicUrl),
|
||||
default => new PrefixPublicUrlGenerator($publicUrl),
|
||||
};
|
||||
}
|
||||
|
||||
if ($this->adapter instanceof PublicUrlGenerator) {
|
||||
return $this->adapter;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $contents
|
||||
*/
|
||||
private function assertIsResource($contents): void
|
||||
{
|
||||
if (is_resource($contents) === false) {
|
||||
throw new InvalidStreamProvided(
|
||||
"Invalid stream provided, expected stream resource, received " . gettype($contents)
|
||||
);
|
||||
} elseif ($type = get_resource_type($contents) !== 'stream') {
|
||||
throw new InvalidStreamProvided(
|
||||
"Invalid stream provided, expected stream resource, received resource of type " . $type
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param resource $resource
|
||||
*/
|
||||
private function rewindStream($resource): void
|
||||
{
|
||||
if (ftell($resource) !== 0 && stream_get_meta_data($resource)['seekable']) {
|
||||
rewind($resource);
|
||||
}
|
||||
}
|
||||
|
||||
private function resolveConfigForMoveAndCopy(array $config): Config
|
||||
{
|
||||
$retainVisibility = $this->config->get(Config::OPTION_RETAIN_VISIBILITY, $config[Config::OPTION_RETAIN_VISIBILITY] ?? true);
|
||||
$fullConfig = $this->config->extend($config);
|
||||
|
||||
/*
|
||||
* By default, we retain visibility. When we do not retain visibility, the visibility setting
|
||||
* from the default configuration is ignored. Only when it is set explicitly, we propagate the
|
||||
* setting.
|
||||
*/
|
||||
if ($retainVisibility && ! array_key_exists(Config::OPTION_VISIBILITY, $config)) {
|
||||
$fullConfig = $fullConfig->withoutSettings(Config::OPTION_VISIBILITY)->extend($config);
|
||||
}
|
||||
|
||||
return $fullConfig;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,434 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace League\Flysystem;
|
||||
|
||||
use DateTimeInterface;
|
||||
use Throwable;
|
||||
|
||||
use function compact;
|
||||
use function method_exists;
|
||||
use function sprintf;
|
||||
|
||||
class MountManager implements FilesystemOperator
|
||||
{
|
||||
/**
|
||||
* @var array<string, FilesystemOperator>
|
||||
*/
|
||||
private $filesystems = [];
|
||||
|
||||
/**
|
||||
* @var Config
|
||||
*/
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* MountManager constructor.
|
||||
*
|
||||
* @param array<string,FilesystemOperator> $filesystems
|
||||
*/
|
||||
public function __construct(array $filesystems = [], array $config = [])
|
||||
{
|
||||
$this->mountFilesystems($filesystems);
|
||||
$this->config = new Config($config);
|
||||
}
|
||||
|
||||
/**
|
||||
* It is not recommended to mount filesystems after creation because interacting
|
||||
* with the Mount Manager becomes unpredictable. Use this as an escape hatch.
|
||||
*/
|
||||
public function dangerouslyMountFilesystems(string $key, FilesystemOperator $filesystem): void
|
||||
{
|
||||
$this->mountFilesystem($key, $filesystem);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string,FilesystemOperator> $filesystems
|
||||
*/
|
||||
public function extend(array $filesystems, array $config = []): MountManager
|
||||
{
|
||||
$clone = clone $this;
|
||||
$clone->config = $this->config->extend($config);
|
||||
$clone->mountFilesystems($filesystems);
|
||||
|
||||
return $clone;
|
||||
}
|
||||
|
||||
public function fileExists(string $location): bool
|
||||
{
|
||||
/** @var FilesystemOperator $filesystem */
|
||||
[$filesystem, $path] = $this->determineFilesystemAndPath($location);
|
||||
|
||||
try {
|
||||
return $filesystem->fileExists($path);
|
||||
} catch (Throwable $exception) {
|
||||
throw UnableToCheckFileExistence::forLocation($location, $exception);
|
||||
}
|
||||
}
|
||||
|
||||
public function has(string $location): bool
|
||||
{
|
||||
/** @var FilesystemOperator $filesystem */
|
||||
[$filesystem, $path] = $this->determineFilesystemAndPath($location);
|
||||
|
||||
try {
|
||||
return $filesystem->fileExists($path) || $filesystem->directoryExists($path);
|
||||
} catch (Throwable $exception) {
|
||||
throw UnableToCheckExistence::forLocation($location, $exception);
|
||||
}
|
||||
}
|
||||
|
||||
public function directoryExists(string $location): bool
|
||||
{
|
||||
/** @var FilesystemOperator $filesystem */
|
||||
[$filesystem, $path] = $this->determineFilesystemAndPath($location);
|
||||
|
||||
try {
|
||||
return $filesystem->directoryExists($path);
|
||||
} catch (Throwable $exception) {
|
||||
throw UnableToCheckDirectoryExistence::forLocation($location, $exception);
|
||||
}
|
||||
}
|
||||
|
||||
public function read(string $location): string
|
||||
{
|
||||
/** @var FilesystemOperator $filesystem */
|
||||
[$filesystem, $path] = $this->determineFilesystemAndPath($location);
|
||||
|
||||
try {
|
||||
return $filesystem->read($path);
|
||||
} catch (UnableToReadFile $exception) {
|
||||
throw UnableToReadFile::fromLocation($location, $exception->reason(), $exception);
|
||||
}
|
||||
}
|
||||
|
||||
public function readStream(string $location)
|
||||
{
|
||||
/** @var FilesystemOperator $filesystem */
|
||||
[$filesystem, $path] = $this->determineFilesystemAndPath($location);
|
||||
|
||||
try {
|
||||
return $filesystem->readStream($path);
|
||||
} catch (UnableToReadFile $exception) {
|
||||
throw UnableToReadFile::fromLocation($location, $exception->reason(), $exception);
|
||||
}
|
||||
}
|
||||
|
||||
public function listContents(string $location, bool $deep = self::LIST_SHALLOW): DirectoryListing
|
||||
{
|
||||
/** @var FilesystemOperator $filesystem */
|
||||
[$filesystem, $path, $mountIdentifier] = $this->determineFilesystemAndPath($location);
|
||||
|
||||
return
|
||||
$filesystem
|
||||
->listContents($path, $deep)
|
||||
->map(
|
||||
function (StorageAttributes $attributes) use ($mountIdentifier) {
|
||||
return $attributes->withPath(sprintf('%s://%s', $mountIdentifier, $attributes->path()));
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public function lastModified(string $location): int
|
||||
{
|
||||
/** @var FilesystemOperator $filesystem */
|
||||
[$filesystem, $path] = $this->determineFilesystemAndPath($location);
|
||||
|
||||
try {
|
||||
return $filesystem->lastModified($path);
|
||||
} catch (UnableToRetrieveMetadata $exception) {
|
||||
throw UnableToRetrieveMetadata::lastModified($location, $exception->reason(), $exception);
|
||||
}
|
||||
}
|
||||
|
||||
public function fileSize(string $location): int
|
||||
{
|
||||
/** @var FilesystemOperator $filesystem */
|
||||
[$filesystem, $path] = $this->determineFilesystemAndPath($location);
|
||||
|
||||
try {
|
||||
return $filesystem->fileSize($path);
|
||||
} catch (UnableToRetrieveMetadata $exception) {
|
||||
throw UnableToRetrieveMetadata::fileSize($location, $exception->reason(), $exception);
|
||||
}
|
||||
}
|
||||
|
||||
public function mimeType(string $location): string
|
||||
{
|
||||
/** @var FilesystemOperator $filesystem */
|
||||
[$filesystem, $path] = $this->determineFilesystemAndPath($location);
|
||||
|
||||
try {
|
||||
return $filesystem->mimeType($path);
|
||||
} catch (UnableToRetrieveMetadata $exception) {
|
||||
throw UnableToRetrieveMetadata::mimeType($location, $exception->reason(), $exception);
|
||||
}
|
||||
}
|
||||
|
||||
public function visibility(string $path): string
|
||||
{
|
||||
/** @var FilesystemOperator $filesystem */
|
||||
[$filesystem, $location] = $this->determineFilesystemAndPath($path);
|
||||
|
||||
try {
|
||||
return $filesystem->visibility($location);
|
||||
} catch (UnableToRetrieveMetadata $exception) {
|
||||
throw UnableToRetrieveMetadata::visibility($path, $exception->reason(), $exception);
|
||||
}
|
||||
}
|
||||
|
||||
public function write(string $location, string $contents, array $config = []): void
|
||||
{
|
||||
/** @var FilesystemOperator $filesystem */
|
||||
[$filesystem, $path] = $this->determineFilesystemAndPath($location);
|
||||
|
||||
try {
|
||||
$filesystem->write($path, $contents, $this->config->extend($config)->toArray());
|
||||
} catch (UnableToWriteFile $exception) {
|
||||
throw UnableToWriteFile::atLocation($location, $exception->reason(), $exception);
|
||||
}
|
||||
}
|
||||
|
||||
public function writeStream(string $location, $contents, array $config = []): void
|
||||
{
|
||||
/** @var FilesystemOperator $filesystem */
|
||||
[$filesystem, $path] = $this->determineFilesystemAndPath($location);
|
||||
$filesystem->writeStream($path, $contents, $this->config->extend($config)->toArray());
|
||||
}
|
||||
|
||||
public function setVisibility(string $path, string $visibility): void
|
||||
{
|
||||
/** @var FilesystemOperator $filesystem */
|
||||
[$filesystem, $path] = $this->determineFilesystemAndPath($path);
|
||||
$filesystem->setVisibility($path, $visibility);
|
||||
}
|
||||
|
||||
public function delete(string $location): void
|
||||
{
|
||||
/** @var FilesystemOperator $filesystem */
|
||||
[$filesystem, $path] = $this->determineFilesystemAndPath($location);
|
||||
|
||||
try {
|
||||
$filesystem->delete($path);
|
||||
} catch (UnableToDeleteFile $exception) {
|
||||
throw UnableToDeleteFile::atLocation($location, $exception->reason(), $exception);
|
||||
}
|
||||
}
|
||||
|
||||
public function deleteDirectory(string $location): void
|
||||
{
|
||||
/** @var FilesystemOperator $filesystem */
|
||||
[$filesystem, $path] = $this->determineFilesystemAndPath($location);
|
||||
|
||||
try {
|
||||
$filesystem->deleteDirectory($path);
|
||||
} catch (UnableToDeleteDirectory $exception) {
|
||||
throw UnableToDeleteDirectory::atLocation($location, $exception->reason(), $exception);
|
||||
}
|
||||
}
|
||||
|
||||
public function createDirectory(string $location, array $config = []): void
|
||||
{
|
||||
/** @var FilesystemOperator $filesystem */
|
||||
[$filesystem, $path] = $this->determineFilesystemAndPath($location);
|
||||
|
||||
try {
|
||||
$filesystem->createDirectory($path, $this->config->extend($config)->toArray());
|
||||
} catch (UnableToCreateDirectory $exception) {
|
||||
throw UnableToCreateDirectory::dueToFailure($location, $exception);
|
||||
}
|
||||
}
|
||||
|
||||
public function move(string $source, string $destination, array $config = []): void
|
||||
{
|
||||
/** @var FilesystemOperator $sourceFilesystem */
|
||||
/* @var FilesystemOperator $destinationFilesystem */
|
||||
[$sourceFilesystem, $sourcePath] = $this->determineFilesystemAndPath($source);
|
||||
[$destinationFilesystem, $destinationPath] = $this->determineFilesystemAndPath($destination);
|
||||
|
||||
$sourceFilesystem === $destinationFilesystem ? $this->moveInTheSameFilesystem(
|
||||
$sourceFilesystem,
|
||||
$sourcePath,
|
||||
$destinationPath,
|
||||
$source,
|
||||
$destination,
|
||||
$config,
|
||||
) : $this->moveAcrossFilesystems($source, $destination, $config);
|
||||
}
|
||||
|
||||
public function copy(string $source, string $destination, array $config = []): void
|
||||
{
|
||||
/** @var FilesystemOperator $sourceFilesystem */
|
||||
/* @var FilesystemOperator $destinationFilesystem */
|
||||
[$sourceFilesystem, $sourcePath] = $this->determineFilesystemAndPath($source);
|
||||
[$destinationFilesystem, $destinationPath] = $this->determineFilesystemAndPath($destination);
|
||||
|
||||
$sourceFilesystem === $destinationFilesystem ? $this->copyInSameFilesystem(
|
||||
$sourceFilesystem,
|
||||
$sourcePath,
|
||||
$destinationPath,
|
||||
$source,
|
||||
$destination,
|
||||
$config,
|
||||
) : $this->copyAcrossFilesystem(
|
||||
$sourceFilesystem,
|
||||
$sourcePath,
|
||||
$destinationFilesystem,
|
||||
$destinationPath,
|
||||
$source,
|
||||
$destination,
|
||||
$config,
|
||||
);
|
||||
}
|
||||
|
||||
public function publicUrl(string $path, array $config = []): string
|
||||
{
|
||||
/** @var FilesystemOperator $filesystem */
|
||||
[$filesystem, $path] = $this->determineFilesystemAndPath($path);
|
||||
|
||||
if ( ! method_exists($filesystem, 'publicUrl')) {
|
||||
throw new UnableToGeneratePublicUrl(sprintf('%s does not support generating public urls.', $filesystem::class), $path);
|
||||
}
|
||||
|
||||
return $filesystem->publicUrl($path, $config);
|
||||
}
|
||||
|
||||
public function temporaryUrl(string $path, DateTimeInterface $expiresAt, array $config = []): string
|
||||
{
|
||||
/** @var FilesystemOperator $filesystem */
|
||||
[$filesystem, $path] = $this->determineFilesystemAndPath($path);
|
||||
|
||||
if ( ! method_exists($filesystem, 'temporaryUrl')) {
|
||||
throw new UnableToGenerateTemporaryUrl(sprintf('%s does not support generating public urls.', $filesystem::class), $path);
|
||||
}
|
||||
|
||||
return $filesystem->temporaryUrl($path, $expiresAt, $this->config->extend($config)->toArray());
|
||||
}
|
||||
|
||||
public function checksum(string $path, array $config = []): string
|
||||
{
|
||||
/** @var FilesystemOperator $filesystem */
|
||||
[$filesystem, $path] = $this->determineFilesystemAndPath($path);
|
||||
|
||||
if ( ! method_exists($filesystem, 'checksum')) {
|
||||
throw new UnableToProvideChecksum(sprintf('%s does not support providing checksums.', $filesystem::class), $path);
|
||||
}
|
||||
|
||||
return $filesystem->checksum($path, $this->config->extend($config)->toArray());
|
||||
}
|
||||
|
||||
private function mountFilesystems(array $filesystems): void
|
||||
{
|
||||
foreach ($filesystems as $key => $filesystem) {
|
||||
$this->guardAgainstInvalidMount($key, $filesystem);
|
||||
/* @var string $key */
|
||||
/* @var FilesystemOperator $filesystem */
|
||||
$this->mountFilesystem($key, $filesystem);
|
||||
}
|
||||
}
|
||||
|
||||
private function guardAgainstInvalidMount(mixed $key, mixed $filesystem): void
|
||||
{
|
||||
if ( ! is_string($key)) {
|
||||
throw UnableToMountFilesystem::becauseTheKeyIsNotValid($key);
|
||||
}
|
||||
|
||||
if ( ! $filesystem instanceof FilesystemOperator) {
|
||||
throw UnableToMountFilesystem::becauseTheFilesystemWasNotValid($filesystem);
|
||||
}
|
||||
}
|
||||
|
||||
private function mountFilesystem(string $key, FilesystemOperator $filesystem): void
|
||||
{
|
||||
$this->filesystems[$key] = $filesystem;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
*
|
||||
* @return array{0:FilesystemOperator, 1:string, 2:string}
|
||||
*/
|
||||
private function determineFilesystemAndPath(string $path): array
|
||||
{
|
||||
if (strpos($path, '://') < 1) {
|
||||
throw UnableToResolveFilesystemMount::becauseTheSeparatorIsMissing($path);
|
||||
}
|
||||
|
||||
/** @var string $mountIdentifier */
|
||||
/** @var string $mountPath */
|
||||
[$mountIdentifier, $mountPath] = explode('://', $path, 2);
|
||||
|
||||
if ( ! array_key_exists($mountIdentifier, $this->filesystems)) {
|
||||
throw UnableToResolveFilesystemMount::becauseTheMountWasNotRegistered($mountIdentifier);
|
||||
}
|
||||
|
||||
return [$this->filesystems[$mountIdentifier], $mountPath, $mountIdentifier];
|
||||
}
|
||||
|
||||
private function copyInSameFilesystem(
|
||||
FilesystemOperator $sourceFilesystem,
|
||||
string $sourcePath,
|
||||
string $destinationPath,
|
||||
string $source,
|
||||
string $destination,
|
||||
array $config,
|
||||
): void {
|
||||
try {
|
||||
$sourceFilesystem->copy($sourcePath, $destinationPath, $this->config->extend($config)->toArray());
|
||||
} catch (UnableToCopyFile $exception) {
|
||||
throw UnableToCopyFile::fromLocationTo($source, $destination, $exception);
|
||||
}
|
||||
}
|
||||
|
||||
private function copyAcrossFilesystem(
|
||||
FilesystemOperator $sourceFilesystem,
|
||||
string $sourcePath,
|
||||
FilesystemOperator $destinationFilesystem,
|
||||
string $destinationPath,
|
||||
string $source,
|
||||
string $destination,
|
||||
array $config,
|
||||
): void {
|
||||
$config = $this->config->extend($config);
|
||||
$retainVisibility = (bool) $config->get(Config::OPTION_RETAIN_VISIBILITY, true);
|
||||
$visibility = $config->get(Config::OPTION_VISIBILITY);
|
||||
|
||||
try {
|
||||
if ($visibility == null && $retainVisibility) {
|
||||
$visibility = $sourceFilesystem->visibility($sourcePath);
|
||||
$config = $config->extend(compact('visibility'));
|
||||
}
|
||||
|
||||
$stream = $sourceFilesystem->readStream($sourcePath);
|
||||
$destinationFilesystem->writeStream($destinationPath, $stream, $config->toArray());
|
||||
} catch (UnableToRetrieveMetadata | UnableToReadFile | UnableToWriteFile $exception) {
|
||||
throw UnableToCopyFile::fromLocationTo($source, $destination, $exception);
|
||||
}
|
||||
}
|
||||
|
||||
private function moveInTheSameFilesystem(
|
||||
FilesystemOperator $sourceFilesystem,
|
||||
string $sourcePath,
|
||||
string $destinationPath,
|
||||
string $source,
|
||||
string $destination,
|
||||
array $config,
|
||||
): void {
|
||||
try {
|
||||
$sourceFilesystem->move($sourcePath, $destinationPath, $this->config->extend($config)->toArray());
|
||||
} catch (UnableToMoveFile $exception) {
|
||||
throw UnableToMoveFile::fromLocationTo($source, $destination, $exception);
|
||||
}
|
||||
}
|
||||
|
||||
private function moveAcrossFilesystems(string $source, string $destination, array $config = []): void
|
||||
{
|
||||
try {
|
||||
$this->copy($source, $destination, $config);
|
||||
$this->delete($source);
|
||||
} catch (UnableToCopyFile | UnableToDeleteFile $exception) {
|
||||
throw UnableToMoveFile::fromLocationTo($source, $destination, $exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace League\Flysystem;
|
||||
|
||||
use RuntimeException;
|
||||
use Throwable;
|
||||
|
||||
class UnableToCheckExistence extends RuntimeException implements FilesystemOperationFailed
|
||||
{
|
||||
final public function __construct(string $message = "", int $code = 0, ?Throwable $previous = null)
|
||||
{
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
|
||||
public static function forLocation(string $path, ?Throwable $exception = null): static
|
||||
{
|
||||
return new static("Unable to check existence for: {$path}", 0, $exception);
|
||||
}
|
||||
|
||||
public function operation(): string
|
||||
{
|
||||
return FilesystemOperationFailed::OPERATION_EXISTENCE_CHECK;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace League\Flysystem;
|
||||
|
||||
use RuntimeException;
|
||||
use Throwable;
|
||||
|
||||
final class UnableToCopyFile extends RuntimeException implements FilesystemOperationFailed
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $source;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $destination;
|
||||
|
||||
public function source(): string
|
||||
{
|
||||
return $this->source;
|
||||
}
|
||||
|
||||
public function destination(): string
|
||||
{
|
||||
return $this->destination;
|
||||
}
|
||||
|
||||
public static function fromLocationTo(
|
||||
string $sourcePath,
|
||||
string $destinationPath,
|
||||
?Throwable $previous = null
|
||||
): UnableToCopyFile {
|
||||
$e = new static("Unable to copy file from $sourcePath to $destinationPath", 0 , $previous);
|
||||
$e->source = $sourcePath;
|
||||
$e->destination = $destinationPath;
|
||||
|
||||
return $e;
|
||||
}
|
||||
|
||||
public static function sourceAndDestinationAreTheSame(string $source, string $destination): UnableToCopyFile
|
||||
{
|
||||
return UnableToCopyFile::because('Source and destination are the same', $source, $destination);
|
||||
}
|
||||
|
||||
public static function because(string $reason, string $sourcePath, string $destinationPath): UnableToCopyFile
|
||||
{
|
||||
$e = new static("Unable to copy file from $sourcePath to $destinationPath, because $reason");
|
||||
$e->source = $sourcePath;
|
||||
$e->destination = $destinationPath;
|
||||
|
||||
return $e;
|
||||
}
|
||||
|
||||
public function operation(): string
|
||||
{
|
||||
return FilesystemOperationFailed::OPERATION_COPY;
|
||||
}
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace League\Flysystem;
|
||||
|
||||
use RuntimeException;
|
||||
use Throwable;
|
||||
|
||||
final class UnableToDeleteDirectory extends RuntimeException implements FilesystemOperationFailed
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $location = '';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $reason;
|
||||
|
||||
public static function atLocation(
|
||||
string $location,
|
||||
string $reason = '',
|
||||
?Throwable $previous = null
|
||||
): UnableToDeleteDirectory {
|
||||
$e = new static(rtrim("Unable to delete directory located at: {$location}. {$reason}"), 0, $previous);
|
||||
$e->location = $location;
|
||||
$e->reason = $reason;
|
||||
|
||||
return $e;
|
||||
}
|
||||
|
||||
public function operation(): string
|
||||
{
|
||||
return FilesystemOperationFailed::OPERATION_DELETE_DIRECTORY;
|
||||
}
|
||||
|
||||
public function reason(): string
|
||||
{
|
||||
return $this->reason;
|
||||
}
|
||||
|
||||
public function location(): string
|
||||
{
|
||||
return $this->location;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace League\Flysystem;
|
||||
|
||||
use RuntimeException;
|
||||
use Throwable;
|
||||
|
||||
final class UnableToDeleteFile extends RuntimeException implements FilesystemOperationFailed
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $location = '';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $reason;
|
||||
|
||||
public static function atLocation(string $location, string $reason = '', ?Throwable $previous = null): UnableToDeleteFile
|
||||
{
|
||||
$e = new static(rtrim("Unable to delete file located at: {$location}. {$reason}"), 0, $previous);
|
||||
$e->location = $location;
|
||||
$e->reason = $reason;
|
||||
|
||||
return $e;
|
||||
}
|
||||
|
||||
public function operation(): string
|
||||
{
|
||||
return FilesystemOperationFailed::OPERATION_DELETE;
|
||||
}
|
||||
|
||||
public function reason(): string
|
||||
{
|
||||
return $this->reason;
|
||||
}
|
||||
|
||||
public function location(): string
|
||||
{
|
||||
return $this->location;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace League\Flysystem;
|
||||
|
||||
use RuntimeException;
|
||||
use Throwable;
|
||||
|
||||
final class UnableToMoveFile extends RuntimeException implements FilesystemOperationFailed
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $source;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $destination;
|
||||
|
||||
public static function sourceAndDestinationAreTheSame(string $source, string $destination): UnableToMoveFile
|
||||
{
|
||||
return UnableToMoveFile::because('Source and destination are the same', $source, $destination);
|
||||
}
|
||||
|
||||
public function source(): string
|
||||
{
|
||||
return $this->source;
|
||||
}
|
||||
|
||||
public function destination(): string
|
||||
{
|
||||
return $this->destination;
|
||||
}
|
||||
|
||||
public static function fromLocationTo(
|
||||
string $sourcePath,
|
||||
string $destinationPath,
|
||||
?Throwable $previous = null
|
||||
): UnableToMoveFile {
|
||||
$message = $previous?->getMessage() ?? "Unable to move file from $sourcePath to $destinationPath";
|
||||
$e = new static($message, 0, $previous);
|
||||
$e->source = $sourcePath;
|
||||
$e->destination = $destinationPath;
|
||||
|
||||
return $e;
|
||||
}
|
||||
|
||||
public static function because(
|
||||
string $reason,
|
||||
string $sourcePath,
|
||||
string $destinationPath,
|
||||
): UnableToMoveFile {
|
||||
$message = "Unable to move file from $sourcePath to $destinationPath, because $reason";
|
||||
$e = new static($message);
|
||||
$e->source = $sourcePath;
|
||||
$e->destination = $destinationPath;
|
||||
|
||||
return $e;
|
||||
}
|
||||
|
||||
public function operation(): string
|
||||
{
|
||||
return FilesystemOperationFailed::OPERATION_MOVE;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace League\Flysystem;
|
||||
|
||||
use RuntimeException;
|
||||
use Throwable;
|
||||
|
||||
final class UnableToReadFile extends RuntimeException implements FilesystemOperationFailed
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $location = '';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $reason = '';
|
||||
|
||||
public static function fromLocation(string $location, string $reason = '', ?Throwable $previous = null): UnableToReadFile
|
||||
{
|
||||
$e = new static(rtrim("Unable to read file from location: {$location}. {$reason}"), 0, $previous);
|
||||
$e->location = $location;
|
||||
$e->reason = $reason;
|
||||
|
||||
return $e;
|
||||
}
|
||||
|
||||
public function operation(): string
|
||||
{
|
||||
return FilesystemOperationFailed::OPERATION_READ;
|
||||
}
|
||||
|
||||
public function reason(): string
|
||||
{
|
||||
return $this->reason;
|
||||
}
|
||||
|
||||
public function location(): string
|
||||
{
|
||||
return $this->location;
|
||||
}
|
||||
}
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace League\Flysystem;
|
||||
|
||||
use RuntimeException;
|
||||
use Throwable;
|
||||
|
||||
final class UnableToRetrieveMetadata extends RuntimeException implements FilesystemOperationFailed
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $location;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $metadataType;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $reason;
|
||||
|
||||
public static function lastModified(string $location, string $reason = '', ?Throwable $previous = null): self
|
||||
{
|
||||
return static::create($location, FileAttributes::ATTRIBUTE_LAST_MODIFIED, $reason, $previous);
|
||||
}
|
||||
|
||||
public static function visibility(string $location, string $reason = '', ?Throwable $previous = null): self
|
||||
{
|
||||
return static::create($location, FileAttributes::ATTRIBUTE_VISIBILITY, $reason, $previous);
|
||||
}
|
||||
|
||||
public static function fileSize(string $location, string $reason = '', ?Throwable $previous = null): self
|
||||
{
|
||||
return static::create($location, FileAttributes::ATTRIBUTE_FILE_SIZE, $reason, $previous);
|
||||
}
|
||||
|
||||
public static function mimeType(string $location, string $reason = '', ?Throwable $previous = null): self
|
||||
{
|
||||
return static::create($location, FileAttributes::ATTRIBUTE_MIME_TYPE, $reason, $previous);
|
||||
}
|
||||
|
||||
public static function create(string $location, string $type, string $reason = '', ?Throwable $previous = null): self
|
||||
{
|
||||
$e = new static("Unable to retrieve the $type for file at location: $location. {$reason}", 0, $previous);
|
||||
$e->reason = $reason;
|
||||
$e->location = $location;
|
||||
$e->metadataType = $type;
|
||||
|
||||
return $e;
|
||||
}
|
||||
|
||||
public function reason(): string
|
||||
{
|
||||
return $this->reason;
|
||||
}
|
||||
|
||||
public function location(): string
|
||||
{
|
||||
return $this->location;
|
||||
}
|
||||
|
||||
public function metadataType(): string
|
||||
{
|
||||
return $this->metadataType;
|
||||
}
|
||||
|
||||
public function operation(): string
|
||||
{
|
||||
return FilesystemOperationFailed::OPERATION_RETRIEVE_METADATA;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace League\Flysystem;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
use Throwable;
|
||||
|
||||
use function rtrim;
|
||||
|
||||
final class UnableToSetVisibility extends RuntimeException implements FilesystemOperationFailed
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $location;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $reason;
|
||||
|
||||
public function reason(): string
|
||||
{
|
||||
return $this->reason;
|
||||
}
|
||||
|
||||
public static function atLocation(string $filename, string $extraMessage = '', ?Throwable $previous = null): self
|
||||
{
|
||||
$message = "Unable to set visibility for file {$filename}. $extraMessage";
|
||||
$e = new static(rtrim($message), 0, $previous);
|
||||
$e->reason = $extraMessage;
|
||||
$e->location = $filename;
|
||||
|
||||
return $e;
|
||||
}
|
||||
|
||||
public function operation(): string
|
||||
{
|
||||
return FilesystemOperationFailed::OPERATION_SET_VISIBILITY;
|
||||
}
|
||||
|
||||
public function location(): string
|
||||
{
|
||||
return $this->location;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace League\Flysystem;
|
||||
|
||||
use RuntimeException;
|
||||
use Throwable;
|
||||
|
||||
final class UnableToWriteFile extends RuntimeException implements FilesystemOperationFailed
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $location = '';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $reason;
|
||||
|
||||
public static function atLocation(string $location, string $reason = '', ?Throwable $previous = null): UnableToWriteFile
|
||||
{
|
||||
$e = new static(rtrim("Unable to write file at location: {$location}. {$reason}"), 0, $previous);
|
||||
$e->location = $location;
|
||||
$e->reason = $reason;
|
||||
|
||||
return $e;
|
||||
}
|
||||
|
||||
public function operation(): string
|
||||
{
|
||||
return FilesystemOperationFailed::OPERATION_WRITE;
|
||||
}
|
||||
|
||||
public function reason(): string
|
||||
{
|
||||
return $this->reason;
|
||||
}
|
||||
|
||||
public function location(): string
|
||||
{
|
||||
return $this->location;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user