3 declare(strict_types
=1);
5 namespace Shaarli\Bookmark
;
7 use malkusch\lock\mutex\Mutex
;
8 use malkusch\lock\mutex\NoMutex
;
9 use Shaarli\Bookmark\Exception\DatastoreNotInitializedException
;
10 use Shaarli\Bookmark\Exception\EmptyDataStoreException
;
11 use Shaarli\Bookmark\Exception\NotWritableDataStoreException
;
12 use Shaarli\Config\ConfigManager
;
17 * This class performs read/write operation to the file data store.
18 * Used by BookmarkFileService.
20 * @package Shaarli\Bookmark
25 * @var string Datastore file path
30 * @var ConfigManager instance
39 * string Datastore PHP prefix
41 protected static $phpPrefix = '<?php /* ';
43 * string Datastore PHP suffix
45 protected static $phpSuffix = ' */ ?>';
48 * LinksIO constructor.
50 * @param ConfigManager $conf instance
52 public function __construct(ConfigManager
$conf, Mutex
$mutex = null)
54 if ($mutex === null) {
55 // This should only happen with legacy classes
56 $mutex = new NoMutex();
59 $this->datastore
= $conf->get('resource.datastore');
60 $this->mutex
= $mutex;
64 * Reads database from disk to memory
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
72 public function read()
74 if (! file_exists($this->datastore
)) {
75 throw new DatastoreNotInitializedException();
78 if (!is_writable($this->datastore
)) {
79 throw new NotWritableDataStoreException($this->datastore
);
83 $this->mutex
->synchronized(function () use (&$content) {
84 $content = file_get_contents($this->datastore
);
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(
90 substr($content, strlen(self
::$phpPrefix), -strlen(self
::$phpSuffix))
94 if (filesize($this->datastore
) > 100) {
95 throw new NotWritableDataStoreException($this->datastore
);
97 throw new EmptyDataStoreException();
104 * Saves the database from memory to disk
106 * @param Bookmark[] $links
108 * @throws NotWritableDataStoreException the datastore is not writable
110 public function write($links)
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
));
120 $data = self
::$phpPrefix.base64_encode(gzdeflate(serialize($links))).self
::$phpSuffix;
122 $this->mutex
->synchronized(function () use ($data) {