|
1
|
|
|
<?php |
|
2
|
|
|
/****************************************************************************** |
|
3
|
|
|
* An iterator interface over the Leagues flysystem. |
|
4
|
|
|
* Copyright (c) 2021, 2015 Richard Klees <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* This software is licensed under GPLv3. You should have received |
|
7
|
|
|
* a copy of the along with the code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Lechimp\Flightcontrol; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* The flightcontrol serves an interface over the Leagues flysystem. |
|
14
|
|
|
* |
|
15
|
|
|
* The interface consists of objects for files and directories means |
|
16
|
|
|
* to iterate over the directories. |
|
17
|
|
|
*/ |
|
18
|
|
|
class Flightcontrol |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var \League\Flysystem\Filesystem |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $filesystem; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var bool |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $strict_evaluation; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Initialize the flightcontrol over a flysystem. |
|
32
|
|
|
*/ |
|
33
|
67 |
|
public function __construct( |
|
34
|
|
|
\League\Flysystem\Filesystem $filesystem, |
|
35
|
|
|
bool $strict_evaluation = true |
|
36
|
|
|
) { |
|
37
|
67 |
|
assert(is_bool($strict_evaluation)); |
|
38
|
67 |
|
$this->strict_evaluation = $strict_evaluation; |
|
39
|
67 |
|
$this->filesystem = $filesystem; |
|
40
|
67 |
|
} |
|
41
|
|
|
|
|
42
|
52 |
|
public function filesystem() : \League\Flysystem\Filesystem |
|
43
|
|
|
{ |
|
44
|
52 |
|
return $this->filesystem; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Get an object from the filesystem based on its path. |
|
49
|
|
|
*/ |
|
50
|
65 |
|
public function get(string $path) : FSObject |
|
51
|
|
|
{ |
|
52
|
|
|
// TODO: This does not deal with ~ for home directory. |
|
53
|
|
|
|
|
54
|
65 |
|
if ($this->filesystem->fileExists($path)) { |
|
55
|
49 |
|
return new File($this, $path); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
59 |
|
return new Directory($this, $path); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Get a directory from the filesystem. |
|
63
|
|
|
* |
|
64
|
|
|
* Dependening on the adapter in the underlying flysystem, this might treat |
|
65
|
|
|
* empty directories as if they would not exist (e.g. for ZipArchiveAdapter). |
|
66
|
|
|
*/ |
|
67
|
46 |
|
public function directory(string $path) : ?Directory |
|
68
|
|
|
{ |
|
69
|
46 |
|
return $this->file_or_dir($path, false); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Get a file from the filesystem. |
|
74
|
|
|
*/ |
|
75
|
2 |
|
public function file(string $path) : ?File |
|
76
|
|
|
{ |
|
77
|
2 |
|
return $this->file_or_dir($path, true); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Make a directory when unfolding a directory structure via Directory::unfold. |
|
82
|
|
|
*/ |
|
83
|
5 |
|
public function makeFDirectory(string $name, array $content) : FDirectory |
|
84
|
|
|
{ |
|
85
|
5 |
|
return Directory::makeFDirectory($this, $name, $content); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Make a file when unfolding a directory structure via Directory::unfold. |
|
90
|
|
|
*/ |
|
91
|
5 |
|
public function makeFile(string $name, string $content) : VirtualFile |
|
92
|
|
|
{ |
|
93
|
5 |
|
return Directory::makeFile($this, $name, $content); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
// Helper |
|
97
|
|
|
|
|
98
|
|
|
// Get an object from fs that either is as file or a dir. |
|
99
|
48 |
|
private function file_or_dir(string $path, bool $is_file) : ?FSObject |
|
100
|
|
|
{ |
|
101
|
48 |
|
$obj = $this->get($path); |
|
102
|
48 |
|
if ($obj !== null && $is_file === $obj->isFile()) { |
|
103
|
48 |
|
return $obj; |
|
104
|
|
|
} |
|
105
|
2 |
|
return null; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Create an FDirectory with metadata from some FSObject and some content |
|
110
|
|
|
* that could be lazily produced by some function. |
|
111
|
|
|
*/ |
|
112
|
44 |
|
public function newFDirectory(FSObject $fs_object, \Closure $contents_lazy) : FDirectory |
|
113
|
|
|
{ |
|
114
|
44 |
|
$fdir = new FDirectory($fs_object, $contents_lazy); |
|
115
|
44 |
|
if ($this->strict_evaluation) { |
|
116
|
27 |
|
$fdir->fcontents(); |
|
117
|
|
|
} |
|
118
|
44 |
|
return $fdir; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|