<?php
namespace Engine\Storage; class Disk implements \Engine\Storage\BaseInterface { protected $dataPath=''; public function __construct() { global $CFG; $this->dataPath = realpath(MODULE_REL_PATH.$CFG['paths']['files']).'/'.$CFG['storage']['disk']['location']; } private function getFileDetails($key) { $return=[]; $key=$key; $return['keyHash'] = md5($key); $keyDir = substr($return['keyHash'], 0, 2); $return['fileLocation'] = $this->dataPath.$keyDir.'/'; $return['fileName'] = $return['keyHash'].'.data'; return $return; } public function set($key, $value) { if (!$key) { throw new \Exception("The key wasn't specified"); } if ($value===null) { return false; } $params = $this->getFileDetails($key); if (!\Engine\Dir::create($params['fileLocation'])) { throw new \Exception("Cannot create stored data folder"); } $fileContent = serialize($value); if (!\Engine\File::create($params['fileLocation'].$params['fileName'], $fileContent)) { throw new \Exception(sprintf("Cannot set key in storage! key: %s, value: %s", $key, $value)); } return true; } public function get($key) { if (!$key) { throw new \Exception("The key wasn't specified"); } $params=$this->getFileDetails($key); if (!is_file($params['fileLocation'].$params['fileName'])) { return null; } $fileContents = \Engine\File::getContents($params['fileLocation'].$params['fileName']); return unserialize($fileContents); } public function delete($key) { if (!$key) { throw new \Exception("The key wasn't specified"); } $params = $this->getFileDetails($key); if (!is_file($params['fileLocation'].$params['fileName'])) { return true; } $return = \Engine\File::delete($params['fileLocation'].$params['fileName']); return (Bool)$return; } public function flush() { return \Engine\Dir::delete($this->dataPath); } public function getInfo($key) { if (!$key) { throw new \Exception("The key wasn't specified"); } $return = []; $return['key'] = $key; $params = $this->getFileDetails($key); $fullFileName = $params['fileLocation'].$params['fileName']; if (!is_file($fullFileName)) { $return['fileExists'] = false; } else { $return['fileExists'] = true; $return['keyHash'] = $params['keyHash']; $return['fileLocation'] = $params['fileLocation']; $return['fileName'] = $params['fileName']; $return['fileSize'] = filesize($fullFileName); } return $return; } } 