1: <?php
2:
3: namespace webfilesframework\core\datastore;
4:
5: use webfilesframework\core\datastore\types\directory\MDirectoryDatastore;
6: use webfilesframework\core\datasystem\file\format\MWebfile;
7: use webfilesframework\core\datasystem\file\format\MWebfileStream;
8: use webfilesframework\core\datasystem\file\system\MDirectory;
9:
10: /**
11: * Base class for defining datastores to save and load webfiles on a standardized way.<br />
12: * More about the definition of a datastore can be found under
13: * the following <a href="http://simpleserv.de/webfiles/doc/doku.php?id=definitiondatastore">link</a>.<br />
14: * <br />
15: * Implements the webfiles standard to be able to edit datastores with help of the webfile editor.
16: *
17: * @author Sebastian Monzel < mail@sebastianmonzel.de >
18: * @since 0.1.7
19: */
20: abstract class MAbstractDatastore extends MWebfile
21: {
22:
23: /**
24: * Checks if a connection is possible.
25: */
26: public abstract function tryConnect();
27:
28: /**
29: * Determines if the datastore is read-only or not.
30: * @return boolean information if datastore is readonly or not.
31: */
32: public abstract function isReadOnly();
33:
34: /**
35: * Some datastore cannot be sorted by time due to performance issues.
36: * In this time cache can solve the problem. For letting the developer
37: * decide if implementing sorting by timestamp this function sets
38: * the sorting to true or false.
39: *
40: * @param $timestamp
41: *
42: * @throws MDatastoreException
43: */
44: public function getNextWebfileForTimestamp($timestamp) {
45: throw new MDatastoreException("datastore cannot be sorted by timestamp.");
46: }
47:
48: /**
49: * Returns a webfiles stream with all webfiles from
50: * the actual datastore.
51: * @return MWebfileStream
52: */
53: public abstract function getWebfilesAsStream();
54:
55: /**
56: * Returns all webfiles from the actual datastore.
57: * @return array list of webfiles
58: */
59: public abstract function getWebfilesAsArray();
60:
61: /**
62: * Returns the latests webfiles. Sorting will
63: * happen according to the time information of the webfiles.
64: *
65: * @param int $count Count of webfiles to be selected.
66: * @return array list of webfiles
67: */
68: public abstract function getLatestWebfiles($count = 5);
69:
70:
71: /**
72: * Returns a set of webfiles in the actual datastore which matches
73: * with the given template.<br />
74: * Searching by template is devided in two steps:<br />
75: * <ol>
76: * <li>On the first step you define the template you want to search with. Here can help you the method
77: * <b>presetDefaultForTemplate</b> on the class <b>MWebfile</b>.</li>
78: * <li>On the second step you put the template to the datastore to start the search</li>
79: * </ol>
80: *
81: * @param MWebfile $template template to search for
82: * @return array list of webfiles
83: */
84: public abstract function searchByTemplate(MWebfile $template);/** @noinspection PhpUnusedParameterInspection */
85:
86:
87: /**
88: *
89: * @param array $webfiles
90: * @param MWebfile $template
91: *
92: * @return array
93: * @throws \ReflectionException
94: */
95: protected function filterWebfilesArrayByTemplate($webfiles, MWebfile $template)
96: {
97:
98: $filteredWebfiles = array();
99:
100: /** @var MWebfile $webfile */
101: foreach ($webfiles as $webfile) {
102: if ($webfile->matchesTemplate($template)) {
103: $filteredWebfiles = $this->addWebfileSafetyToArray($webfile, $filteredWebfiles);
104: }
105: }
106:
107: return $filteredWebfiles;
108: }
109:
110: /**@noinspection PhpUnusedParameterInspection*/
111: /**
112: * Stores a single webfile in the datastore.
113: *
114: * @param MWebfile $webfile
115: * @throws MDatastoreException
116: */
117: public function storeWebfile(MWebfile $webfile)
118: {
119: if ($this->isReadOnly()) {
120: throw new MDatastoreException("cannot modify data on read-only datastore.");
121: } else {
122: throw new MDatastoreException("not implemented yet.");
123: }
124: }
125:
126: /**
127: * Stores all webfiles from a given webfilestream in the actual datastore
128: *
129: * @param MWebfileStream $webfileStream
130: * @throws MDatastoreException
131: */
132: public function storeWebfilesFromStream(MWebfileStream $webfileStream)
133: {
134:
135: if ($this->isReadOnly()) {
136: throw new MDatastoreException("cannot modify data on read-only datastore.");
137: }
138:
139: $webfiles = $webfileStream->getWebfiles();
140: foreach ($webfiles as $webfile) {
141: $this->storeWebfile($webfile);
142: }
143: }
144:
145: /**@noinspection PhpUnusedParameterInspection*/
146: /**
147: * Deletes a set of webfiles in the actual datastore which can be
148: * applied to the given template.
149: *
150: * @param MWebfile $template
151: * @throws MDatastoreException
152: */
153: public function deleteByTemplate(MWebfile $template)
154: {
155: if ($this->isReadOnly()) {
156: throw new MDatastoreException("cannot modify data on read-only datastore.");
157: } else {
158: throw new MDatastoreException("not implemented yet.");
159: }
160: }
161:
162: /**
163: * Resolves a datastore which is localized in the folder
164: * <b>"./custom/datastore"</b> according to the given id.<br />
165: * Every file situated in the datastore folder will be converted
166: * to a webfile an the list of webfiles will be used to compare
167: * the id on each datastore in the folder.
168: *
169: * @param string $datastoreId
170: *
171: * @return MWebfile returns the found datastore
172: * @throws MDatastoreException will be thrown if no datastore with*@throws
173: * \webfilesframework\MWebfilesFrameworkException the given id is available.
174: * @throws \ReflectionException
175: * @throws \webfilesframework\MWebfilesFrameworkException
176: */
177: public static function resolveCustomDatastoreById($datastoreId)
178: {
179:
180: $datastoreDirectory = new MDirectory("./custom/datastore/");
181: $datastoreHolder = new MDirectoryDatastore($datastoreDirectory);
182:
183: $webfiles = $datastoreHolder->getWebfilesAsArray();
184:
185: $selectedWebfile = null;
186:
187: /**@var MWebfile $webfile **/
188: foreach ($webfiles as $webfile) {
189: if ($webfile->getId() == $datastoreId) {
190: $selectedWebfile = $webfile;
191: }
192: }
193:
194: if ($selectedWebfile == null) {
195: throw new MDatastoreException("Cannot find datastore for id '" . $datastoreId . "'");
196: }
197:
198: return $selectedWebfile;
199: }
200:
201: /**
202: * @param $webfilesArray
203: *
204: * @return array
205: * @throws \ReflectionException
206: */
207: public static function extractDatasetsFromWebfilesArray($webfilesArray)
208: {
209: $webfilesDatasets = array();
210:
211: /** @var MWebfile $webfile */
212: foreach ($webfilesArray as $webfile) {
213: $dataset = $webfile->getDataset();
214: $webfilesDatasets[] = $dataset;
215: }
216: return $webfilesDatasets;
217: }
218:
219: protected function addWebfileSafetyToArray(MWebfile $webfile, $objectsArray) {
220:
221: $arrayKey = $webfile->getTime();
222: $arrayKeyCount = 1;
223:
224: // make sure files with the same key (normally timetamp) have an unique array key
225: while (isset($objectsArray[$arrayKey])) {
226: $arrayKeyCount++;
227: $arrayKey = $arrayKey . "," . $arrayKeyCount;
228: }
229: $objectsArray[$arrayKey] = $webfile;
230:
231: return $objectsArray;
232: }
233:
234: }