]>
Commit | Line | Data |
---|---|---|
336a28fa A |
1 | <?php |
2 | ||
3 | namespace Shaarli\Bookmark; | |
4 | ||
fd1ddad9 A |
5 | use malkusch\lock\mutex\Mutex; |
6 | use malkusch\lock\mutex\NoMutex; | |
d6e5f04d | 7 | use Shaarli\Bookmark\Exception\DatastoreNotInitializedException; |
336a28fa A |
8 | use Shaarli\Bookmark\Exception\EmptyDataStoreException; |
9 | use Shaarli\Bookmark\Exception\NotWritableDataStoreException; | |
10 | use Shaarli\Config\ConfigManager; | |
11 | ||
12 | /** | |
13 | * Class BookmarkIO | |
14 | * | |
15 | * This class performs read/write operation to the file data store. | |
16 | * Used by BookmarkFileService. | |
17 | * | |
18 | * @package Shaarli\Bookmark | |
19 | */ | |
20 | class BookmarkIO | |
21 | { | |
22 | /** | |
23 | * @var string Datastore file path | |
24 | */ | |
25 | protected $datastore; | |
26 | ||
27 | /** | |
28 | * @var ConfigManager instance | |
29 | */ | |
30 | protected $conf; | |
31 | ||
fd1ddad9 A |
32 | |
33 | /** @var Mutex */ | |
34 | protected $mutex; | |
35 | ||
336a28fa A |
36 | /** |
37 | * string Datastore PHP prefix | |
38 | */ | |
39 | protected static $phpPrefix = '<?php /* '; | |
336a28fa A |
40 | /** |
41 | * string Datastore PHP suffix | |
42 | */ | |
43 | protected static $phpSuffix = ' */ ?>'; | |
44 | ||
45 | /** | |
46 | * LinksIO constructor. | |
47 | * | |
48 | * @param ConfigManager $conf instance | |
49 | */ | |
fd1ddad9 | 50 | public function __construct(ConfigManager $conf, Mutex $mutex = null) |
336a28fa | 51 | { |
fd1ddad9 A |
52 | if ($mutex === null) { |
53 | // This should only happen with legacy classes | |
54 | $mutex = new NoMutex(); | |
55 | } | |
336a28fa A |
56 | $this->conf = $conf; |
57 | $this->datastore = $conf->get('resource.datastore'); | |
fd1ddad9 | 58 | $this->mutex = $mutex; |
336a28fa A |
59 | } |
60 | ||
61 | /** | |
62 | * Reads database from disk to memory | |
63 | * | |
64 | * @return BookmarkArray instance | |
65 | * | |
d6e5f04d A |
66 | * @throws NotWritableDataStoreException Data couldn't be loaded |
67 | * @throws EmptyDataStoreException Datastore file exists but does not contain any bookmark | |
68 | * @throws DatastoreNotInitializedException File does not exists | |
336a28fa A |
69 | */ |
70 | public function read() | |
71 | { | |
72 | if (! file_exists($this->datastore)) { | |
d6e5f04d | 73 | throw new DatastoreNotInitializedException(); |
336a28fa A |
74 | } |
75 | ||
76 | if (!is_writable($this->datastore)) { | |
77 | throw new NotWritableDataStoreException($this->datastore); | |
78 | } | |
79 | ||
fd1ddad9 A |
80 | $content = null; |
81 | $this->mutex->synchronized(function () use (&$content) { | |
82 | $content = file_get_contents($this->datastore); | |
83 | }); | |
84 | ||
336a28fa A |
85 | // Note that gzinflate is faster than gzuncompress. |
86 | // See: http://www.php.net/manual/en/function.gzdeflate.php#96439 | |
87 | $links = unserialize(gzinflate(base64_decode( | |
fd1ddad9 A |
88 | substr($content, strlen(self::$phpPrefix), -strlen(self::$phpSuffix)) |
89 | ))); | |
336a28fa A |
90 | |
91 | if (empty($links)) { | |
92 | if (filesize($this->datastore) > 100) { | |
93 | throw new NotWritableDataStoreException($this->datastore); | |
94 | } | |
95 | throw new EmptyDataStoreException(); | |
96 | } | |
97 | ||
98 | return $links; | |
99 | } | |
100 | ||
101 | /** | |
102 | * Saves the database from memory to disk | |
103 | * | |
104 | * @param BookmarkArray $links instance. | |
105 | * | |
106 | * @throws NotWritableDataStoreException the datastore is not writable | |
107 | */ | |
108 | public function write($links) | |
109 | { | |
110 | if (is_file($this->datastore) && !is_writeable($this->datastore)) { | |
111 | // The datastore exists but is not writeable | |
112 | throw new NotWritableDataStoreException($this->datastore); | |
113 | } else if (!is_file($this->datastore) && !is_writeable(dirname($this->datastore))) { | |
114 | // The datastore does not exist and its parent directory is not writeable | |
115 | throw new NotWritableDataStoreException(dirname($this->datastore)); | |
116 | } | |
117 | ||
fd1ddad9 A |
118 | $data = self::$phpPrefix.base64_encode(gzdeflate(serialize($links))).self::$phpSuffix; |
119 | ||
120 | $this->mutex->synchronized(function () use ($data) { | |
121 | file_put_contents( | |
122 | $this->datastore, | |
123 | $data | |
124 | ); | |
125 | }); | |
336a28fa A |
126 | } |
127 | } |