Upgrade framework

This commit is contained in:
2023-11-14 16:54:35 +01:00
parent 1648a5cd42
commit 4fcf6fffcc
10548 changed files with 693138 additions and 466698 deletions

View File

@@ -0,0 +1,107 @@
<?php
namespace Illuminate\Filesystem;
use Aws\S3\S3Client;
use Illuminate\Support\Traits\Conditionable;
use League\Flysystem\AwsS3V3\AwsS3V3Adapter as S3Adapter;
use League\Flysystem\FilesystemOperator;
class AwsS3V3Adapter extends FilesystemAdapter
{
use Conditionable;
/**
* The AWS S3 client.
*
* @var \Aws\S3\S3Client
*/
protected $client;
/**
* Create a new AwsS3V3FilesystemAdapter instance.
*
* @param \League\Flysystem\FilesystemOperator $driver
* @param \League\Flysystem\AwsS3V3\AwsS3V3Adapter $adapter
* @param array $config
* @param \Aws\S3\S3Client $client
* @return void
*/
public function __construct(FilesystemOperator $driver, S3Adapter $adapter, array $config, S3Client $client)
{
parent::__construct($driver, $adapter, $config);
$this->client = $client;
}
/**
* Get the URL for the file at the given path.
*
* @param string $path
* @return string
*
* @throws \RuntimeException
*/
public function url($path)
{
// If an explicit base URL has been set on the disk configuration then we will use
// it as the base URL instead of the default path. This allows the developer to
// have full control over the base path for this filesystem's generated URLs.
if (isset($this->config['url'])) {
return $this->concatPathToUrl($this->config['url'], $this->prefixer->prefixPath($path));
}
return $this->client->getObjectUrl(
$this->config['bucket'], $this->prefixer->prefixPath($path)
);
}
/**
* Determine if temporary URLs can be generated.
*
* @return bool
*/
public function providesTemporaryUrls()
{
return true;
}
/**
* Get a temporary URL for the file at the given path.
*
* @param string $path
* @param \DateTimeInterface $expiration
* @param array $options
* @return string
*/
public function temporaryUrl($path, $expiration, array $options = [])
{
$command = $this->client->getCommand('GetObject', array_merge([
'Bucket' => $this->config['bucket'],
'Key' => $this->prefixer->prefixPath($path),
], $options));
$uri = $this->client->createPresignedRequest(
$command, $expiration, $options
)->getUri();
// If an explicit base URL has been set on the disk configuration then we will use
// it as the base URL instead of the default path. This allows the developer to
// have full control over the base path for this filesystem's generated URLs.
if (isset($this->config['temporary_url'])) {
$uri = $this->replaceBaseUrl($uri, $this->config['temporary_url']);
}
return (string) $uri;
}
/**
* Get the underlying S3 client.
*
* @return \Aws\S3\S3Client
*/
public function getClient()
{
return $this->client;
}
}

View File

@@ -4,12 +4,19 @@ namespace Illuminate\Filesystem;
use ErrorException;
use FilesystemIterator;
use Symfony\Component\Finder\Finder;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Support\LazyCollection;
use Illuminate\Support\Traits\Conditionable;
use Illuminate\Support\Traits\Macroable;
use RuntimeException;
use SplFileObject;
use Symfony\Component\Filesystem\Filesystem as SymfonyFilesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Mime\MimeTypes;
class Filesystem
{
use Conditionable;
use Macroable;
/**
@@ -23,6 +30,17 @@ class Filesystem
return file_exists($path);
}
/**
* Determine if a file or directory is missing.
*
* @param string $path
* @return bool
*/
public function missing($path)
{
return ! $this->exists($path);
}
/**
* Get the contents of a file.
*
@@ -38,7 +56,7 @@ class Filesystem
return $lock ? $this->sharedGet($path) : file_get_contents($path);
}
throw new FileNotFoundException("File does not exist at path {$path}");
throw new FileNotFoundException("File does not exist at path {$path}.");
}
/**
@@ -74,39 +92,89 @@ class Filesystem
* Get the returned value of a file.
*
* @param string $path
* @param array $data
* @return mixed
*
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
public function getRequire($path)
public function getRequire($path, array $data = [])
{
if ($this->isFile($path)) {
return require $path;
$__path = $path;
$__data = $data;
return (static function () use ($__path, $__data) {
extract($__data, EXTR_SKIP);
return require $__path;
})();
}
throw new FileNotFoundException("File does not exist at path {$path}");
throw new FileNotFoundException("File does not exist at path {$path}.");
}
/**
* Require the given file once.
*
* @param string $file
* @param string $path
* @param array $data
* @return mixed
*
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
public function requireOnce($file)
public function requireOnce($path, array $data = [])
{
require_once $file;
if ($this->isFile($path)) {
$__path = $path;
$__data = $data;
return (static function () use ($__path, $__data) {
extract($__data, EXTR_SKIP);
return require_once $__path;
})();
}
throw new FileNotFoundException("File does not exist at path {$path}.");
}
/**
* Get the MD5 hash of the file at the given path.
* Get the contents of a file one line at a time.
*
* @param string $path
* @return \Illuminate\Support\LazyCollection
*
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
public function lines($path)
{
if (! $this->isFile($path)) {
throw new FileNotFoundException(
"File does not exist at path {$path}."
);
}
return LazyCollection::make(function () use ($path) {
$file = new SplFileObject($path);
$file->setFlags(SplFileObject::DROP_NEW_LINE);
while (! $file->eof()) {
yield $file->fgets();
}
});
}
/**
* Get the hash of the file at the given path.
*
* @param string $path
* @param string $algorithm
* @return string
*/
public function hash($path)
public function hash($path, $algorithm = 'md5')
{
return md5_file($path);
return hash_file($algorithm, $path);
}
/**
@@ -115,13 +183,50 @@ class Filesystem
* @param string $path
* @param string $contents
* @param bool $lock
* @return int
* @return int|bool
*/
public function put($path, $contents, $lock = false)
{
return file_put_contents($path, $contents, $lock ? LOCK_EX : 0);
}
/**
* Write the contents of a file, replacing it atomically if it already exists.
*
* @param string $path
* @param string $content
* @return void
*/
public function replace($path, $content)
{
// If the path already exists and is a symlink, get the real path...
clearstatcache(true, $path);
$path = realpath($path) ?: $path;
$tempPath = tempnam(dirname($path), basename($path));
// Fix permissions of tempPath because `tempnam()` creates it with permissions set to 0600...
chmod($tempPath, 0777 - umask());
file_put_contents($tempPath, $content);
rename($tempPath, $path);
}
/**
* Replace a given string within a given file.
*
* @param array|string $search
* @param array|string $replace
* @param string $path
* @return void
*/
public function replaceInFile($search, $replace, $path)
{
file_put_contents($path, str_replace($search, $replace, file_get_contents($path)));
}
/**
* Prepend to a file.
*
@@ -154,7 +259,7 @@ class Filesystem
* Get or set UNIX mode of a file or directory.
*
* @param string $path
* @param int $mode
* @param int|null $mode
* @return mixed
*/
public function chmod($path, $mode = null)
@@ -180,7 +285,9 @@ class Filesystem
foreach ($paths as $path) {
try {
if (! @unlink($path)) {
if (@unlink($path)) {
clearstatcache(false, $path);
} else {
$success = false;
}
} catch (ErrorException $e) {
@@ -216,7 +323,7 @@ class Filesystem
}
/**
* Create a hard link to the target file or directory.
* Create a symlink to the target file or directory. On Windows, a hard link is created if the target is a file.
*
* @param string $target
* @param string $link
@@ -230,7 +337,29 @@ class Filesystem
$mode = $this->isDirectory($target) ? 'J' : 'H';
exec("mklink /{$mode} \"{$link}\" \"{$target}\"");
exec("mklink /{$mode} ".escapeshellarg($link).' '.escapeshellarg($target));
}
/**
* Create a relative symlink to the target file or directory.
*
* @param string $target
* @param string $link
* @return void
*
* @throws \RuntimeException
*/
public function relativeLink($target, $link)
{
if (! class_exists(SymfonyFilesystem::class)) {
throw new RuntimeException(
'To enable support for relative links, please install the symfony/filesystem package.'
);
}
$relativeTarget = (new SymfonyFilesystem)->makePathRelative($target, dirname($link));
$this->link($this->isFile($target) ? rtrim($relativeTarget, '/') : $relativeTarget, $link);
}
/**
@@ -277,6 +406,25 @@ class Filesystem
return pathinfo($path, PATHINFO_EXTENSION);
}
/**
* Guess the file extension from the mime-type of a given file.
*
* @param string $path
* @return string|null
*
* @throws \RuntimeException
*/
public function guessExtension($path)
{
if (! class_exists(MimeTypes::class)) {
throw new RuntimeException(
'To enable support for guessing extensions, please install the symfony/mime package.'
);
}
return (new MimeTypes)->getExtensions($this->mimeType($path))[0] ?? null;
}
/**
* Get the file type of a given file.
*
@@ -332,6 +480,18 @@ class Filesystem
return is_dir($directory);
}
/**
* Determine if the given path is a directory that does not contain any other files or directories.
*
* @param string $directory
* @param bool $ignoreDotFiles
* @return bool
*/
public function isEmptyDirectory($directory, $ignoreDotFiles = false)
{
return ! Finder::create()->ignoreDotFiles($ignoreDotFiles)->in($directory)->depth(0)->hasResults();
}
/**
* Determine if the given path is readable.
*
@@ -354,6 +514,20 @@ class Filesystem
return is_writable($path);
}
/**
* Determine if two files are the same by comparing their hashes.
*
* @param string $firstFile
* @param string $secondFile
* @return bool
*/
public function hasSameHash($firstFile, $secondFile)
{
$hash = @md5_file($firstFile);
return $hash && $hash === @md5_file($secondFile);
}
/**
* Determine if the given path is a file.
*
@@ -369,7 +543,7 @@ class Filesystem
* Find path names matching a given pattern.
*
* @param string $pattern
* @param int $flags
* @param int $flags
* @return array
*/
public function glob($pattern, $flags = 0)
@@ -381,22 +555,15 @@ class Filesystem
* Get an array of all files in a directory.
*
* @param string $directory
* @return array
* @param bool $hidden
* @return \Symfony\Component\Finder\SplFileInfo[]
*/
public function files($directory)
public function files($directory, $hidden = false)
{
$glob = glob($directory.DIRECTORY_SEPARATOR.'*');
if ($glob === false) {
return [];
}
// To get the appropriate files, we'll simply glob the directory and filter
// out any "files" that are not truly files so we do not end up with any
// directories in our list, but only true files within the directory.
return array_filter($glob, function ($file) {
return filetype($file) == 'file';
});
return iterator_to_array(
Finder::create()->files()->ignoreDotFiles(! $hidden)->in($directory)->depth(0)->sortByName(),
false
);
}
/**
@@ -404,11 +571,14 @@ class Filesystem
*
* @param string $directory
* @param bool $hidden
* @return array
* @return \Symfony\Component\Finder\SplFileInfo[]
*/
public function allFiles($directory, $hidden = false)
{
return iterator_to_array(Finder::create()->files()->ignoreDotFiles(! $hidden)->in($directory), false);
return iterator_to_array(
Finder::create()->files()->ignoreDotFiles(! $hidden)->in($directory)->sortByName(),
false
);
}
/**
@@ -421,20 +591,35 @@ class Filesystem
{
$directories = [];
foreach (Finder::create()->in($directory)->directories()->depth(0) as $dir) {
foreach (Finder::create()->in($directory)->directories()->depth(0)->sortByName() as $dir) {
$directories[] = $dir->getPathname();
}
return $directories;
}
/**
* Ensure a directory exists.
*
* @param string $path
* @param int $mode
* @param bool $recursive
* @return void
*/
public function ensureDirectoryExists($path, $mode = 0755, $recursive = true)
{
if (! $this->isDirectory($path)) {
$this->makeDirectory($path, $mode, $recursive);
}
}
/**
* Create a directory.
*
* @param string $path
* @param int $mode
* @param bool $recursive
* @param bool $force
* @param int $mode
* @param bool $recursive
* @param bool $force
* @return bool
*/
public function makeDirectory($path, $mode = 0755, $recursive = false, $force = false)
@@ -456,10 +641,8 @@ class Filesystem
*/
public function moveDirectory($from, $to, $overwrite = false)
{
if ($overwrite && $this->isDirectory($to)) {
if (! $this->deleteDirectory($to)) {
return false;
}
if ($overwrite && $this->isDirectory($to) && ! $this->deleteDirectory($to)) {
return false;
}
return @rename($from, $to) === true;
@@ -470,7 +653,7 @@ class Filesystem
*
* @param string $directory
* @param string $destination
* @param int $options
* @param int|null $options
* @return bool
*/
public function copyDirectory($directory, $destination, $options = null)
@@ -484,9 +667,7 @@ class Filesystem
// If the destination directory does not actually exist, we will go ahead and
// create it recursively, which just gets the destination prepared to copy
// the files over. Once we make the directory we'll proceed the copying.
if (! $this->isDirectory($destination)) {
$this->makeDirectory($destination, 0777, true);
}
$this->ensureDirectoryExists($destination, 0777);
$items = new FilesystemIterator($directory, $options);
@@ -507,10 +688,8 @@ class Filesystem
// If the current items is just a regular file, we will just copy this to the new
// location and keep looping. If for some reason the copy fails we'll bail out
// and return false, so the developer is aware that the copy process failed.
else {
if (! $this->copy($item->getPathname(), $target)) {
return false;
}
elseif (! $this->copy($item->getPathname(), $target)) {
return false;
}
}
@@ -523,7 +702,7 @@ class Filesystem
* The directory itself may be optionally preserved.
*
* @param string $directory
* @param bool $preserve
* @param bool $preserve
* @return bool
*/
public function deleteDirectory($directory, $preserve = false)
@@ -557,6 +736,27 @@ class Filesystem
return true;
}
/**
* Remove all of the directories within a given directory.
*
* @param string $directory
* @return bool
*/
public function deleteDirectories($directory)
{
$allDirectories = $this->directories($directory);
if (! empty($allDirectories)) {
foreach ($allDirectories as $directoryName) {
$this->deleteDirectory($directoryName);
}
return true;
}
return false;
}
/**
* Empty the specified directory of all files and folders.
*

View File

@@ -2,70 +2,176 @@
namespace Illuminate\Filesystem;
use RuntimeException;
use Illuminate\Http\File;
use Illuminate\Support\Str;
use InvalidArgumentException;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Collection;
use League\Flysystem\AdapterInterface;
use PHPUnit\Framework\Assert as PHPUnit;
use League\Flysystem\FilesystemInterface;
use League\Flysystem\AwsS3v3\AwsS3Adapter;
use League\Flysystem\FileNotFoundException;
use League\Flysystem\Adapter\Local as LocalAdapter;
use Closure;
use Illuminate\Contracts\Filesystem\Cloud as CloudFilesystemContract;
use Illuminate\Contracts\Filesystem\Filesystem as FilesystemContract;
use Illuminate\Contracts\Filesystem\FileNotFoundException as ContractFileNotFoundException;
use Illuminate\Http\File;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Conditionable;
use Illuminate\Support\Traits\Macroable;
use InvalidArgumentException;
use League\Flysystem\FilesystemAdapter as FlysystemAdapter;
use League\Flysystem\FilesystemOperator;
use League\Flysystem\Ftp\FtpAdapter;
use League\Flysystem\Local\LocalFilesystemAdapter as LocalAdapter;
use League\Flysystem\PathPrefixer;
use League\Flysystem\PhpseclibV3\SftpAdapter;
use League\Flysystem\StorageAttributes;
use League\Flysystem\UnableToCopyFile;
use League\Flysystem\UnableToCreateDirectory;
use League\Flysystem\UnableToDeleteDirectory;
use League\Flysystem\UnableToDeleteFile;
use League\Flysystem\UnableToMoveFile;
use League\Flysystem\UnableToProvideChecksum;
use League\Flysystem\UnableToReadFile;
use League\Flysystem\UnableToRetrieveMetadata;
use League\Flysystem\UnableToSetVisibility;
use League\Flysystem\UnableToWriteFile;
use League\Flysystem\Visibility;
use PHPUnit\Framework\Assert as PHPUnit;
use Psr\Http\Message\StreamInterface;
use RuntimeException;
use Symfony\Component\HttpFoundation\StreamedResponse;
class FilesystemAdapter implements FilesystemContract, CloudFilesystemContract
/**
* @mixin \League\Flysystem\FilesystemOperator
*/
class FilesystemAdapter implements CloudFilesystemContract
{
use Conditionable;
use Macroable {
__call as macroCall;
}
/**
* The Flysystem filesystem implementation.
*
* @var \League\Flysystem\FilesystemInterface
* @var \League\Flysystem\FilesystemOperator
*/
protected $driver;
/**
* The Flysystem adapter implementation.
*
* @var \League\Flysystem\FilesystemAdapter
*/
protected $adapter;
/**
* The filesystem configuration.
*
* @var array
*/
protected $config;
/**
* The Flysystem PathPrefixer instance.
*
* @var \League\Flysystem\PathPrefixer
*/
protected $prefixer;
/**
* The temporary URL builder callback.
*
* @var \Closure|null
*/
protected $temporaryUrlCallback;
/**
* Create a new filesystem adapter instance.
*
* @param \League\Flysystem\FilesystemInterface $driver
* @param \League\Flysystem\FilesystemOperator $driver
* @param \League\Flysystem\FilesystemAdapter $adapter
* @param array $config
* @return void
*/
public function __construct(FilesystemInterface $driver)
public function __construct(FilesystemOperator $driver, FlysystemAdapter $adapter, array $config = [])
{
$this->driver = $driver;
$this->adapter = $adapter;
$this->config = $config;
$separator = $config['directory_separator'] ?? DIRECTORY_SEPARATOR;
$this->prefixer = new PathPrefixer($config['root'] ?? '', $separator);
if (isset($config['prefix'])) {
$this->prefixer = new PathPrefixer($this->prefixer->prefixPath($config['prefix']), $separator);
}
}
/**
* Assert that the given file exists.
* Assert that the given file or directory exists.
*
* @param string $path
* @return void
* @param string|array $path
* @param string|null $content
* @return $this
*/
public function assertExists($path)
public function assertExists($path, $content = null)
{
PHPUnit::assertTrue(
$this->exists($path), "Unable to find a file at path [{$path}]."
);
clearstatcache();
$paths = Arr::wrap($path);
foreach ($paths as $path) {
PHPUnit::assertTrue(
$this->exists($path), "Unable to find a file or directory at path [{$path}]."
);
if (! is_null($content)) {
$actual = $this->get($path);
PHPUnit::assertSame(
$content,
$actual,
"File or directory [{$path}] was found, but content [{$actual}] does not match [{$content}]."
);
}
}
return $this;
}
/**
* Assert that the given file does not exist.
* Assert that the given file or directory does not exist.
*
* @param string $path
* @return void
* @param string|array $path
* @return $this
*/
public function assertMissing($path)
{
PHPUnit::assertFalse(
$this->exists($path), "Found unexpected file at path [{$path}]."
);
clearstatcache();
$paths = Arr::wrap($path);
foreach ($paths as $path) {
PHPUnit::assertFalse(
$this->exists($path), "Found unexpected file or directory at path [{$path}]."
);
}
return $this;
}
/**
* Determine if a file exists.
* Assert that the given directory is empty.
*
* @param string $path
* @return $this
*/
public function assertDirectoryEmpty($path)
{
PHPUnit::assertEmpty(
$this->allFiles($path), "Directory [{$path}] is not empty."
);
return $this;
}
/**
* Determine if a file or directory exists.
*
* @param string $path
* @return bool
@@ -76,35 +182,164 @@ class FilesystemAdapter implements FilesystemContract, CloudFilesystemContract
}
/**
* Get the contents of a file.
* Determine if a file or directory is missing.
*
* @param string $path
* @return bool
*/
public function missing($path)
{
return ! $this->exists($path);
}
/**
* Determine if a file exists.
*
* @param string $path
* @return bool
*/
public function fileExists($path)
{
return $this->driver->fileExists($path);
}
/**
* Determine if a file is missing.
*
* @param string $path
* @return bool
*/
public function fileMissing($path)
{
return ! $this->fileExists($path);
}
/**
* Determine if a directory exists.
*
* @param string $path
* @return bool
*/
public function directoryExists($path)
{
return $this->driver->directoryExists($path);
}
/**
* Determine if a directory is missing.
*
* @param string $path
* @return bool
*/
public function directoryMissing($path)
{
return ! $this->directoryExists($path);
}
/**
* Get the full path for the file at the given "short" path.
*
* @param string $path
* @return string
*/
public function path($path)
{
return $this->prefixer->prefixPath($path);
}
/**
* Get the contents of a file.
*
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
* @param string $path
* @return string|null
*/
public function get($path)
{
try {
return $this->driver->read($path);
} catch (FileNotFoundException $e) {
throw new ContractFileNotFoundException($path, $e->getCode(), $e);
} catch (UnableToReadFile $e) {
throw_if($this->throwsExceptions(), $e);
}
}
/**
* Create a streamed response for a given file.
*
* @param string $path
* @param string|null $name
* @param array $headers
* @param string|null $disposition
* @return \Symfony\Component\HttpFoundation\StreamedResponse
*/
public function response($path, $name = null, array $headers = [], $disposition = 'inline')
{
$response = new StreamedResponse;
if (! array_key_exists('Content-Type', $headers)) {
$headers['Content-Type'] = $this->mimeType($path);
}
if (! array_key_exists('Content-Length', $headers)) {
$headers['Content-Length'] = $this->size($path);
}
if (! array_key_exists('Content-Disposition', $headers)) {
$filename = $name ?? basename($path);
$disposition = $response->headers->makeDisposition(
$disposition, $filename, $this->fallbackName($filename)
);
$headers['Content-Disposition'] = $disposition;
}
$response->headers->replace($headers);
$response->setCallback(function () use ($path) {
$stream = $this->readStream($path);
fpassthru($stream);
fclose($stream);
});
return $response;
}
/**
* Create a streamed download response for a given file.
*
* @param string $path
* @param string|null $name
* @return \Symfony\Component\HttpFoundation\StreamedResponse
*/
public function download($path, $name = null, array $headers = [])
{
return $this->response($path, $name, $headers, 'attachment');
}
/**
* Convert the string to ASCII characters that are equivalent to the given name.
*
* @param string $name
* @return string
*/
protected function fallbackName($name)
{
return str_replace('%', '', Str::ascii($name));
}
/**
* Write the contents of a file.
*
* @param string $path
* @param string|resource $contents
* @param array $options
* @return bool
* @param \Psr\Http\Message\StreamInterface|\Illuminate\Http\File|\Illuminate\Http\UploadedFile|string|resource $contents
* @param mixed $options
* @return string|bool
*/
public function put($path, $contents, $options = [])
{
if (is_string($options)) {
$options = ['visibility' => $options];
}
$options = is_string($options)
? ['visibility' => $options]
: (array) $options;
// If the given contents is actually a file or uploaded file instance than we will
// automatically store the file using a stream. This provides a convenient path
@@ -114,21 +349,37 @@ class FilesystemAdapter implements FilesystemContract, CloudFilesystemContract
return $this->putFile($path, $contents, $options);
}
return is_resource($contents)
? $this->driver->putStream($path, $contents, $options)
: $this->driver->put($path, $contents, $options);
try {
if ($contents instanceof StreamInterface) {
$this->driver->writeStream($path, $contents->detach(), $options);
return true;
}
is_resource($contents)
? $this->driver->writeStream($path, $contents, $options)
: $this->driver->write($path, $contents, $options);
} catch (UnableToWriteFile|UnableToSetVisibility $e) {
throw_if($this->throwsExceptions(), $e);
return false;
}
return true;
}
/**
* Store the uploaded file on the disk.
*
* @param string $path
* @param \Illuminate\Http\File|\Illuminate\Http\UploadedFile $file
* @param array $options
* @param \Illuminate\Http\File|\Illuminate\Http\UploadedFile|string $file
* @param mixed $options
* @return string|false
*/
public function putFile($path, $file, $options = [])
{
$file = is_string($file) ? new File($file) : $file;
return $this->putFileAs($path, $file, $file->hashName(), $options);
}
@@ -136,14 +387,14 @@ class FilesystemAdapter implements FilesystemContract, CloudFilesystemContract
* Store the uploaded file on the disk with a given name.
*
* @param string $path
* @param \Illuminate\Http\File|\Illuminate\Http\UploadedFile $file
* @param \Illuminate\Http\File|\Illuminate\Http\UploadedFile|string $file
* @param string $name
* @param array $options
* @param mixed $options
* @return string|false
*/
public function putFileAs($path, $file, $name, $options = [])
{
$stream = fopen($file->getRealPath(), 'r+');
$stream = fopen(is_string($file) ? $file : $file->getRealPath(), 'r');
// Next, we will format the path of the file and store the file using a stream since
// they provide better performance than alternatives. Once we write the file this
@@ -167,7 +418,7 @@ class FilesystemAdapter implements FilesystemContract, CloudFilesystemContract
*/
public function getVisibility($path)
{
if ($this->driver->getVisibility($path) == AdapterInterface::VISIBILITY_PUBLIC) {
if ($this->driver->visibility($path) == Visibility::PUBLIC) {
return FilesystemContract::VISIBILITY_PUBLIC;
}
@@ -179,11 +430,19 @@ class FilesystemAdapter implements FilesystemContract, CloudFilesystemContract
*
* @param string $path
* @param string $visibility
* @return void
* @return bool
*/
public function setVisibility($path, $visibility)
{
return $this->driver->setVisibility($path, $this->parseVisibility($visibility));
try {
$this->driver->setVisibility($path, $this->parseVisibility($visibility));
} catch (UnableToSetVisibility $e) {
throw_if($this->throwsExceptions(), $e);
return false;
}
return true;
}
/**
@@ -192,11 +451,11 @@ class FilesystemAdapter implements FilesystemContract, CloudFilesystemContract
* @param string $path
* @param string $data
* @param string $separator
* @return int
* @return bool
*/
public function prepend($path, $data, $separator = PHP_EOL)
{
if ($this->exists($path)) {
if ($this->fileExists($path)) {
return $this->put($path, $data.$separator.$this->get($path));
}
@@ -209,11 +468,11 @@ class FilesystemAdapter implements FilesystemContract, CloudFilesystemContract
* @param string $path
* @param string $data
* @param string $separator
* @return int
* @return bool
*/
public function append($path, $data, $separator = PHP_EOL)
{
if ($this->exists($path)) {
if ($this->fileExists($path)) {
return $this->put($path, $this->get($path).$separator.$data);
}
@@ -234,10 +493,10 @@ class FilesystemAdapter implements FilesystemContract, CloudFilesystemContract
foreach ($paths as $path) {
try {
if (! $this->driver->delete($path)) {
$success = false;
}
} catch (FileNotFoundException $e) {
$this->driver->delete($path);
} catch (UnableToDeleteFile $e) {
throw_if($this->throwsExceptions(), $e);
$success = false;
}
}
@@ -254,7 +513,15 @@ class FilesystemAdapter implements FilesystemContract, CloudFilesystemContract
*/
public function copy($from, $to)
{
return $this->driver->copy($from, $to);
try {
$this->driver->copy($from, $to);
} catch (UnableToCopyFile $e) {
throw_if($this->throwsExceptions(), $e);
return false;
}
return true;
}
/**
@@ -266,7 +533,15 @@ class FilesystemAdapter implements FilesystemContract, CloudFilesystemContract
*/
public function move($from, $to)
{
return $this->driver->rename($from, $to);
try {
$this->driver->move($from, $to);
} catch (UnableToMoveFile $e) {
throw_if($this->throwsExceptions(), $e);
return false;
}
return true;
}
/**
@@ -277,7 +552,25 @@ class FilesystemAdapter implements FilesystemContract, CloudFilesystemContract
*/
public function size($path)
{
return $this->driver->getSize($path);
return $this->driver->fileSize($path);
}
/**
* Get the checksum for a file.
*
* @return string|false
*
* @throws UnableToProvideChecksum
*/
public function checksum(string $path, array $options = [])
{
try {
return $this->driver->checksum($path, $options);
} catch (UnableToProvideChecksum $e) {
throw_if($this->throwsExceptions(), $e);
return false;
}
}
/**
@@ -288,7 +581,13 @@ class FilesystemAdapter implements FilesystemContract, CloudFilesystemContract
*/
public function mimeType($path)
{
return $this->driver->getMimetype($path);
try {
return $this->driver->mimeType($path);
} catch (UnableToRetrieveMetadata $e) {
throw_if($this->throwsExceptions(), $e);
}
return false;
}
/**
@@ -299,7 +598,35 @@ class FilesystemAdapter implements FilesystemContract, CloudFilesystemContract
*/
public function lastModified($path)
{
return $this->driver->getTimestamp($path);
return $this->driver->lastModified($path);
}
/**
* {@inheritdoc}
*/
public function readStream($path)
{
try {
return $this->driver->readStream($path);
} catch (UnableToReadFile $e) {
throw_if($this->throwsExceptions(), $e);
}
}
/**
* {@inheritdoc}
*/
public function writeStream($path, $resource, array $options = [])
{
try {
$this->driver->writeStream($path, $resource, $options);
} catch (UnableToWriteFile|UnableToSetVisibility $e) {
throw_if($this->throwsExceptions(), $e);
return false;
}
return true;
}
/**
@@ -307,15 +634,23 @@ class FilesystemAdapter implements FilesystemContract, CloudFilesystemContract
*
* @param string $path
* @return string
*
* @throws \RuntimeException
*/
public function url($path)
{
$adapter = $this->driver->getAdapter();
if (isset($this->config['prefix'])) {
$path = $this->concatPathToUrl($this->config['prefix'], $path);
}
$adapter = $this->adapter;
if (method_exists($adapter, 'getUrl')) {
return $adapter->getUrl($path);
} elseif ($adapter instanceof AwsS3Adapter) {
return $this->getAwsUrl($adapter, $path);
} elseif (method_exists($this->driver, 'getUrl')) {
return $this->driver->getUrl($path);
} elseif ($adapter instanceof FtpAdapter || $adapter instanceof SftpAdapter) {
return $this->getFtpUrl($path);
} elseif ($adapter instanceof LocalAdapter) {
return $this->getLocalUrl($path);
} else {
@@ -326,15 +661,14 @@ class FilesystemAdapter implements FilesystemContract, CloudFilesystemContract
/**
* Get the URL for the file at the given path.
*
* @param \League\Flysystem\AwsS3v3\AwsS3Adapter $adapter
* @param string $path
* @return string
*/
protected function getAwsUrl($adapter, $path)
protected function getFtpUrl($path)
{
return $adapter->getClient()->getObjectUrl(
$adapter->getBucket(), $adapter->getPathPrefix().$path
);
return isset($this->config['url'])
? $this->concatPathToUrl($this->config['url'], $path)
: $path;
}
/**
@@ -345,13 +679,11 @@ class FilesystemAdapter implements FilesystemContract, CloudFilesystemContract
*/
protected function getLocalUrl($path)
{
$config = $this->driver->getConfig();
// If an explicit base URL has been set on the disk configuration then we will use
// it as the base URL instead of the default path. This allows the developer to
// have full control over the base path for this filesystem's generated URLs.
if ($config->has('url')) {
return rtrim($config->get('url'), '/').'/'.ltrim($path, '/');
if (isset($this->config['url'])) {
return $this->concatPathToUrl($this->config['url'], $path);
}
$path = '/storage/'.$path;
@@ -359,11 +691,75 @@ class FilesystemAdapter implements FilesystemContract, CloudFilesystemContract
// If the path contains "storage/public", it probably means the developer is using
// the default disk to generate the path instead of the "public" disk like they
// are really supposed to use. We will remove the public from this path here.
if (Str::contains($path, '/storage/public/')) {
if (str_contains($path, '/storage/public/')) {
return Str::replaceFirst('/public/', '/', $path);
} else {
return $path;
}
return $path;
}
/**
* Determine if temporary URLs can be generated.
*
* @return bool
*/
public function providesTemporaryUrls()
{
return method_exists($this->adapter, 'getTemporaryUrl') || isset($this->temporaryUrlCallback);
}
/**
* Get a temporary URL for the file at the given path.
*
* @param string $path
* @param \DateTimeInterface $expiration
* @param array $options
* @return string
*
* @throws \RuntimeException
*/
public function temporaryUrl($path, $expiration, array $options = [])
{
if (method_exists($this->adapter, 'getTemporaryUrl')) {
return $this->adapter->getTemporaryUrl($path, $expiration, $options);
}
if ($this->temporaryUrlCallback) {
return $this->temporaryUrlCallback->bindTo($this, static::class)(
$path, $expiration, $options
);
}
throw new RuntimeException('This driver does not support creating temporary URLs.');
}
/**
* Concatenate a path to a URL.
*
* @param string $url
* @param string $path
* @return string
*/
protected function concatPathToUrl($url, $path)
{
return rtrim($url, '/').'/'.ltrim($path, '/');
}
/**
* Replace the scheme, host and port of the given UriInterface with values from the given URL.
*
* @param \Psr\Http\Message\UriInterface $uri
* @param string $url
* @return \Psr\Http\Message\UriInterface
*/
protected function replaceBaseUrl($uri, $url)
{
$parsed = parse_url($url);
return $uri
->withScheme($parsed['scheme'])
->withHost($parsed['host'])
->withPort($parsed['port'] ?? null);
}
/**
@@ -375,9 +771,15 @@ class FilesystemAdapter implements FilesystemContract, CloudFilesystemContract
*/
public function files($directory = null, $recursive = false)
{
$contents = $this->driver->listContents($directory, $recursive);
return $this->filterContentsByType($contents, 'file');
return $this->driver->listContents($directory ?? '', $recursive)
->filter(function (StorageAttributes $attributes) {
return $attributes->isFile();
})
->sortByPath()
->map(function (StorageAttributes $attributes) {
return $attributes->path();
})
->toArray();
}
/**
@@ -400,13 +802,18 @@ class FilesystemAdapter implements FilesystemContract, CloudFilesystemContract
*/
public function directories($directory = null, $recursive = false)
{
$contents = $this->driver->listContents($directory, $recursive);
return $this->filterContentsByType($contents, 'dir');
return $this->driver->listContents($directory ?? '', $recursive)
->filter(function (StorageAttributes $attributes) {
return $attributes->isDir();
})
->map(function (StorageAttributes $attributes) {
return $attributes->path();
})
->toArray();
}
/**
* Get all (recursive) of the directories within a given directory.
* Get all the directories within a given directory (recursive).
*
* @param string|null $directory
* @return array
@@ -424,7 +831,15 @@ class FilesystemAdapter implements FilesystemContract, CloudFilesystemContract
*/
public function makeDirectory($path)
{
return $this->driver->createDir($path);
try {
$this->driver->createDirectory($path);
} catch (UnableToCreateDirectory|UnableToSetVisibility $e) {
throw_if($this->throwsExceptions(), $e);
return false;
}
return true;
}
/**
@@ -435,13 +850,21 @@ class FilesystemAdapter implements FilesystemContract, CloudFilesystemContract
*/
public function deleteDirectory($directory)
{
return $this->driver->deleteDir($directory);
try {
$this->driver->deleteDirectory($directory);
} catch (UnableToDeleteDirectory $e) {
throw_if($this->throwsExceptions(), $e);
return false;
}
return true;
}
/**
* Get the Flysystem driver.
*
* @return \League\Flysystem\FilesystemInterface
* @return \League\Flysystem\FilesystemOperator
*/
public function getDriver()
{
@@ -449,19 +872,23 @@ class FilesystemAdapter implements FilesystemContract, CloudFilesystemContract
}
/**
* Filter directory contents by type.
* Get the Flysystem adapter.
*
* @return \League\Flysystem\FilesystemAdapter
*/
public function getAdapter()
{
return $this->adapter;
}
/**
* Get the configuration values.
*
* @param array $contents
* @param string $type
* @return array
*/
protected function filterContentsByType($contents, $type)
public function getConfig()
{
return Collection::make($contents)
->where('type', $type)
->pluck('path')
->values()
->all();
return $this->config;
}
/**
@@ -478,14 +905,32 @@ class FilesystemAdapter implements FilesystemContract, CloudFilesystemContract
return;
}
switch ($visibility) {
case FilesystemContract::VISIBILITY_PUBLIC:
return AdapterInterface::VISIBILITY_PUBLIC;
case FilesystemContract::VISIBILITY_PRIVATE:
return AdapterInterface::VISIBILITY_PRIVATE;
}
return match ($visibility) {
FilesystemContract::VISIBILITY_PUBLIC => Visibility::PUBLIC,
FilesystemContract::VISIBILITY_PRIVATE => Visibility::PRIVATE,
default => throw new InvalidArgumentException("Unknown visibility: {$visibility}."),
};
}
throw new InvalidArgumentException('Unknown visibility: '.$visibility);
/**
* Define a custom temporary URL builder callback.
*
* @param \Closure $callback
* @return void
*/
public function buildTemporaryUrlsUsing(Closure $callback)
{
$this->temporaryUrlCallback = $callback;
}
/**
* Determine if Flysystem exceptions should be thrown.
*
* @return bool
*/
protected function throwsExceptions(): bool
{
return (bool) ($this->config['throw'] ?? false);
}
/**
@@ -499,6 +944,10 @@ class FilesystemAdapter implements FilesystemContract, CloudFilesystemContract
*/
public function __call($method, array $parameters)
{
return call_user_func_array([$this->driver, $method], $parameters);
if (static::hasMacro($method)) {
return $this->macroCall($method, $parameters);
}
return $this->driver->{$method}(...$parameters);
}
}

View File

@@ -2,20 +2,28 @@
namespace Illuminate\Filesystem;
use Closure;
use Aws\S3\S3Client;
use OpenCloud\Rackspace;
use Closure;
use Illuminate\Contracts\Filesystem\Factory as FactoryContract;
use Illuminate\Support\Arr;
use InvalidArgumentException;
use League\Flysystem\AdapterInterface;
use League\Flysystem\FilesystemInterface;
use League\Flysystem\AwsS3V3\AwsS3V3Adapter as S3Adapter;
use League\Flysystem\AwsS3V3\PortableVisibilityConverter as AwsS3PortableVisibilityConverter;
use League\Flysystem\Filesystem as Flysystem;
use League\Flysystem\Adapter\Ftp as FtpAdapter;
use League\Flysystem\Rackspace\RackspaceAdapter;
use League\Flysystem\Adapter\Local as LocalAdapter;
use League\Flysystem\AwsS3v3\AwsS3Adapter as S3Adapter;
use Illuminate\Contracts\Filesystem\Factory as FactoryContract;
use League\Flysystem\FilesystemAdapter as FlysystemAdapter;
use League\Flysystem\Ftp\FtpAdapter;
use League\Flysystem\Ftp\FtpConnectionOptions;
use League\Flysystem\Local\LocalFilesystemAdapter as LocalAdapter;
use League\Flysystem\PathPrefixing\PathPrefixedAdapter;
use League\Flysystem\PhpseclibV3\SftpAdapter;
use League\Flysystem\PhpseclibV3\SftpConnectionProvider;
use League\Flysystem\ReadOnly\ReadOnlyFilesystemAdapter;
use League\Flysystem\UnixVisibility\PortableVisibilityConverter;
use League\Flysystem\Visibility;
/**
* @mixin \Illuminate\Contracts\Filesystem\Filesystem
*/
class FilesystemManager implements FactoryContract
{
/**
@@ -53,7 +61,7 @@ class FilesystemManager implements FactoryContract
/**
* Get a filesystem instance.
*
* @param string $name
* @param string|null $name
* @return \Illuminate\Contracts\Filesystem\Filesystem
*/
public function drive($name = null)
@@ -64,7 +72,7 @@ class FilesystemManager implements FactoryContract
/**
* Get a filesystem instance.
*
* @param string $name
* @param string|null $name
* @return \Illuminate\Contracts\Filesystem\Filesystem
*/
public function disk($name = null)
@@ -86,6 +94,20 @@ class FilesystemManager implements FactoryContract
return $this->disks[$name] = $this->get($name);
}
/**
* Build an on-demand disk.
*
* @param string|array $config
* @return \Illuminate\Contracts\Filesystem\Filesystem
*/
public function build($config)
{
return $this->resolve('ondemand', is_array($config) ? $config : [
'driver' => 'local',
'root' => $config,
]);
}
/**
* Attempt to get the disk from the local cache.
*
@@ -94,32 +116,39 @@ class FilesystemManager implements FactoryContract
*/
protected function get($name)
{
return isset($this->disks[$name]) ? $this->disks[$name] : $this->resolve($name);
return $this->disks[$name] ?? $this->resolve($name);
}
/**
* Resolve the given disk.
*
* @param string $name
* @param array|null $config
* @return \Illuminate\Contracts\Filesystem\Filesystem
*
* @throws \InvalidArgumentException
*/
protected function resolve($name)
protected function resolve($name, $config = null)
{
$config = $this->getConfig($name);
$config ??= $this->getConfig($name);
if (isset($this->customCreators[$config['driver']])) {
if (empty($config['driver'])) {
throw new InvalidArgumentException("Disk [{$name}] does not have a configured driver.");
}
$name = $config['driver'];
if (isset($this->customCreators[$name])) {
return $this->callCustomCreator($config);
}
$driverMethod = 'create'.ucfirst($config['driver']).'Driver';
$driverMethod = 'create'.ucfirst($name).'Driver';
if (method_exists($this, $driverMethod)) {
return $this->{$driverMethod}($config);
} else {
throw new InvalidArgumentException("Driver [{$config['driver']}] is not supported.");
if (! method_exists($this, $driverMethod)) {
throw new InvalidArgumentException("Driver [{$name}] is not supported.");
}
return $this->{$driverMethod}($config);
}
/**
@@ -130,13 +159,7 @@ class FilesystemManager implements FactoryContract
*/
protected function callCustomCreator(array $config)
{
$driver = $this->customCreators[$config['driver']]($this->app, $config);
if ($driver instanceof FilesystemInterface) {
return $this->adapt($driver);
}
return $driver;
return $this->customCreators[$config['driver']]($this->app, $config);
}
/**
@@ -147,15 +170,20 @@ class FilesystemManager implements FactoryContract
*/
public function createLocalDriver(array $config)
{
$permissions = isset($config['permissions']) ? $config['permissions'] : [];
$visibility = PortableVisibilityConverter::fromArray(
$config['permissions'] ?? [],
$config['directory_visibility'] ?? $config['visibility'] ?? Visibility::PRIVATE
);
$links = Arr::get($config, 'links') === 'skip'
$links = ($config['links'] ?? null) === 'skip'
? LocalAdapter::SKIP_LINKS
: LocalAdapter::DISALLOW_LINKS;
return $this->adapt($this->createFlysystem(new LocalAdapter(
$config['root'], LOCK_EX, $links, $permissions
), $config));
$adapter = new LocalAdapter(
$config['root'], $visibility, $config['lock'] ?? LOCK_EX, $links
);
return new FilesystemAdapter($this->createFlysystem($adapter, $config), $adapter, $config);
}
/**
@@ -166,13 +194,34 @@ class FilesystemManager implements FactoryContract
*/
public function createFtpDriver(array $config)
{
$ftpConfig = Arr::only($config, [
'host', 'username', 'password', 'port', 'root', 'passive', 'ssl', 'timeout',
]);
if (! isset($config['root'])) {
$config['root'] = '';
}
return $this->adapt($this->createFlysystem(
new FtpAdapter($ftpConfig), $config
));
$adapter = new FtpAdapter(FtpConnectionOptions::fromArray($config));
return new FilesystemAdapter($this->createFlysystem($adapter, $config), $adapter, $config);
}
/**
* Create an instance of the sftp driver.
*
* @param array $config
* @return \Illuminate\Contracts\Filesystem\Filesystem
*/
public function createSftpDriver(array $config)
{
$provider = SftpConnectionProvider::fromArray($config);
$root = $config['root'] ?? '/';
$visibility = PortableVisibilityConverter::fromArray(
$config['permissions'] ?? []
);
$adapter = new SftpAdapter($provider, $root, $visibility);
return new FilesystemAdapter($this->createFlysystem($adapter, $config), $adapter, $config);
}
/**
@@ -185,13 +234,21 @@ class FilesystemManager implements FactoryContract
{
$s3Config = $this->formatS3Config($config);
$root = isset($s3Config['root']) ? $s3Config['root'] : null;
$root = (string) ($s3Config['root'] ?? '');
$options = isset($config['options']) ? $config['options'] : [];
$visibility = new AwsS3PortableVisibilityConverter(
$config['visibility'] ?? Visibility::PUBLIC
);
return $this->adapt($this->createFlysystem(
new S3Adapter(new S3Client($s3Config), $s3Config['bucket'], $root, $options), $config
));
$streamReads = $s3Config['stream_reads'] ?? false;
$client = new S3Client($s3Config);
$adapter = new S3Adapter($client, $s3Config['bucket'], $root, $visibility, null, $config['options'] ?? [], $streamReads);
return new AwsS3V3Adapter(
$this->createFlysystem($adapter, $config), $adapter, $s3Config, $client
);
}
/**
@@ -204,71 +261,57 @@ class FilesystemManager implements FactoryContract
{
$config += ['version' => 'latest'];
if ($config['key'] && $config['secret']) {
$config['credentials'] = Arr::only($config, ['key', 'secret']);
if (! empty($config['key']) && ! empty($config['secret'])) {
$config['credentials'] = Arr::only($config, ['key', 'secret', 'token']);
}
return $config;
return Arr::except($config, ['token']);
}
/**
* Create an instance of the Rackspace driver.
* Create a scoped driver.
*
* @param array $config
* @return \Illuminate\Contracts\Filesystem\Cloud
* @return \Illuminate\Contracts\Filesystem\Filesystem
*/
public function createRackspaceDriver(array $config)
public function createScopedDriver(array $config)
{
$client = new Rackspace($config['endpoint'], [
'username' => $config['username'], 'apiKey' => $config['key'],
]);
if (empty($config['disk'])) {
throw new InvalidArgumentException('Scoped disk is missing "disk" configuration option.');
} elseif (empty($config['prefix'])) {
throw new InvalidArgumentException('Scoped disk is missing "prefix" configuration option.');
}
$root = isset($config['root']) ? $config['root'] : null;
return $this->adapt($this->createFlysystem(
new RackspaceAdapter($this->getRackspaceContainer($client, $config), $root), $config
return $this->build(tap(
$this->getConfig($config['disk']),
fn (&$parent) => $parent['prefix'] = $config['prefix']
));
}
/**
* Get the Rackspace Cloud Files container.
*
* @param \OpenCloud\Rackspace $client
* @param array $config
* @return \OpenCloud\ObjectStore\Resource\Container
*/
protected function getRackspaceContainer(Rackspace $client, array $config)
{
$urlType = Arr::get($config, 'url_type');
$store = $client->objectStoreService('cloudFiles', $config['region'], $urlType);
return $store->getContainer($config['container']);
}
/**
* Create a Flysystem instance with the given adapter.
*
* @param \League\Flysystem\AdapterInterface $adapter
* @param \League\Flysystem\FilesystemAdapter $adapter
* @param array $config
* @return \League\Flysystem\FlysystemInterface
* @return \League\Flysystem\FilesystemOperator
*/
protected function createFlysystem(AdapterInterface $adapter, array $config)
protected function createFlysystem(FlysystemAdapter $adapter, array $config)
{
$config = Arr::only($config, ['visibility', 'disable_asserts', 'url']);
if ($config['read-only'] ?? false === true) {
$adapter = new ReadOnlyFilesystemAdapter($adapter);
}
return new Flysystem($adapter, count($config) > 0 ? $config : null);
}
if (! empty($config['prefix'])) {
$adapter = new PathPrefixedAdapter($adapter, $config['prefix']);
}
/**
* Adapt the filesystem implementation.
*
* @param \League\Flysystem\FilesystemInterface $filesystem
* @return \Illuminate\Contracts\Filesystem\Filesystem
*/
protected function adapt(FilesystemInterface $filesystem)
{
return new FilesystemAdapter($filesystem);
return new Flysystem($adapter, Arr::only($config, [
'directory_visibility',
'disable_asserts',
'temporary_url',
'url',
'visibility',
]));
}
/**
@@ -276,11 +319,13 @@ class FilesystemManager implements FactoryContract
*
* @param string $name
* @param mixed $disk
* @return void
* @return $this
*/
public function set($name, $disk)
{
$this->disks[$name] = $disk;
return $this;
}
/**
@@ -291,7 +336,7 @@ class FilesystemManager implements FactoryContract
*/
protected function getConfig($name)
{
return $this->app['config']["filesystems.disks.{$name}"];
return $this->app['config']["filesystems.disks.{$name}"] ?: [];
}
/**
@@ -311,13 +356,41 @@ class FilesystemManager implements FactoryContract
*/
public function getDefaultCloudDriver()
{
return $this->app['config']['filesystems.cloud'];
return $this->app['config']['filesystems.cloud'] ?? 's3';
}
/**
* Unset the given disk instances.
*
* @param array|string $disk
* @return $this
*/
public function forgetDisk($disk)
{
foreach ((array) $disk as $diskName) {
unset($this->disks[$diskName]);
}
return $this;
}
/**
* Disconnect the given disk and remove from local cache.
*
* @param string|null $name
* @return void
*/
public function purge($name = null)
{
$name ??= $this->getDefaultDriver();
unset($this->disks[$name]);
}
/**
* Register a custom driver creator Closure.
*
* @param string $driver
* @param string $driver
* @param \Closure $callback
* @return $this
*/
@@ -328,11 +401,24 @@ class FilesystemManager implements FactoryContract
return $this;
}
/**
* Set the application instance used by the manager.
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @return $this
*/
public function setApplication($app)
{
$this->app = $app;
return $this;
}
/**
* Dynamically call the default driver instance.
*
* @param string $method
* @param array $parameters
* @param array $parameters
* @return mixed
*/
public function __call($method, $parameters)

View File

@@ -39,12 +39,12 @@ class FilesystemServiceProvider extends ServiceProvider
{
$this->registerManager();
$this->app->singleton('filesystem.disk', function () {
return $this->app['filesystem']->disk($this->getDefaultDriver());
$this->app->singleton('filesystem.disk', function ($app) {
return $app['filesystem']->disk($this->getDefaultDriver());
});
$this->app->singleton('filesystem.cloud', function () {
return $this->app['filesystem']->disk($this->getCloudDriver());
$this->app->singleton('filesystem.cloud', function ($app) {
return $app['filesystem']->disk($this->getCloudDriver());
});
}
@@ -55,8 +55,8 @@ class FilesystemServiceProvider extends ServiceProvider
*/
protected function registerManager()
{
$this->app->singleton('filesystem', function () {
return new FilesystemManager($this->app);
$this->app->singleton('filesystem', function ($app) {
return new FilesystemManager($app);
});
}

View File

@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) Taylor Otwell
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@@ -0,0 +1,194 @@
<?php
namespace Illuminate\Filesystem;
use Exception;
use Illuminate\Contracts\Filesystem\LockTimeoutException;
class LockableFile
{
/**
* The file resource.
*
* @var resource
*/
protected $handle;
/**
* The file path.
*
* @var string
*/
protected $path;
/**
* Indicates if the file is locked.
*
* @var bool
*/
protected $isLocked = false;
/**
* Create a new File instance.
*
* @param string $path
* @param string $mode
* @return void
*/
public function __construct($path, $mode)
{
$this->path = $path;
$this->ensureDirectoryExists($path);
$this->createResource($path, $mode);
}
/**
* Create the file's directory if necessary.
*
* @param string $path
* @return void
*/
protected function ensureDirectoryExists($path)
{
if (! file_exists(dirname($path))) {
@mkdir(dirname($path), 0777, true);
}
}
/**
* Create the file resource.
*
* @param string $path
* @param string $mode
* @return void
*
* @throws \Exception
*/
protected function createResource($path, $mode)
{
$this->handle = @fopen($path, $mode);
if (! $this->handle) {
throw new Exception('Unable to create lockable file: '.$path.'. Please ensure you have permission to create files in this location.');
}
}
/**
* Read the file contents.
*
* @param int|null $length
* @return string
*/
public function read($length = null)
{
clearstatcache(true, $this->path);
return fread($this->handle, $length ?? ($this->size() ?: 1));
}
/**
* Get the file size.
*
* @return int
*/
public function size()
{
return filesize($this->path);
}
/**
* Write to the file.
*
* @param string $contents
* @return $this
*/
public function write($contents)
{
fwrite($this->handle, $contents);
fflush($this->handle);
return $this;
}
/**
* Truncate the file.
*
* @return $this
*/
public function truncate()
{
rewind($this->handle);
ftruncate($this->handle, 0);
return $this;
}
/**
* Get a shared lock on the file.
*
* @param bool $block
* @return $this
*
* @throws \Illuminate\Contracts\Filesystem\LockTimeoutException
*/
public function getSharedLock($block = false)
{
if (! flock($this->handle, LOCK_SH | ($block ? 0 : LOCK_NB))) {
throw new LockTimeoutException("Unable to acquire file lock at path [{$this->path}].");
}
$this->isLocked = true;
return $this;
}
/**
* Get an exclusive lock on the file.
*
* @param bool $block
* @return bool
*
* @throws \Illuminate\Contracts\Filesystem\LockTimeoutException
*/
public function getExclusiveLock($block = false)
{
if (! flock($this->handle, LOCK_EX | ($block ? 0 : LOCK_NB))) {
throw new LockTimeoutException("Unable to acquire file lock at path [{$this->path}].");
}
$this->isLocked = true;
return $this;
}
/**
* Release the lock on the file.
*
* @return $this
*/
public function releaseLock()
{
flock($this->handle, LOCK_UN);
$this->isLocked = false;
return $this;
}
/**
* Close the file.
*
* @return bool
*/
public function close()
{
if ($this->isLocked) {
$this->releaseLock();
}
return fclose($this->handle);
}
}

View File

@@ -14,10 +14,12 @@
}
],
"require": {
"php": ">=5.6.4",
"illuminate/contracts": "5.4.*",
"illuminate/support": "5.4.*",
"symfony/finder": "~3.2"
"php": "^8.0.2",
"illuminate/collections": "^9.0",
"illuminate/contracts": "^9.0",
"illuminate/macroable": "^9.0",
"illuminate/support": "^9.0",
"symfony/finder": "^6.0"
},
"autoload": {
"psr-4": {
@@ -26,13 +28,19 @@
},
"extra": {
"branch-alias": {
"dev-master": "5.4-dev"
"dev-master": "9.x-dev"
}
},
"suggest": {
"league/flysystem": "Required to use the Flysystem local and FTP drivers (~1.0).",
"league/flysystem-aws-s3-v3": "Required to use the Flysystem S3 driver (~1.0).",
"league/flysystem-rackspace": "Required to use the Flysystem Rackspace driver (~1.0)."
"ext-ftp": "Required to use the Flysystem FTP driver.",
"illuminate/http": "Required for handling uploaded files (^7.0).",
"league/flysystem": "Required to use the Flysystem local driver (^3.0.16).",
"league/flysystem-aws-s3-v3": "Required to use the Flysystem S3 driver (^3.0).",
"league/flysystem-ftp": "Required to use the Flysystem FTP driver (^3.0).",
"league/flysystem-sftp-v3": "Required to use the Flysystem SFTP driver (^3.0).",
"psr/http-message": "Required to allow Storage::put to accept a StreamInterface (^1.0).",
"symfony/filesystem": "Required to enable support for relative symbolic links (^6.0).",
"symfony/mime": "Required to enable support for guessing extensions (^6.0)."
},
"config": {
"sort-packages": true