/
SqonInterface.php
384 lines (359 loc) · 10.8 KB
/
SqonInterface.php
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<?php
namespace Sqon;
use Countable;
use Generator;
use Iterator;
use Sqon\Container\Database;
use Sqon\Exception\SqonException;
use Sqon\Path\PathInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
* Defines the public interface for a Sqon manager.
*
* @author Kevin Herrera <kevin@herrera.io>
*/
interface SqonInterface extends Countable
{
/**
* Indicates that the contents are compressed using bzip2.
*
* @var integer
*/
const BZIP2 = Database::BZIP2;
/**
* Indicates that the contents are compressed using gzip.
*
* @var integer
*/
const GZIP = Database::GZIP;
/**
* Indicates that the contents are not compressed.
*
* @var integer
*/
const NONE = Database::NONE;
/**
* The path to the primary script in the Sqon.
*
* @var string
*/
const PRIMARY = '.sqon/primary.php';
/**
* Commits the changes to disk.
*
* Changes made to a Sqon are not performed directly on the Sqon. To save
* any changes that are made to a new or existing Sqon, they have to be
* committed to disk. If an existing Sqon is being committed, the contents
* of the existing Sqon is replaced.
*
* ```php
* $sqon->commit();
* ```
*
* **Events:**
*
* - `Sqon\Event\BeforeCommitEvent::NAME`
* - `Sqon\Event\AfterCommitEvent::NAME`
*/
public function commit();
/**
* Returns the total number of paths in the Sqon.
*
* ```php
* $count = count($sqon);
* ```
*
* @return integer The total number of paths.
*/
public function count();
/**
* Creates a new Sqon.
*
* If a file exists at the given path, the contents will be replaced when
* the `commit()` method is called. Any existing path information in the
* Sqon will be lost if the new Sqon is committed to disk.
*
* ```php
* $sqon = Sqon::create('/path/to/example.sqon');
* ```
*
* If a `$bootstrap` script is not provided, the default PHP bootstrap
* script provided by the implementation will be used. This is the same
* as providing the value returned by the `createBootstrap()` method
* without any arguments.
*
* @param string $path The path to the Sqon.
* @param null|string $bootstrap The PHP bootstrap script.
*
* @return SqonInterface The new Sqon manager.
*/
public static function create($path, $bootstrap = null);
/**
* Creates a new PHP bootstrap script.
*
* The implementation of this interface will provide its own PHP bootstrap
* script that is used to create new Sqons. By default, the script will not
* have a shebang line. If a shebang line is provided, it is prepended to
* the PHP bootstrap script that is returned.
*
* ```php
* $bootstrap = Sqon::createBootstrap('#!/usr/bin/env php');
* ```
*
* The PHP bootstrap script always ends with `__HALT_COMPILER();`.
*
* @param null|string $shebang The shebang line.
*
* @return string The new PHP bootstrap script.
*/
public static function createBootstrap($shebang = null);
/**
* Extracts the contents of the Sqon to a directory.
*
* If the given directory path does not exist, it will be automatically
* created. If one or more `$paths` are not specified, all paths in the
* Sqon will be extracted to the directory. If `$overwrite` is true, any
* path that already exists will be overwritten.
*
* **Events:**
*
* - `Sqon\Event\BeforeExtractToEvent::NAME`
* - `Sqon\Event\AfterExtractToEvent::NAME`
*
* @param string $dir The path to the directory to extract to.
* @param string[] $paths The paths in the Sqon to extract.
* @param boolean $overwrite Overwrite existing paths?
*/
public function extractTo($dir, array $paths = [], $overwrite = true);
/**
* Returns the PHP bootstrap script for the Sqon.
*
* ```php
* $bootstrap = $sqon->getBootstrap();
* ```
*
* @return string The PHP bootstrap script.
*/
public function getBootstrap();
/**
* Returns the event dispatcher.
*
* ```php
* $dispatcher = $sqon->getEventDispatcher();
*
* if (null !== $dispatcher) {
* // ...
* }
* ```
*
* @return EventDispatcherInterface|null The event dispatcher.
*/
public function getEventDispatcher();
/**
* Returns the path manager for a path stored in the Sqon.
*
* If the file contents were compressed, they are automatically
* decompressed before being returned in a path manager.
*
* ```php
* $path = $sqon->getPath('path/inside/sqon.php');
* ```
*
* @param string $path The path in the Sqon.
*
* @return PathInterface The path manager.
*
* @throws SqonException If the path does not exist.
*/
public function getPath($path);
/**
* Returns the path to the Sqon or where the Sqon will be committed.
*
* @return string The path to the Sqon.
*/
public function getPathToSqon();
/**
* Returns all of the paths stored in the Sqon as path managers.
*
* ```php
* foreach ($sqon->getPaths() as $path) {
* // ...
* }
* ```
*
* @return Generator|PathInterface[] The path managers.
*/
public function getPaths();
/**
* Checks if a path exists in the Sqon.
*
* ```php
* if ($sqon->hasPath('path/inside/sqon.php')) {
* // ...
* }
* ```
*
* @param string $path The path to check for.
*
* @return boolean Returns `true` if it exists, `false` if not.
*/
public function hasPath($path);
/**
* Checks if the signature for a Sqon is valid.
*
* ```php
* if (Sqon::isValid('/path/to/example.sqon')) {
* // ...
* }
* ```
*
* @param string $path The path to the Sqon.
*
* @return boolean Returns `true` if it is valid, `false` if not.
*/
public static function isValid($path);
/**
* Opens an existing Sqon.
*
* The `open()` verifies the signature and then parses the Sqon file to
* extract the PHP bootstrap script and embedded database. Unlike the
* `create()` method, the PHP bootstrap script and path information in
* the database are preserved.
*
* ```php
* $sqon = Sqon::open('/path/to/example.sqon');
* ```
*
* @param string $path The path to the Sqon.
*
* @return SqonInterface The new Sqon manager.
*
* @throws SqonException If the Sqon does not exist.
* @throws SqonException If the signature is not valid.
*/
public static function open($path);
/**
* Removes a path in of the Sqon.
*
* ```php
* $sqon->removePath('path/inside/sqon.php');
* ```
*
* @param string $path The path to remove.
*
* @return SqonInterface A fluent interface to the Sqon manager.
*/
public function removePath($path);
/**
* Sets the PHP bootstrap script.
*
* If the script does not begin with `<?php` (excluding the shebang line)
* or does not end with `__HALT_COMPILER();` an exception is thrown stating
* the issue.
*
* ```php
* $sqon->setBootstrap($script);
* ```
*
* **Events:**
*
* - `Sqon\Event\BeforeSetBootstrapEvent::NAME`
* - `Sqon\Event\AfterSetBootstrapEvent::NAME`
*
* @param string $script The PHP bootstrap script.
*
* @return SqonInterface A fluent interface to the Sqon manager.
*
* @throws SqonException If the script is not valid.
*/
public function setBootstrap($script);
/**
* Sets the compression mode for newly set paths.
*
* By default, file contents are not compressed when added to the Sqon.
* If a compression mode is set, new file contents added to the Sqon are
* automatically compressed using the compression scheme chose through the
* compression mode.
*
* ```php
* $sqon->setCompression(Sqon::GZIP);
* ```
*
* @param integer $mode The compression mode.
*
* @return SqonInterface A fluent interface to the Sqon manager.
*/
public function setCompression($mode);
/**
* Sets the event dispatcher.
*
* An event dispatcher can be registered with the Sqon manager to allow
* for certain processes to be modified by event listeners. Each process
* can be listened to before or after it has been executed. The following
* processes can be listened to:
*
* - `commit()`
* - `extractTo()`
* - `setBootstrap()`
* - `setPath()`
* - `setPathsUsingIterator()`
*
* > It is important to note that using this functionality could cause a
* > a significant impact to performance for certain processes. An example
* > would be registering a listener that modifies file contents as they
* > are set in the Sqon, which would slow down the rate at which paths can
* > be set in the Sqon.
*
* ```php
* $sqon->setEventDispatcher(new EventDispatcher());
* ```
*
* @param EventDispatcherInterface|null $dispatcher The event dispatcher.
*
* @return SqonInterface A fluent interface to the Sqon manager.
*/
public function setEventDispatcher(
EventDispatcherInterface $dispatcher = null
);
/**
* Sets the information for a path in the Sqon.
*
* ```php
* $sqon->setPath('path/inside/sqon.php', $path);
* ```
*
* **Events:**
*
* - `Sqon\Event\BeforeSetPathEvent::NAME`
* - `Sqon\Event\AfterSetPathEvent::NAME`
*
* @param string $path The path to set.
* @param PathInterface $manager The path manager.
*
* @return SqonInterface A fluent interface to the Sqon manager.
*/
public function setPath($path, PathInterface $manager);
/**
* Sets one or more paths using a path manager iterator.
*
* The iterator is expected to provide a canonicalized, relative path as
* the key and the corresponding path manager as the value. If an iterator
* does not return the values as expected, an exception is thrown.
*
* ```php
* $sqon->setUsingIterator($iterator);
* ```
*
* **Events:**
*
* - `Sqon\Event\BeforeSetPathsUsingIteratorEvent::NAME`
* - `Sqon\Event\AfterSetPathsUsingIteratorEvent::NAME`
*
* @param Iterator $iterator The path manager iterator.
*
* @return SqonInterface A fluent interface to the Sqon manager.
*
* @throws SqonException If the iterator is not valid.
*/
public function setPathsUsingIterator(Iterator $iterator);
}