1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Support; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Filesystem\Filesystem; |
7
|
|
|
use League\Flysystem\Adapter\Local as LocalAdapter; |
8
|
|
|
use League\Flysystem\Filesystem as Flysystem; |
9
|
|
|
use League\Flysystem\MountManager; |
10
|
|
|
|
11
|
|
|
class FilePublisher |
12
|
|
|
{ |
13
|
|
|
/** @var Filesystem */ |
14
|
|
|
protected $files; |
15
|
|
|
|
16
|
|
|
/** @var bool Should we force the publishing of the files? */ |
17
|
|
|
protected $force = false; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* FilePublisher constructor. |
21
|
|
|
* |
22
|
|
|
* @param Filesystem $files |
23
|
|
|
*/ |
24
|
210 |
|
public function __construct(Filesystem $files) |
25
|
|
|
{ |
26
|
210 |
|
$this->files = $files; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Get the Filesystem. |
31
|
|
|
* |
32
|
|
|
* @return Filesystem |
33
|
|
|
*/ |
34
|
210 |
|
public function getFilesystem() |
35
|
|
|
{ |
36
|
210 |
|
return $this->files; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Set the publisher to force publish the files, or not. |
41
|
|
|
* |
42
|
|
|
* @param bool $bool |
43
|
|
|
* |
44
|
|
|
* @return $this |
45
|
|
|
*/ |
46
|
|
|
public function force($bool = true) |
47
|
|
|
{ |
48
|
|
|
$this->force = (bool) $bool; |
49
|
|
|
|
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Publish the given item from and to the given location. |
55
|
|
|
* |
56
|
|
|
* @param string $from |
57
|
|
|
* @param string $to |
58
|
|
|
* |
59
|
|
|
* @throws Exception |
60
|
|
|
* |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
210 |
|
public function publish($from, $to) |
64
|
|
|
{ |
65
|
210 |
|
if ($this->files->isFile($from)) { |
66
|
|
|
$this->publishFile($from, $to); |
67
|
210 |
|
} elseif ($this->files->isDirectory($from)) { |
68
|
210 |
|
$this->publishDirectory($from, $to); |
69
|
|
|
} else { |
70
|
|
|
throw new Exception("Can't locate path: <{$from}>"); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Publish the file to the given path. |
76
|
|
|
* |
77
|
|
|
* @param string $from |
78
|
|
|
* @param string $to |
79
|
|
|
* |
80
|
|
|
* @return void |
81
|
|
|
*/ |
82
|
|
|
protected function publishFile($from, $to) |
83
|
|
|
{ |
84
|
|
|
if (!$this->files->exists($to) || $this->force) { |
85
|
|
|
$this->createParentDirectory(dirname($to)); |
86
|
|
|
|
87
|
|
|
$this->files->copy($from, $to); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Publish the directory to the given directory. |
93
|
|
|
* |
94
|
|
|
* @param string $from |
95
|
|
|
* @param string $to |
96
|
|
|
* |
97
|
|
|
* @throws \League\Flysystem\FileNotFoundException |
98
|
|
|
* |
99
|
|
|
* @return void |
100
|
|
|
*/ |
101
|
210 |
|
protected function publishDirectory($from, $to) |
102
|
|
|
{ |
103
|
210 |
|
$this->moveManagedFiles(new MountManager([ |
104
|
210 |
|
'from' => new Flysystem(new LocalAdapter($from)), |
105
|
210 |
|
'to' => new Flysystem(new LocalAdapter($to)), |
106
|
|
|
])); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Move all the files in the given MountManager. |
111
|
|
|
* |
112
|
|
|
* @param \League\Flysystem\MountManager $manager |
113
|
|
|
* |
114
|
|
|
* @throws \League\Flysystem\FileNotFoundException |
115
|
|
|
* |
116
|
|
|
* @return void |
117
|
|
|
*/ |
118
|
210 |
|
protected function moveManagedFiles($manager) |
119
|
|
|
{ |
120
|
210 |
|
foreach ($manager->listContents('from://', true) as $file) { |
121
|
210 |
|
if ($file['type'] === 'file' && (!$manager->has('to://'.$file['path']) || $this->force)) { |
122
|
210 |
|
$manager->put('to://'.$file['path'], (string) $manager->read('from://'.$file['path'])); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Create the directory to house the published files if needed. |
129
|
|
|
* |
130
|
|
|
* @param string $directory |
131
|
|
|
* |
132
|
|
|
* @return void |
133
|
|
|
*/ |
134
|
|
|
protected function createParentDirectory($directory) |
135
|
|
|
{ |
136
|
|
|
if (!$this->files->isDirectory($directory)) { |
137
|
|
|
$this->files->makeDirectory($directory, 0755, true); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|