PageRenderTime 26ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/concrete/vendor/concrete5/flysystem/src/Cache/AbstractCache.php

https://gitlab.com/koodersmiikka/operaatio-terveys
PHP | 463 lines | 231 code | 69 blank | 163 comment | 33 complexity | 318bd34dcc80939cfffdca72f1728c7d MD5 | raw file
  1. <?php
  2. namespace Concrete\Flysystem\Cache;
  3. use Concrete\Flysystem\CacheInterface;
  4. use Concrete\Flysystem\Util;
  5. abstract class AbstractCache implements CacheInterface
  6. {
  7. /**
  8. * @var boolean $autosave
  9. */
  10. protected $autosave = true;
  11. /**
  12. * @var array $cache
  13. */
  14. protected $cache = array();
  15. /**
  16. * @var array $complete
  17. */
  18. protected $complete = array();
  19. /**
  20. * Destructor
  21. */
  22. public function __destruct()
  23. {
  24. if ( ! $this->autosave) {
  25. $this->save();
  26. }
  27. }
  28. /**
  29. * Get the autosave setting
  30. *
  31. * @return boolean autosave
  32. */
  33. public function getAutosave()
  34. {
  35. return $this->autosave;
  36. }
  37. /**
  38. * Get the autosave setting
  39. *
  40. * @param boolean $autosave
  41. * @return $this
  42. */
  43. public function setAutosave($autosave)
  44. {
  45. $this->autosave = $autosave;
  46. return $this;
  47. }
  48. /**
  49. * Store the contents listing
  50. *
  51. * @param string $directory
  52. * @param array $contents
  53. * @param boolean $recursive
  54. * @return array contents listing
  55. */
  56. public function storeContents($directory, array $contents, $recursive = false)
  57. {
  58. $directories = array($directory);
  59. foreach ($contents as $index => $object) {
  60. $object = $this->updateObject($object['path'], $object);
  61. $contents[$index] = $object;
  62. if ( ! empty($directory) && strpos($object['path'], $directory) === false) {
  63. unset($contents[$index]);
  64. continue;
  65. }
  66. if ($recursive && ! in_array($object['dirname'], $directories)) {
  67. $directories[] = $object['dirname'];
  68. }
  69. if ($recursive === false && $object['dirname'] !== $directory) {
  70. unset($contents[$index]);
  71. }
  72. }
  73. foreach ($directories as $directory)
  74. $this->setComplete($directory, $recursive);
  75. $this->autosave();
  76. return array_values($contents);
  77. }
  78. /**
  79. * Update the metadata for an object
  80. *
  81. * @param string $path object path
  82. * @param array $object object metadata
  83. * @param boolean $autosave whether to trigger the autosave routine
  84. */
  85. public function updateObject($path, array $object, $autosave = false)
  86. {
  87. if ( ! $this->has($path)) {
  88. $this->cache[$path] = Util::pathinfo($path);
  89. }
  90. $this->cache[$path] = array_merge($this->cache[$path], $object);
  91. if ($autosave) {
  92. $this->autosave();
  93. }
  94. return $this->cache[$path];
  95. }
  96. /**
  97. * Store object hit miss
  98. *
  99. * @param string $path
  100. * @return $this
  101. */
  102. public function storeMiss($path)
  103. {
  104. $this->cache[$path] = false;
  105. $this->autosave();
  106. return $this;
  107. }
  108. /**
  109. * Get the contents listing
  110. *
  111. * @param string $dirname
  112. * @param boolean $recursive
  113. * @return array contents listing
  114. */
  115. public function listContents($dirname = '', $recursive = false)
  116. {
  117. $result = array();
  118. foreach ($this->cache as $object) {
  119. if ($object['dirname'] !== $dirname) {
  120. continue;
  121. }
  122. $result[] = $object;
  123. if ($recursive && $object['type'] === 'dir') {
  124. $result = array_merge($result, $this->listContents($object['path'], true));
  125. }
  126. }
  127. return $result;
  128. }
  129. /**
  130. * Check whether an object has been cached
  131. *
  132. * @param string $path
  133. * @return boolean cached boolean
  134. */
  135. public function has($path)
  136. {
  137. if ( ! isset($this->cache[$path])) {
  138. return $this->isComplete(Util::dirname($path), false) ? false : null;
  139. }
  140. return $this->cache[$path] !== false;
  141. }
  142. /**
  143. * Retrieve the contents of an object
  144. *
  145. * @param string $path
  146. * @return false|string contents or null on failure
  147. */
  148. public function read($path)
  149. {
  150. if (isset($this->cache[$path]['contents'])) {
  151. return $this->cache[$path]['contents'];
  152. }
  153. return false;
  154. }
  155. /**
  156. * Retrieve the contents of an object
  157. *
  158. * @param string $path
  159. * @return false|string contents or null on failure
  160. */
  161. public function readStream($path)
  162. {
  163. if (isset($this->cache[$path]['stream'])) {
  164. return $this->cache[$path]['stream'];
  165. }
  166. return false;
  167. }
  168. /**
  169. * Rename an object
  170. *
  171. * @param string $path
  172. * @param string $newpath
  173. */
  174. public function rename($path, $newpath)
  175. {
  176. if ( ! $this->has($path)) return false;
  177. $object = $this->cache[$path];
  178. unset($this->cache[$path]);
  179. $object['path'] = $newpath;
  180. $object = array_merge($object, Util::pathinfo($newpath));
  181. $this->cache[$newpath] = $object;
  182. $this->autosave();
  183. }
  184. /**
  185. * Copy an object
  186. *
  187. * @param string $path
  188. * @param string $newpath
  189. */
  190. public function copy($path, $newpath)
  191. {
  192. if ( ! $this->has($path)) {
  193. return false;
  194. }
  195. $object = $this->cache[$path];
  196. $object = array_merge($object, Util::pathinfo($newpath));
  197. return $this->updateObject($newpath, $object, true);
  198. }
  199. /**
  200. * Delete an object from cache
  201. *
  202. * @param string $path object path
  203. * @return $this
  204. */
  205. public function delete($path)
  206. {
  207. if (isset($this->cache[$path])) {
  208. unset($this->cache[$path]);
  209. }
  210. $this->autosave();
  211. return $this;
  212. }
  213. /**
  214. * Delete a directory from cache and all its siblings
  215. *
  216. * @param string $dirname object path
  217. */
  218. public function deleteDir($dirname)
  219. {
  220. foreach ($this->cache as $path => $object) {
  221. if (strpos($path, $dirname) === 0) {
  222. unset($this->cache[$path]);
  223. }
  224. }
  225. if (isset($this->complete[$dirname])) {
  226. unset($this->complete[$dirname]);
  227. }
  228. $this->autosave();
  229. }
  230. /**
  231. * Retrieve the mimetype of an object
  232. *
  233. * @param string $path
  234. * @return null|string mimetype or null on failure
  235. */
  236. public function getMimetype($path)
  237. {
  238. if (isset($this->cache[$path]['mimetype'])) {
  239. return $this->cache[$path]['mimetype'];
  240. }
  241. if ( ! $contents = $this->read($path)) {
  242. return false;
  243. }
  244. $mimetype = Util::guessMimeType($path, $contents);
  245. $this->cache[$path]['mimetype'] = $mimetype;
  246. return $mimetype;
  247. }
  248. /**
  249. * Retrieve the size of an object
  250. *
  251. * @param string $path
  252. * @return false|string size or null on failure
  253. */
  254. public function getSize($path)
  255. {
  256. if (isset($this->cache[$path]['size'])) {
  257. return $this->cache[$path]['size'];
  258. }
  259. return false;
  260. }
  261. /**
  262. * Retrieve the timestamp of an object
  263. *
  264. * @param string $path
  265. * @return null|integer timestamp or null on failure
  266. */
  267. public function getTimestamp($path)
  268. {
  269. if (isset($this->cache[$path]['timestamp'])) {
  270. return $this->cache[$path]['timestamp'];
  271. }
  272. }
  273. /**
  274. * Retrieve the visibility of an object
  275. *
  276. * @param string $path
  277. * @return null|string visibility or null on failure
  278. */
  279. public function getVisibility($path)
  280. {
  281. if (isset($this->cache[$path]['visibility'])) {
  282. return $this->cache[$path]['visibility'];
  283. }
  284. }
  285. /**
  286. * Retrieve the metadata of an object
  287. *
  288. * @param string $path
  289. * @return null|array metadata or null on failure
  290. */
  291. public function getMetadata($path)
  292. {
  293. if (isset($this->cache[$path]['type'])) {
  294. return $this->cache[$path];
  295. }
  296. }
  297. /**
  298. * Check whether the listing is complete
  299. *
  300. * @param string $dirname
  301. * @param boolean $recursive
  302. * @return boolean
  303. */
  304. public function isComplete($dirname, $recursive)
  305. {
  306. if ( ! array_key_exists($dirname, $this->complete)) {
  307. return false;
  308. }
  309. if ($recursive && $this->complete[$dirname] !== 'recursive') {
  310. return false;
  311. }
  312. return true;
  313. }
  314. /**
  315. * Set the cache to complete
  316. *
  317. * @param string $dirname
  318. * @param boolean $recursive
  319. * @return $this
  320. */
  321. public function setComplete($dirname, $recursive)
  322. {
  323. $this->complete[$dirname] = $recursive ? 'recursive' : true;
  324. return $this;
  325. }
  326. /**
  327. * Filter the contents from a listing
  328. *
  329. * @param array $contents object listing
  330. * @return array filtered contents
  331. */
  332. public function cleanContents(array $contents)
  333. {
  334. foreach ($contents as $path => $object) {
  335. if (isset($object['contents'])) {
  336. unset($contents[$path]['contents']);
  337. }
  338. }
  339. return $contents;
  340. }
  341. /**
  342. * Flush the cache
  343. */
  344. public function flush()
  345. {
  346. $this->cache = array();
  347. $this->complete = array();
  348. $this->autosave();
  349. }
  350. /**
  351. * Trigger autosaving
  352. */
  353. public function autosave()
  354. {
  355. if ($this->autosave) {
  356. $this->save();
  357. }
  358. }
  359. /**
  360. * Retrieve serialized cache data
  361. *
  362. * @return string serialized data
  363. */
  364. public function getForStorage()
  365. {
  366. $cleaned = $this->cleanContents($this->cache);
  367. return json_encode(array($cleaned, $this->complete));
  368. }
  369. /**
  370. * Load from serialized cache data
  371. *
  372. * @param string $json
  373. */
  374. public function setFromStorage($json)
  375. {
  376. list ($cache, $complete) = json_decode($json, true);
  377. $this->cache = $cache;
  378. $this->complete = $complete;
  379. }
  380. /**
  381. * Ensure parent directories of an object
  382. *
  383. * @param string $path object path
  384. */
  385. public function ensureParentDirectories($path)
  386. {
  387. $object = $this->cache[$path];
  388. while ($object['dirname'] !== '' && ! isset($this->cache[$object['dirname']])) {
  389. $object = Util::pathinfo($object['dirname']);
  390. $object['type'] = 'dir';
  391. $this->cache[$object['path']] = $object;
  392. }
  393. }
  394. }