This commit is contained in:
Paolo A
2024-08-13 13:44:16 +00:00
parent 1bbb23088d
commit e796d76612
4001 changed files with 30101 additions and 40075 deletions

View File

@@ -5,8 +5,11 @@ namespace Illuminate\Filesystem;
use ErrorException;
use FilesystemIterator;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Support\LazyCollection;
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;
@@ -87,14 +90,22 @@ 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}.");
@@ -103,12 +114,53 @@ class Filesystem
/**
* 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 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();
}
});
}
/**
@@ -159,6 +211,19 @@ class Filesystem
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.
*
@@ -217,7 +282,9 @@ class Filesystem
foreach ($paths as $path) {
try {
if (! @unlink($path)) {
if (@unlink($path)) {
clearstatcache(false, $path);
} else {
$success = false;
}
} catch (ErrorException $e) {
@@ -270,6 +337,28 @@ class Filesystem
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($relativeTarget, $link);
}
/**
* Extract the file name from a file path.
*
@@ -319,6 +408,8 @@ class Filesystem
*
* @param string $path
* @return string|null
*
* @throws \RuntimeException
*/
public function guessExtension($path)
{

View File

@@ -2,6 +2,7 @@
namespace Illuminate\Filesystem;
use Closure;
use Illuminate\Contracts\Filesystem\Cloud as CloudFilesystemContract;
use Illuminate\Contracts\Filesystem\FileExistsException as ContractFileExistsException;
use Illuminate\Contracts\Filesystem\FileNotFoundException as ContractFileNotFoundException;
@@ -11,6 +12,7 @@ use Illuminate\Http\UploadedFile;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Macroable;
use InvalidArgumentException;
use League\Flysystem\Adapter\Ftp;
use League\Flysystem\Adapter\Local as LocalAdapter;
@@ -20,8 +22,10 @@ use League\Flysystem\Cached\CachedAdapter;
use League\Flysystem\FileExistsException;
use League\Flysystem\FileNotFoundException;
use League\Flysystem\FilesystemInterface;
use League\Flysystem\Sftp\SftpAdapter as Sftp;
use PHPUnit\Framework\Assert as PHPUnit;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UriInterface;
use RuntimeException;
use Symfony\Component\HttpFoundation\StreamedResponse;
@@ -30,6 +34,10 @@ use Symfony\Component\HttpFoundation\StreamedResponse;
*/
class FilesystemAdapter implements CloudFilesystemContract
{
use Macroable {
__call as macroCall;
}
/**
* The Flysystem filesystem implementation.
*
@@ -37,6 +45,13 @@ class FilesystemAdapter implements CloudFilesystemContract
*/
protected $driver;
/**
* The temporary URL builder callback.
*
* @var \Closure|null
*/
protected $temporaryUrlCallback;
/**
* Create a new filesystem adapter instance.
*
@@ -52,16 +67,29 @@ class FilesystemAdapter implements CloudFilesystemContract
* Assert that the given file exists.
*
* @param string|array $path
* @param string|null $content
* @return $this
*/
public function assertExists($path)
public function assertExists($path, $content = null)
{
clearstatcache();
$paths = Arr::wrap($path);
foreach ($paths as $path) {
PHPUnit::assertTrue(
$this->exists($path), "Unable to find a file at path [{$path}]."
);
if (! is_null($content)) {
$actual = $this->get($path);
PHPUnit::assertSame(
$content,
$actual,
"File [{$path}] was found, but content [{$actual}] does not match [{$content}]."
);
}
}
return $this;
@@ -75,6 +103,8 @@ class FilesystemAdapter implements CloudFilesystemContract
*/
public function assertMissing($path)
{
clearstatcache();
$paths = Arr::wrap($path);
foreach ($paths as $path) {
@@ -204,7 +234,7 @@ class FilesystemAdapter implements CloudFilesystemContract
* Write the contents of a file.
*
* @param string $path
* @param string|resource $contents
* @param \Psr\Http\Message\StreamInterface|\Illuminate\Http\File|\Illuminate\Http\UploadedFile|string|resource $contents
* @param mixed $options
* @return bool
*/
@@ -438,7 +468,7 @@ class FilesystemAdapter implements CloudFilesystemContract
return $this->driver->getUrl($path);
} elseif ($adapter instanceof AwsS3Adapter) {
return $this->getAwsUrl($adapter, $path);
} elseif ($adapter instanceof Ftp) {
} elseif ($adapter instanceof Ftp || $adapter instanceof Sftp) {
return $this->getFtpUrl($path);
} elseif ($adapter instanceof LocalAdapter) {
return $this->getLocalUrl($path);
@@ -556,11 +586,19 @@ class FilesystemAdapter implements CloudFilesystemContract
if (method_exists($adapter, 'getTemporaryUrl')) {
return $adapter->getTemporaryUrl($path, $expiration, $options);
} elseif ($adapter instanceof AwsS3Adapter) {
return $this->getAwsTemporaryUrl($adapter, $path, $expiration, $options);
} else {
throw new RuntimeException('This driver does not support creating temporary URLs.');
}
if ($this->temporaryUrlCallback) {
return $this->temporaryUrlCallback->bindTo($this, static::class)(
$path, $expiration, $options
);
}
if ($adapter instanceof AwsS3Adapter) {
return $this->getAwsTemporaryUrl($adapter, $path, $expiration, $options);
}
throw new RuntimeException('This driver does not support creating temporary URLs.');
}
/**
@@ -581,9 +619,18 @@ class FilesystemAdapter implements CloudFilesystemContract
'Key' => $adapter->getPathPrefix().$path,
], $options));
return (string) $client->createPresignedRequest(
$uri = $client->createPresignedRequest(
$command, $expiration
)->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 (! is_null($url = $this->driver->getConfig()->get('temporary_url'))) {
$uri = $this->replaceBaseUrl($uri, $url);
}
return (string) $uri;
}
/**
@@ -598,6 +645,23 @@ class FilesystemAdapter implements CloudFilesystemContract
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);
}
/**
* Get an array of all files in a directory.
*
@@ -607,7 +671,7 @@ class FilesystemAdapter implements CloudFilesystemContract
*/
public function files($directory = null, $recursive = false)
{
$contents = $this->driver->listContents($directory, $recursive);
$contents = $this->driver->listContents($directory ?? '', $recursive);
return $this->filterContentsByType($contents, 'file');
}
@@ -632,7 +696,7 @@ class FilesystemAdapter implements CloudFilesystemContract
*/
public function directories($directory = null, $recursive = false)
{
$contents = $this->driver->listContents($directory, $recursive);
$contents = $this->driver->listContents($directory ?? '', $recursive);
return $this->filterContentsByType($contents, 'dir');
}
@@ -734,6 +798,17 @@ class FilesystemAdapter implements CloudFilesystemContract
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;
}
/**
* Pass dynamic methods call onto Flysystem.
*
@@ -745,6 +820,10 @@ class FilesystemAdapter implements CloudFilesystemContract
*/
public function __call($method, array $parameters)
{
return $this->driver->{$method}(...array_values($parameters));
if (static::hasMacro($method)) {
return $this->macroCall($method, $parameters);
}
return $this->driver->{$method}(...$parameters);
}
}

View File

@@ -90,6 +90,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.
*
@@ -105,13 +119,14 @@ class FilesystemManager implements FactoryContract
* 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 = $config ?? $this->getConfig($name);
if (empty($config['driver'])) {
throw new InvalidArgumentException("Disk [{$name}] does not have a configured driver.");
@@ -125,11 +140,11 @@ class FilesystemManager implements FactoryContract
$driverMethod = 'create'.ucfirst($name).'Driver';
if (method_exists($this, $driverMethod)) {
return $this->{$driverMethod}($config);
} else {
if (! method_exists($this, $driverMethod)) {
throw new InvalidArgumentException("Driver [{$name}] is not supported.");
}
return $this->{$driverMethod}($config);
}
/**
@@ -243,7 +258,7 @@ class FilesystemManager implements FactoryContract
{
$cache = Arr::pull($config, 'cache');
$config = Arr::only($config, ['visibility', 'disable_asserts', 'url']);
$config = Arr::only($config, ['visibility', 'disable_asserts', 'url', 'temporary_url']);
if ($cache) {
$adapter = new CachedAdapter($adapter, $this->createCacheStore($cache));
@@ -326,7 +341,7 @@ class FilesystemManager implements FactoryContract
*/
public function getDefaultCloudDriver()
{
return $this->app['config']['filesystems.cloud'];
return $this->app['config']['filesystems.cloud'] ?? 's3';
}
/**
@@ -344,6 +359,19 @@ class FilesystemManager implements FactoryContract
return $this;
}
/**
* Disconnect the given disk and remove from local cache.
*
* @param string|null $name
* @return void
*/
public function purge($name = null)
{
$name = $name ?? $this->getDefaultDriver();
unset($this->disks[$name]);
}
/**
* Register a custom driver creator Closure.
*
@@ -358,6 +386,19 @@ 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.
*

View File

@@ -14,10 +14,12 @@
}
],
"require": {
"php": "^7.2.5|^8.0",
"illuminate/contracts": "^7.0",
"illuminate/support": "^7.0",
"symfony/finder": "^5.0"
"php": "^7.3|^8.0",
"illuminate/collections": "^8.0",
"illuminate/contracts": "^8.0",
"illuminate/macroable": "^8.0",
"illuminate/support": "^8.0",
"symfony/finder": "^5.4"
},
"autoload": {
"psr-4": {
@@ -26,7 +28,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "7.x-dev"
"dev-master": "8.x-dev"
}
},
"suggest": {
@@ -37,7 +39,8 @@
"league/flysystem-cached-adapter": "Required to use the Flysystem cache (^1.0).",
"league/flysystem-sftp": "Required to use the Flysystem SFTP driver (^1.0).",
"psr/http-message": "Required to allow Storage::put to accept a StreamInterface (^1.0).",
"symfony/mime": "Required to enable support for guessing extensions (^5.0)."
"symfony/filesystem": "Required to enable support for relative symbolic links (^5.4).",
"symfony/mime": "Required to enable support for guessing extensions (^5.4)."
},
"config": {
"sort-packages": true