aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/bookmark/BookmarkIO.php
diff options
context:
space:
mode:
Diffstat (limited to 'application/bookmark/BookmarkIO.php')
-rw-r--r--application/bookmark/BookmarkIO.php108
1 files changed, 108 insertions, 0 deletions
diff --git a/application/bookmark/BookmarkIO.php b/application/bookmark/BookmarkIO.php
new file mode 100644
index 00000000..ae9ffcb4
--- /dev/null
+++ b/application/bookmark/BookmarkIO.php
@@ -0,0 +1,108 @@
1<?php
2
3namespace Shaarli\Bookmark;
4
5use Shaarli\Bookmark\Exception\EmptyDataStoreException;
6use Shaarli\Bookmark\Exception\NotWritableDataStoreException;
7use Shaarli\Config\ConfigManager;
8
9/**
10 * Class BookmarkIO
11 *
12 * This class performs read/write operation to the file data store.
13 * Used by BookmarkFileService.
14 *
15 * @package Shaarli\Bookmark
16 */
17class BookmarkIO
18{
19 /**
20 * @var string Datastore file path
21 */
22 protected $datastore;
23
24 /**
25 * @var ConfigManager instance
26 */
27 protected $conf;
28
29 /**
30 * string Datastore PHP prefix
31 */
32 protected static $phpPrefix = '<?php /* ';
33
34 /**
35 * string Datastore PHP suffix
36 */
37 protected static $phpSuffix = ' */ ?>';
38
39 /**
40 * LinksIO constructor.
41 *
42 * @param ConfigManager $conf instance
43 */
44 public function __construct($conf)
45 {
46 $this->conf = $conf;
47 $this->datastore = $conf->get('resource.datastore');
48 }
49
50 /**
51 * Reads database from disk to memory
52 *
53 * @return BookmarkArray instance
54 *
55 * @throws NotWritableDataStoreException Data couldn't be loaded
56 * @throws EmptyDataStoreException Datastore doesn't exist
57 */
58 public function read()
59 {
60 if (! file_exists($this->datastore)) {
61 throw new EmptyDataStoreException();
62 }
63
64 if (!is_writable($this->datastore)) {
65 throw new NotWritableDataStoreException($this->datastore);
66 }
67
68 // Note that gzinflate is faster than gzuncompress.
69 // See: http://www.php.net/manual/en/function.gzdeflate.php#96439
70 $links = unserialize(gzinflate(base64_decode(
71 substr(file_get_contents($this->datastore),
72 strlen(self::$phpPrefix), -strlen(self::$phpSuffix)))));
73
74 if (empty($links)) {
75 if (filesize($this->datastore) > 100) {
76 throw new NotWritableDataStoreException($this->datastore);
77 }
78 throw new EmptyDataStoreException();
79 }
80
81 return $links;
82 }
83
84 /**
85 * Saves the database from memory to disk
86 *
87 * @param BookmarkArray $links instance.
88 *
89 * @throws NotWritableDataStoreException the datastore is not writable
90 */
91 public function write($links)
92 {
93 if (is_file($this->datastore) && !is_writeable($this->datastore)) {
94 // The datastore exists but is not writeable
95 throw new NotWritableDataStoreException($this->datastore);
96 } else if (!is_file($this->datastore) && !is_writeable(dirname($this->datastore))) {
97 // The datastore does not exist and its parent directory is not writeable
98 throw new NotWritableDataStoreException(dirname($this->datastore));
99 }
100
101 file_put_contents(
102 $this->datastore,
103 self::$phpPrefix.base64_encode(gzdeflate(serialize($links))).self::$phpSuffix
104 );
105
106 invalidateCaches($this->conf->get('resource.page_cache'));
107 }
108}