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