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