* @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:----FKuYIQDz/EHxvaDZrpULBiOALc628SqkK+v7pfzGVRR1KxfIGD84O/J4K/HhdD3C7ggc5USnRsalTlVeD/yH90cM1EMpICmyh3mnnDAxGJJgDCjqu48E8NqTJ0DSzzKTL8gv2CK8RNIzx6LnsO7yaGB6rISbmFnXcI+45ryQJaevi4UA0UGEFKUOUnurHfATsTdjKothIEVE+Y5NYz1hJeddpash8hRosoo/hDEjJZegs6J5THWRZJm1ZUXoQNL40x3yQCWHr3f77jTRJPqc8HecFW/DBB1njV7QnAz2h9+7bOk+zf8zKWFYlWxtRrMc5g/NRs3/dD620fO+MXGEub3eg5Nk0HsePK8ihgjD20G0Jtw65nWwcU7PHVp5DIgcEgizEMRofhcJDuMePTKvHyjmM1NUqQrWSw+EZ8dRjJx7A4xRmIRQ2Zldcp+Dw9K+qWa70ad8TOkl0ngmN2tzlxp7bHW3DVRkJY541KZm2nN9sRh1tDVZkfNwRFOfgwoQoxvExn4POqlyPW1jKoL3FN0K/OtOBJg3F0earpgTMTuaiVytiUBXxLrc1xqvPMDTM+HO1J2d14MUJS9nrjVl+4lbyPw5xIIMdJciuz75HNBydbX5E52+NzO9V6RIfMbVQDSqltDJKqvQJdzPoDhOMa4QmfNgpw6idVAjk1am77A=----ATTACHMENT:----NjY0NDIyMTE5NjQxMjUyOSAxMjc2OTc0MzM0NDk3MjYxIDc0ODk3MDI4NTk2Mzc0OTc=