11<?php namespace Filebase ;
22
33use Exception ;
4- use Filebase \Config ;
5- use Filebase \Cache ;
6- use Filebase \Filesystem ;
7- use Filebase \Document ;
8- use Filebase \Backup ;
4+ use Filebase \Format \EncodingException ;
5+ use Filebase \Filesystem \SavingException ;
6+ use Filebase \Filesystem \ReadingException ;
7+ use Filebase \Filesystem \FilesystemException ;
98
109class Database
1110{
@@ -31,9 +30,12 @@ class Database
3130
3231
3332 /**
34- * __construct
35- *
36- */
33+ * Database constructor.
34+ *
35+ * @param array $config
36+ *
37+ * @throws FilesystemException
38+ */
3739 public function __construct (array $ config = [])
3840 {
3941 $ this ->config = new Config ($ config );
@@ -46,12 +48,12 @@ public function __construct(array $config = [])
4648 {
4749 if (!@mkdir ($ this ->config ->dir , 0777 , true ))
4850 {
49- throw new Exception (sprintf ('`%s` doesn \'t exist and can \'t be created. ' , $ this ->config ->dir ));
51+ throw new FilesystemException (sprintf ('`%s` doesn \'t exist and can \'t be created. ' , $ this ->config ->dir ));
5052 }
5153 }
5254 else if (!is_writable ($ this ->config ->dir ))
5355 {
54- throw new Exception (sprintf ('`%s` is not writable. ' , $ this ->config ->dir ));
56+ throw new FilesystemException (sprintf ('`%s` is not writable. ' , $ this ->config ->dir ));
5557 }
5658 }
5759
@@ -235,18 +237,16 @@ public function count()
235237
236238
237239 /**
238- * save
239- *
240- * @param $document \Filebase\Document object
241- * @param mixed $data should be an array, new data to replace all existing data within
242- *
243- * @return (bool) true or false if file was saved
244- */
240+ * @param Document $document
241+ * @param string $wdata
242+ * @return bool|Document
243+ * @throws SavingException
244+ */
245245 public function save (Document $ document , $ wdata = '' )
246246 {
247247 if ($ this ->config ->read_only === true )
248248 {
249- throw new Exception ("This database is set to be read-only. No modifications can be made. " );
249+ throw new SavingException ("This database is set to be read-only. No modifications can be made. " );
250250 }
251251
252252 $ format = $ this ->config ->format ;
@@ -270,7 +270,12 @@ public function save(Document $document, $wdata = '')
270270
271271 $ document ->setUpdatedAt (time ());
272272
273- $ data = $ format ::encode ( $ document ->saveAs (), $ this ->config ->pretty );
273+ try {
274+ $ data = $ format ::encode ( $ document ->saveAs (), $ this ->config ->pretty );
275+ } catch (EncodingException $ e ) {
276+ // TODO: add logging
277+ throw new SavingException ("Can not encode document. " , 0 , $ e );
278+ }
274279
275280 if (Filesystem::write ($ file_location , $ data ))
276281 {
@@ -302,17 +307,30 @@ public function query()
302307 //--------------------------------------------------------------------
303308
304309
305-
306310 /**
307- * read
308- *
309- * @param string $name
310- * @return decoded file data
311- */
311+ * Read and return Document from filesystem by name.
312+ * If doesn't exists return new empty Document.
313+ *
314+ * @param $name
315+ *
316+ * @throws Exception|ReadingException
317+ * @return array|null
318+ */
312319 protected function read ($ name )
313320 {
314321 $ format = $ this ->config ->format ;
315- return $ format ::decode ( Filesystem::read ( $ this ->config ->dir .'/ ' .Filesystem::validateName ($ name , $ this ->config ->safe_filename ).'. ' .$ format ::getFileExtension () ) );
322+
323+ $ file = Filesystem::read (
324+ $ this ->config ->dir . '/ '
325+ . Filesystem::validateName ($ name , $ this ->config ->safe_filename )
326+ . '. ' . $ format ::getFileExtension ()
327+ );
328+
329+ if ($ file !== false ) {
330+ return $ format ::decode ($ file );
331+ }
332+
333+ return null ;
316334 }
317335
318336
0 commit comments