forked from thephpleague/flysystem-bundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPullCommand.php
More file actions
62 lines (51 loc) · 2.08 KB
/
PullCommand.php
File metadata and controls
62 lines (51 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
/*
* This file is part of the flysystem-bundle project.
*
* (c) Titouan Galopin <galopintitouan@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\FlysystemBundle\Command;
use League\Flysystem\FilesystemOperator;
use Symfony\Component\Console\Attribute\AsCommand;
#[AsCommand(name: 'flysystem:pull', description: 'Pull a file from a configured Flysystem storage to the local filesystem.')]
final class PullCommand extends AbstractTransferCommand
{
protected function normalizeDestination(string $source, string $destination): string
{
if (!is_dir($destination)) {
return $destination;
}
return rtrim($destination, '/\\').DIRECTORY_SEPARATOR.basename($source);
}
protected function transfer(FilesystemOperator $storage, string $source, string $destination): void
{
$directory = dirname($destination);
if ('.' !== $directory && !is_dir($directory) && !mkdir($directory, 0777, true) && !is_dir($directory)) {
throw new \RuntimeException(sprintf('Unable to create the destination directory "%s".', $directory));
}
$resource = $storage->readStream($source);
if (!is_resource($resource)) {
throw new \RuntimeException(sprintf('Unable to read the source file "%s" from storage.', $source));
}
$local = fopen($destination, 'wb');
if (false === $local) {
if (is_resource($resource)) {
fclose($resource);
}
throw new \RuntimeException(sprintf('Unable to open the destination file "%s" for writing.', $destination));
}
try {
stream_copy_to_stream($resource, $local);
} finally {
fclose($resource);
fclose($local);
}
}
protected function createSuccessMessage(string $storageName, string $source, string $destination): string
{
return sprintf('Pulled "%s" from storage "%s" to "%s".', $source, $storageName, $destination);
}
}