|
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
|
|
|
abstract class FixedFDirectory /* a */ extends FSObject |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* See documentation of FDirectory. |
|
16
|
|
|
* |
|
17
|
|
|
* @return FDirectory<a> |
|
|
|
|
|
|
18
|
|
|
*/ |
|
19
|
|
|
abstract public function unfix() : FDirectory; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Get an iterator over the content of this directory. |
|
23
|
|
|
*/ |
|
24
|
18 |
|
public function iterateOn() : Iterator |
|
25
|
|
|
{ |
|
26
|
18 |
|
return new DirectoryIterator($this); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Get an recursor over the content of this directory. |
|
31
|
|
|
*/ |
|
32
|
16 |
|
public function recurseOn() : Recursor |
|
33
|
|
|
{ |
|
34
|
16 |
|
return new Recursor($this); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Get an object that can unfold a directory structure in this directory. |
|
39
|
|
|
* |
|
40
|
|
|
* @param mixed $start_value |
|
41
|
|
|
* @throws \LogicException If the directory is not empty. |
|
42
|
|
|
*/ |
|
43
|
6 |
|
public function unfold($start_value) : Unfolder |
|
44
|
|
|
{ |
|
45
|
6 |
|
if (count($this->contents()) > 0) { |
|
46
|
1 |
|
throw new \LogicException("Can't unfold into non-empty directory '" . $this->path() . "'."); |
|
47
|
|
|
} |
|
48
|
5 |
|
return new Unfolder($this, $start_value); |
|
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @inheritdoc |
|
53
|
|
|
*/ |
|
54
|
51 |
|
public function isFile() : bool |
|
55
|
|
|
{ |
|
56
|
51 |
|
return false; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Only regard contents that match the predicate. |
|
61
|
|
|
* |
|
62
|
|
|
* @param \Closure $predicate File|Directory -> bool |
|
63
|
|
|
*/ |
|
64
|
20 |
|
public function filter(\Closure $predicate) : FixedFDirectory |
|
65
|
|
|
{ |
|
66
|
20 |
|
return new GenericFixedFDirectory( |
|
67
|
20 |
|
$this->unfix()->filter($predicate) |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Map over the contents of this directory. |
|
73
|
|
|
* |
|
74
|
|
|
* @param \Closure $trans |
|
75
|
|
|
*/ |
|
76
|
14 |
|
public function map(\Closure $trans) : FixedFDirectory |
|
77
|
|
|
{ |
|
78
|
14 |
|
return new GenericFixedFDirectory( |
|
79
|
14 |
|
$this->unfix()->fmap($trans) |
|
80
|
|
|
); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Map the contents this directory. |
|
85
|
|
|
* |
|
86
|
|
|
* @param \Closure $trans |
|
87
|
|
|
*/ |
|
88
|
|
|
public function outer_map(\Closure $trans) : Directory |
|
89
|
|
|
{ |
|
90
|
|
|
return new GenericFixedFDirectory( |
|
91
|
|
|
$this->unfix()->outer_fmap($trans) |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Fold the contents of this directory with a function. |
|
97
|
|
|
* |
|
98
|
|
|
* Provide a start value that is fed together with the any content |
|
99
|
|
|
* of this directory to the function successively to get a new value. |
|
100
|
|
|
* |
|
101
|
|
|
* @param \Closure $iteration a -> b -> a |
|
102
|
|
|
*/ |
|
103
|
18 |
|
public function fold($start_value, $iteration) : FixedFDirectory |
|
104
|
|
|
{ |
|
105
|
18 |
|
return new GenericFixedFDirectory( |
|
106
|
18 |
|
$this->unfix()->fold($start_value, $iteration) |
|
107
|
|
|
); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Get the the things inside this abstract directory. |
|
112
|
|
|
* |
|
113
|
|
|
* @return mixed |
|
114
|
|
|
*/ |
|
115
|
24 |
|
public function contents() |
|
116
|
|
|
{ |
|
117
|
24 |
|
return $this->unfix()->fcontents(); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* We could also use the catamorphism on this to do recursion, as we |
|
122
|
|
|
* have an unfix and an underlying fmap from the FDirectory. |
|
123
|
|
|
* |
|
124
|
|
|
* Supply a function $trans from File|FDirectory a to a that flattens |
|
125
|
|
|
* (folds) a directory. Will start the directories where only files are |
|
126
|
|
|
* included, folds them and then proceeds upwards. |
|
127
|
|
|
* |
|
128
|
|
|
* The return type should be 'a' (from the function $trans) instead |
|
129
|
|
|
* of mixed, but we can't express that fact correctly in the docstring |
|
130
|
|
|
* typing. |
|
131
|
|
|
* |
|
132
|
|
|
* @param \Closure $trans File|FDirectory a -> a |
|
133
|
|
|
* @return mixed |
|
134
|
|
|
*/ |
|
135
|
16 |
|
public function cata(\Closure $trans) |
|
136
|
|
|
{ |
|
137
|
16 |
|
return $this->recurseOn()->cata($trans); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Fold over all files in this directory and subjacent |
|
142
|
|
|
* directories. |
|
143
|
|
|
* |
|
144
|
|
|
* Start with an initial value of some type and a function from that type |
|
145
|
|
|
* and File to a new value of the type. Will successively feed all files |
|
146
|
|
|
* and the resulting values to that function. |
|
147
|
|
|
* |
|
148
|
|
|
* @param mixed $start_value |
|
149
|
|
|
* @param \Closure $fold_with a -> File -> a |
|
150
|
|
|
* @return Recursor|array |
|
151
|
|
|
*/ |
|
152
|
2 |
|
public function foldFiles($start_value, \Closure $fold_with) |
|
153
|
|
|
{ |
|
154
|
2 |
|
return $this->recurseOn()->foldFiles($start_value, $fold_with); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* As we have an unfix and an underlying fmap from FDirectory, we could |
|
159
|
|
|
* also implement the anamorphism. |
|
160
|
|
|
* |
|
161
|
|
|
* An anamorphism creates a structure from a start value and thus somehow |
|
162
|
|
|
* is the reverse of cata. |
|
163
|
|
|
* |
|
164
|
|
|
* You need to provide a function from a to File or FDirectory a. This |
|
165
|
|
|
* function then is recursively applied on the contents it produces, |
|
166
|
|
|
* starting with the start value. |
|
167
|
|
|
* |
|
168
|
|
|
* @param \Closure $unfolder a -> File|FDirectory a |
|
169
|
|
|
* @param mixed $start_value |
|
170
|
|
|
*/ |
|
171
|
5 |
|
final public static function ana(\Closure $unfolder, $start_value) : FSObject |
|
172
|
|
|
{ |
|
173
|
5 |
|
$unfolded = $unfolder($start_value); |
|
174
|
5 |
|
if ($unfolded->isFile()) { |
|
175
|
5 |
|
return $unfolded; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
4 |
|
return new GenericFixedFDirectory( |
|
179
|
4 |
|
$unfolded->fmap(function ($value) use ($unfolder) { |
|
180
|
4 |
|
return self::ana($unfolder, $value); |
|
181
|
4 |
|
}) |
|
182
|
|
|
); |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.