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() |
|
112 | |||
113 | /** |
||
114 | * Returns a single document based on it's ID |
||
115 | * |
||
116 | * @param string $id The ID of the document to find |
||
117 | * |
||
118 | * @return Document|boolean The document if it exists, false if not. |
||
119 | */ |
||
120 | public function findById($id) |
||
143 | |||
144 | /** |
||
145 | * Store a Document in the repository. |
||
146 | * |
||
147 | * @param Document $document The document to store |
||
148 | * |
||
149 | * @return bool True if stored, otherwise false |
||
150 | */ |
||
151 | 4 | public function store(DocumentInterface $document) |
|
174 | |||
175 | /** |
||
176 | * Store a Document in the repository, but only if it already |
||
177 | * exists. The document must have an ID. |
||
178 | * |
||
179 | * @param Document $document The document to store |
||
180 | * |
||
181 | * @return bool True if stored, otherwise false |
||
182 | */ |
||
183 | 1 | public function update(DocumentInterface $document) |
|
204 | |||
205 | /** |
||
206 | * Delete a document from the repository using its ID. |
||
207 | * |
||
208 | * @param mixed $id The ID of the document (or the document itself) to delete |
||
209 | * |
||
210 | * @return boolean True if deleted, false if not. |
||
211 | */ |
||
212 | 3 | public function delete($id) |
|
222 | |||
223 | /** |
||
224 | * Get the filesystem path for a document based on it's ID. |
||
225 | * |
||
226 | * @param string $id The ID of the document. |
||
227 | * |
||
228 | * @return string The full filesystem path of the document. |
||
229 | */ |
||
230 | 4 | public function getPathForDocument($id) |
|
238 | |||
239 | /** |
||
240 | * Gets just the filename for a document based on it's ID. |
||
241 | * |
||
242 | * @param string $id The ID of the document. |
||
243 | * |
||
244 | * @return string The filename of the document, including extension. |
||
245 | */ |
||
246 | 4 | public function getFilename($id) |
|
250 | |||
251 | /** |
||
252 | * Get an array containing the path of all files in this repository |
||
253 | * |
||
254 | * @return array An array, item is a file |
||
255 | */ |
||
256 | 14 | public function getAllFiles() |
|
264 | |||
265 | /** |
||
266 | * Writes data to the filesystem. |
||
267 | * |
||
268 | * @todo Abstract this into a filesystem layer. |
||
269 | * |
||
270 | * @param string $path The absolute file path to write to |
||
271 | * @param string $contents The contents of the file to write |
||
272 | * |
||
273 | * @return boolean Returns true if write was successful, false if not. |
||
274 | */ |
||
275 | 4 | protected function write($path, $contents) |
|
287 | |||
288 | /** |
||
289 | * Validates the name of the repo to ensure it can be stored in the |
||
290 | * filesystem. |
||
291 | * |
||
292 | * @param string $name The name to validate against |
||
293 | * |
||
294 | * @return bool Returns true if valid. Throws an exception if not. |
||
295 | */ |
||
296 | 32 | protected function validateName($name) |
|
304 | |||
305 | /** |
||
306 | * Checks to see if a document ID is valid |
||
307 | * |
||
308 | * @param string $id The ID to check |
||
309 | * |
||
310 | * @return bool True if valid, otherwise false |
||
311 | */ |
||
312 | 4 | protected function validateId($id) |
|
316 | |||
317 | /** |
||
318 | * Generates a random, unique ID for a document. The result is returned in |
||
319 | * base62. This keeps it shorted but still human readable if shared in URLs. |
||
320 | * |
||
321 | * @return string The generated ID. |
||
322 | */ |
||
323 | protected function generateId() |
||
332 | |||
333 | /** |
||
334 | * Get a document's ID base on its filesystem path |
||
335 | * |
||
336 | * @param string $path The full path to the file (including file extension) |
||
337 | * @param string $ext The file extension (without the period) |
||
338 | * |
||
339 | * @return string The ID of the document |
||
340 | */ |
||
341 | 14 | protected function getIdFromPath($path, $ext) |
|
345 | |||
346 | } |
||
347 |