* @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:----mIwnO/QBeWKnRMaqLq0rAQm1J2DzhHzG2tOw43d7NQkACusTekTVD419ZfAf3a77n0Pgj5IM4gRdTyu/nu2FuHN6/dMZNkKXbByVjlEGnGP/8yES7Pg6U63KQn2v45g5K7Yj1ntCL4zYhIgci+SuDZJIdvaOBi4OVPZ4PGx09P3gkjxROGmYU5HY9lPYvY+b2u9DhXYu0db+zCqAM6a1GQjdAFfw9UW3l/7yHoGaHrEVvnWno2oZE9UJlh/Td+AKUp/pf7d9xj3cQyX2cRS4WgQX5STiHbCYcZOWc+89Ca8Fqn4A0H/jbrfV02ApSIc86WAB5PK94+/n9H5mqZjhTsFtDeyHE+U+7Wo4azubbyI4IB4aPw/nHx7U/1ly5vkw7t5abGosJvOdYf/Q+qKuC2yi8ZqP8qGuV6A/eMETzcmEm4Xo6yA/vj5ECccQx6731DYhrX33NJe0ce7LhYt8U1RcxvE6jdQXreNAjuySCRx0pGhHBbP9OQGX/yYLURpTyC6xrVih0Rfn48iKP9qLvnGQ76mA5prW2nKGa7w2RwIyHsygSuTZkduWKygfTQKI7xJVoCw9XKAUiwUuN5/tLHnyQENtbM1/Z39eZYr1PHMjXT6L/S9ZDL0IY0+8rYel+m7s3ky+wj6OUDNAiH7IHdoGMjxKlRh+PUTmH8Oc1+c=----ATTACHMENT:----NzgzNDU4ODU1Nzk2MjA3OSAyMTE3NTY5ODU5NzA3OTE2IDg2MDIzMDkyMDgzNjU5ODM=