|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Support; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use Exception; |
|
7
|
|
|
use Illuminate\Filesystem\Filesystem; |
|
8
|
|
|
use League\Flysystem\MountManager; |
|
9
|
|
|
use League\Flysystem\Filesystem as Flysystem; |
|
10
|
|
|
use League\Flysystem\Adapter\Local as LocalAdapter; |
|
11
|
|
|
|
|
12
|
|
|
class FilePublisher |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var Filesystem */ |
|
15
|
|
|
protected $files; |
|
16
|
|
|
|
|
17
|
|
|
protected $force = false; |
|
18
|
|
|
|
|
19
|
54 |
|
public function __construct(Filesystem $files) |
|
20
|
|
|
{ |
|
21
|
54 |
|
$this->files = $files; |
|
22
|
54 |
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @return Filesystem |
|
26
|
|
|
*/ |
|
27
|
54 |
|
public function getFilesystem() |
|
28
|
|
|
{ |
|
29
|
54 |
|
return $this->files; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function force($bool = true) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->force = (bool) $bool; |
|
35
|
|
|
|
|
36
|
|
|
return $this; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Publish the given item from and to the given location. |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $from |
|
43
|
|
|
* @param string $to |
|
44
|
|
|
* @return void |
|
45
|
|
|
* @throws Exception |
|
46
|
|
|
*/ |
|
47
|
54 |
|
public function publish($from, $to) |
|
48
|
|
|
{ |
|
49
|
54 |
|
if ($this->files->isFile($from)) { |
|
50
|
|
|
return $this->publishFile($from, $to); |
|
|
|
|
|
|
51
|
54 |
|
} elseif ($this->files->isDirectory($from)) { |
|
52
|
54 |
|
return $this->publishDirectory($from, $to); |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
throw new Exception("Can't locate path: <{$from}>"); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Publish the file to the given path. |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $from |
|
62
|
|
|
* @param string $to |
|
63
|
|
|
* @return void |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function publishFile($from, $to) |
|
66
|
|
|
{ |
|
67
|
|
|
if (! $this->files->exists($to) || $this->force) { |
|
68
|
|
|
$this->createParentDirectory(dirname($to)); |
|
69
|
|
|
|
|
70
|
|
|
$this->files->copy($from, $to); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Publish the directory to the given directory. |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $from |
|
78
|
|
|
* @param string $to |
|
79
|
|
|
* @return void |
|
80
|
|
|
*/ |
|
81
|
54 |
|
protected function publishDirectory($from, $to) |
|
82
|
|
|
{ |
|
83
|
54 |
|
$this->moveManagedFiles(new MountManager([ |
|
84
|
54 |
|
'from' => new Flysystem(new LocalAdapter($from)), |
|
85
|
54 |
|
'to' => new Flysystem(new LocalAdapter($to)), |
|
86
|
|
|
])); |
|
87
|
54 |
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Move all the files in the given MountManager. |
|
91
|
|
|
* |
|
92
|
|
|
* @param \League\Flysystem\MountManager $manager |
|
93
|
|
|
* @return void |
|
94
|
|
|
*/ |
|
95
|
54 |
|
protected function moveManagedFiles($manager) |
|
96
|
|
|
{ |
|
97
|
54 |
|
foreach ($manager->listContents('from://', true) as $file) { |
|
98
|
54 |
|
if ($file['type'] === 'file' && (! $manager->has('to://'.$file['path']) || $this->force)) { |
|
99
|
54 |
|
$manager->put('to://'.$file['path'], $manager->read('from://'.$file['path'])); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
54 |
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Create the directory to house the published files if needed. |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $directory |
|
108
|
|
|
* @return void |
|
109
|
|
|
*/ |
|
110
|
|
|
protected function createParentDirectory($directory) |
|
111
|
|
|
{ |
|
112
|
|
|
if (! $this->files->isDirectory($directory)) { |
|
113
|
|
|
$this->files->makeDirectory($directory, 0755, true); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.