-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPulp.php
More file actions
274 lines (235 loc) · 7.21 KB
/
Pulp.php
File metadata and controls
274 lines (235 loc) · 7.21 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
declare(strict_types=1);
namespace OpenMapsight;
use OpenMapsight\pulp\DebugHandler;
use OpenMapsight\pulp\DeleteHandler;
use OpenMapsight\pulp\DestHandler;
use OpenMapsight\pulp\File;
use OpenMapsight\pulp\FileSwitchHandler;
use OpenMapsight\pulp\FilterHandler;
use OpenMapsight\pulp\Handler;
use OpenMapsight\pulp\MapHandler;
use OpenMapsight\pulp\MergeHandler;
use OpenMapsight\pulp\ResultsHandler;
use OpenMapsight\pulp\ServeHttpHandler;
use OpenMapsight\pulp\ShadowHandler;
use OpenMapsight\pulp\SrcFileHandler;
use OpenMapsight\pulp\SrcHandler;
use OpenMapsight\pulp\SrcHttpHandler;
use OpenMapsight\pulp\Utf8DecodeHandler;
use OpenMapsight\pulp\Utf8EncodeHandler;
use RuntimeException;
class Pulp implements Handler
{
private ?Handler $firstHandler = null;
private ?Handler $lastHandler = null;
private array $result = [];
public static function start(): Pulp
{
return new self();
}
public static function src($pattern, $directory = null): SrcHandler
{
return new SrcHandler($pattern, $directory);
}
/**
* ## Options
* * `skipExceptions`: Skips file (no virtual file is emitted) on exception, and logs.
* (default: `false`)
* * `logSkipExceptions`: (default: `"stderr"`)
* * `false`: no logging
* * `"stdout"`: log to stdout
* * `"stderr"`: log to stderr
*/
public static function srcFile(
$fileName,
$aliasFileName = null,
array $options = []
): SrcFileHandler {
return new SrcFileHandler($fileName, $aliasFileName, $options);
}
/**
* ## Options
* * `skipExceptions`: Skips file (no virtual file is emitted) on exception, and logs.
* (default: `false`)
* * `logSkipExceptions`: (default: `"stderr"`)
* * `false`: no logging
* * `"stdout"`: log to stdout
* * `"stderr"`: log to stderr
*/
public static function srcHttp(
$method,
$uri,
array $guzzleOptions,
$aliasFileName,
array $options = []
): SrcHttpHandler {
return new SrcHttpHandler(
$method,
$uri,
$guzzleOptions,
$aliasFileName,
$options
);
}
/**
* ## Options
* * `flush`: `fflush`es files to disk (default: `false`)
* * `handleLogicallyEmpty`: skips write if the "virtual" file has its `isLogicallyEmpty`
* parameter set to `true` and the size of the "physical" destination file has the same
* length as the value in `logicallyEmptyContent`, **REGARDLESS OF THE ACTUALL CONTENT**.
* If the `isLogicallyEmpty` is set, but the file length is different,
* `logicallyEmptyContent` gets written to the file.
*
* **WARNING**: overwrites the "physical" destination file with `logicallyEmptyContent` if
* the "virtual" files `isLogicallyEmpty` parameter is set to `true`! So watch out if you're
* reusing a `File` object, and set `isLogicallyEmpty` to `false` to be sure, that your file
* doesn't get destroyed.
*
* **WARNING**: **DON'T USE THIS OPTION, IT IS A FOOTGUN**
*
* (default: `false`)
* * `logicallyEmptyContent`: see `handleLogicallyEmpty` option (default: `""`)
* * `skipExceptions`: Ignores exceptions and logs them. (default: `false`)
* * `logSkipExceptions`: (default: `"stderr"`)
* * `false`: no logging
* * `"stdout"`: log to stdout
* * `"stderr"`: log to stderr
*
* @param {string} $directory The path that is used as the base directory path to store the
* files
* @param {{
* flush?: boolean,
* skipWriteOnEmptyFile?: boolean,
* logicallyEmptyContent?: [u8],
* }} [$options]
*/
public static function dest($directory, array $options = []): DestHandler
{
return new DestHandler($directory, $options);
}
public static function delete(): DeleteHandler
{
return new DeleteHandler();
}
public static function filter($callback): FilterHandler
{
return new FilterHandler($callback);
}
public static function map($callback): MapHandler
{
return new MapHandler($callback);
}
public static function utf8encode(): Utf8EncodeHandler
{
return new Utf8EncodeHandler();
}
public static function utf8decode(): Utf8DecodeHandler
{
return new Utf8DecodeHandler();
}
public static function debug($length = 30): DebugHandler
{
return new DebugHandler($length);
}
public static function merge(): MergeHandler
{
$pulps = func_get_args();
foreach ($pulps as $p) {
if (!($p instanceof self)) {
throw new RuntimeException('Not a Pulp');
}
}
return new MergeHandler($pulps);
}
public static function shadow($cb): ShadowHandler
{
return new ShadowHandler($cb);
}
public static function results($cb): ResultsHandler
{
return new ResultsHandler($cb);
}
public static function serveHttp(): ServeHttpHandler
{
return new ServeHttpHandler();
}
/**
* @param array $patterns
* @param callable $defaultCb
*
* @return pulp\FileSwitchHandler
*/
public static function fileSwitch(array $patterns, ?callable $defaultCb = null): FileSwitchHandler
{
return new FileSwitchHandler($patterns, $defaultCb);
}
public function pipe(Handler $handler): static
{
if (!$this->firstHandler instanceof Handler) {
$this->firstHandler = $handler;
}
if ($this->lastHandler instanceof Handler) {
$this->lastHandler->setNextHandler($handler);
}
$handler->setPulp($this);
$this->lastHandler = $handler;
return $this;
}
/**
* @return Handler|null
*/
public function getFirstHandler(): ?Handler
{
return $this->firstHandler;
}
/**
* @return Handler|null
*/
public function getLastHandler(): ?Handler
{
return $this->lastHandler;
}
public function addResultFile(File $result): void
{
$this->result[] = $result;
}
/**
* @param File|File[]|null $files
*
* @return File[]
* @throws RuntimeException
*/
public function run(array|File|null $files = null): array
{
if ($files === null) {
$files = [];
}
if (!is_array($files)) {
$files = [$files];
}
if (!$this->firstHandler instanceof Handler) {
return $files;
}
foreach ($files as $file) {
if (!($file instanceof File)) {
throw new RuntimeException('Not a OpenMapsight\pulp\File object');
}
$this->firstHandler->handleFile($file);
}
$this->firstHandler->handleFile();
return $this->result;
}
public function handleFile(?File $file = null): void
{
$this->firstHandler->handleFile($file);
}
public function setNextHandler(Handler $handler): void
{
$this->lastHandler->setNextHandler($handler);
}
public function setPulp(Pulp $pulp): void
{
$this->lastHandler->setPulp($pulp);
}
}