1 | <?php |
||
11 | class Repository |
||
12 | { |
||
13 | protected $name; |
||
14 | protected $path; |
||
15 | protected $formatter; |
||
16 | protected $queryClass; |
||
17 | protected $documentClass; |
||
18 | |||
19 | /** |
||
20 | * Constructor |
||
21 | * |
||
22 | * @param string $name The name of the repository. Must match /[A-Za-z0-9_-]{1,63}+/ |
||
23 | * @param Config $config The config to use for this repo |
||
24 | */ |
||
25 | 32 | public function __construct($name, Config $config) |
|
46 | |||
47 | /** |
||
48 | * Returns the name of this repository |
||
49 | * |
||
50 | * @return string The name of the repo |
||
51 | */ |
||
52 | 3 | public function getName() |
|
56 | |||
57 | /** |
||
58 | * Returns the filesystem path of this repository. |
||
59 | * |
||
60 | * @return string The path where documents are stored. |
||
61 | */ |
||
62 | public function getPath() |
||
66 | |||
67 | /** |
||
68 | * A factory method that initialises and returns an instance of a Query object. |
||
69 | * |
||
70 | * @return Query A new Query class for this repo. |
||
71 | */ |
||
72 | 1 | public function query() |
|
78 | |||
79 | /** |
||
80 | * Returns all the documents within this repo. |
||
81 | * |
||
82 | * @return array An array of Documents. |
||
83 | */ |
||
84 | 15 | public function findAll() |
|
107 | |||
108 | /** |
||
109 | * Returns a single document based on it's ID |
||
110 | * |
||
111 | * @param string $id The ID of the document to find |
||
112 | * |
||
113 | * @return Document|boolean The document if it exists, false if not. |
||
114 | */ |
||
115 | public function findById($id) |
||
138 | |||
139 | /** |
||
140 | * Store a Document in the repository. |
||
141 | * |
||
142 | * @param Document $document The document to store |
||
143 | * |
||
144 | * @return bool True if stored, otherwise false |
||
145 | */ |
||
146 | 4 | public function store(DocumentInterface $document) |
|
169 | |||
170 | /** |
||
171 | * Store a Document in the repository, but only if it already |
||
172 | * exists. The document must have an ID. |
||
173 | * |
||
174 | * @param Document $document The document to store |
||
175 | * |
||
176 | * @return bool True if stored, otherwise false |
||
177 | */ |
||
178 | 1 | public function update(DocumentInterface $document) |
|
199 | |||
200 | /** |
||
201 | * Delete a document from the repository using its ID. |
||
202 | * |
||
203 | * @param mixed $id The ID of the document (or the document itself) to delete |
||
204 | * |
||
205 | * @return boolean True if deleted, false if not. |
||
206 | */ |
||
207 | 3 | public function delete($id) |
|
217 | |||
218 | /** |
||
219 | * Get the filesystem path for a document based on it's ID. |
||
220 | * |
||
221 | * @param string $id The ID of the document. |
||
222 | * |
||
223 | * @return string The full filesystem path of the document. |
||
224 | */ |
||
225 | 4 | public function getPathForDocument($id) |
|
233 | |||
234 | /** |
||
235 | * Gets just the filename for a document based on it's ID. |
||
236 | * |
||
237 | * @param string $id The ID of the document. |
||
238 | * |
||
239 | * @return string The filename of the document, including extension. |
||
240 | */ |
||
241 | 4 | public function getFilename($id) |
|
245 | |||
246 | /** |
||
247 | * Get an array containing the path of all files in this repository |
||
248 | * |
||
249 | * @return array An array, item is a file |
||
250 | */ |
||
251 | 14 | public function getAllFiles() |
|
259 | |||
260 | /** |
||
261 | * Writes data to the filesystem. |
||
262 | * |
||
263 | * @todo Abstract this into a filesystem layer. |
||
264 | * |
||
265 | * @param string $path The absolute file path to write to |
||
266 | * @param string $contents The contents of the file to write |
||
267 | * |
||
268 | * @return boolean Returns true if write was successful, false if not. |
||
269 | */ |
||
270 | 4 | protected function write($path, $contents) |
|
282 | |||
283 | /** |
||
284 | * Validates the name of the repo to ensure it can be stored in the |
||
285 | * filesystem. |
||
286 | * |
||
287 | * @param string $name The name to validate against |
||
288 | * |
||
289 | * @return bool Returns true if valid. Throws an exception if not. |
||
290 | */ |
||
291 | 32 | protected function validateName($name) |
|
299 | |||
300 | /** |
||
301 | * Checks to see if a document ID is valid |
||
302 | * |
||
303 | * @param string $id The ID to check |
||
304 | * |
||
305 | * @return bool True if valid, otherwise false |
||
306 | */ |
||
307 | 4 | protected function validateId($id) |
|
311 | |||
312 | /** |
||
313 | * Generates a random, unique ID for a document. The result is returned in |
||
314 | * base62. This keeps it shorted but still human readable if shared in URLs. |
||
315 | * |
||
316 | * @return string The generated ID. |
||
317 | */ |
||
318 | protected function generateId() |
||
327 | |||
328 | /** |
||
329 | * Get a document's ID base on its filesystem path |
||
330 | * |
||
331 | * @param string $path The full path to the file (including file extension) |
||
332 | * @param string $ext The file extension (without the period) |
||
333 | * |
||
334 | * @return string The ID of the document |
||
335 | */ |
||
336 | 14 | protected function getIdFromPath($path, $ext) |
|
340 | |||
341 | } |
||
342 |