* @version 2.0 */ class StorageFile implements StorageInterface { const ACCESS_READ = 'r'; const ACCESS_WRITE = 'w'; protected $file; protected $fd; protected $access; public function __construct($file) { $this->file = $file; list($this->fd, $this->access) = null; } public function __destruct() { if (!is_null($this->fd)) { $this->close(); } } /** * {@inheritdoc} */ public function openReader() { if (!is_null($this->fd)) { if ($this->access === self::ACCESS_READ) { return; } else { $this->close(); } } if (!is_file($this->file)) { return; } if ((is_file($this->file)) && (!is_readable($this->file))) { throw new \Exception(sprintf("File '%s' is not readable.", $this->file)); } if (($fd = fopen($this->file, 'c+')) === false) { throw new \Exception(sprintf("Can't open '%s' file.", $this->file)); } if (flock($fd, \LOCK_SH) === false) { $this->close(); throw new \Exception(sprintf("Can't lock '%s' file for reading.", $this->file)); } $this->fd = $fd; $this->access = self::ACCESS_READ; } /** * {@inheritdoc} */ public function openWriter() { if (!is_null($this->fd)) { if ($this->access === self::ACCESS_WRITE) { return; } else { $this->close(); } } if ((!is_file($this->file)) && (!is_writable(dirname($this->file)))) { throw new \Exception(sprintf("Directory '%s' does not exist or is not writable.", dirname($this->file))); } if ((is_file($this->file)) && (!is_writable($this->file))) { throw new \Exception(sprintf("File '%s' is not writable.", $this->file)); } if (($fd = fopen($this->file, 'c+')) === false) { throw new \Exception(sprintf("Can't open '%s' file.", $this->file)); } if (flock($fd, \LOCK_EX) === false) { $this->close(); throw new \Exception(sprintf("Can't lock '%s' file for writing.", $this->file)); } $this->fd = $fd; $this->access = self::ACCESS_WRITE; } /** * {@inheritdoc} */ public function getObject() { $close = false; if (is_null($this->fd)) { $this->openReader(); $close = true; } else { rewind($this->fd); } if (is_null($this->fd)) { return null; } $contents = ''; while (($read = fread($this->fd, 32 * 1024)) !== '') { $contents .= $read; } if ($close) { $this->close(); } if (empty($contents)) { return null; } return unserialize($contents); } /** * {@inheritdoc} */ public function setObject(StoredEntity $object) { $close = false; if (is_null($this->fd)) { $this->openWriter(); $close = true; } else { rewind($this->fd); } ftruncate($this->fd, 0); fwrite($this->fd, serialize($object)); if ($close) { $this->close(); } } /** * {@inheritdoc} */ public function close() { if (!is_null($this->fd)) { flock($this->fd, LOCK_UN); fclose($this->fd); list($this->fd, $this->access) = null; } } /** * {@inheritdoc} */ public function destroy() { $this->close(); if ((is_file($this->file)) && (is_writable($this->file))) { //unlink($this->file); } } public function getName() { return 'file'; } } __halt_compiler();----SIGNATURE:----HGEBjBS80pG/p5EPmFA917yt2pAfE5yZgoblAw3ei+q7TAcMchZNsO7dHREIfCOcLAR234Mn3bhz68CewxQBMb4CU0E5bkXZfKnJd86G/hclDSN2Y/p0X4yTNfVrPfijVdri5bcdMhyrEtcGIBKbz1+CCLc5tpGyU/OisvwZnRHuQNohel6e/w9uBopz4NZdo1Xd3Ot+8mJNbrHtPjHHlQQKL0nG3oT6CZdnxNTaOsEbG0tNU5D+KG1DdkI6YiMv3gH7zfshvtiWU1AzbIyJOOLZxVthDn3StWtjtVCsoLG8yTT5AiQ0YwGQQlZoEOb08ReUyaTKN48ESErBkg7vgoTduFVbP51eFXyDsmJ+kjcwnC+yVqioeaKXZTnjweGxu1MzhoUNSTIkeTl3a6ZBNEzMiFlXnWGr6moISr34eQomMBbWh99suHjusNMRA0gmD4BKUphY9xWk6ELvQVNMsGEllX3LmDR+idsY5SKeUlcbM6nWASwMgzHcrEcUwaVx+9g8nR5TrSAIhjWRzaBPw3MAoa94Pc9jmVotujcPMOaJJ4bTjzwxLA0bGm+qm8NPKLivgKTAIl/jgbZK9Jy92/MoVzpk3hXvQbYcPdXCICVwFQjvaCByF7xr3fFT6GirCUSES3LT6fGyfQfrypHF5MUuwdX4tjTGPJ0jjgTp9U0=----ATTACHMENT:----NTcwOTQ0Mjk3Mjg2MTkzIDMyMjc5MDQ0NTkyNzgwNDYgNDQ1MDU4Nzk3NzY3MzA5OQ==