Permalink
Cannot retrieve contributors at this time
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
62 lines (51 sloc)
1.45 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Rubix\Engine\Persisters; | |
use InvalidArgumentException; | |
use RuntimeException; | |
class Filesystem implements Persister | |
{ | |
const MODE = 'wb+'; | |
/** | |
* The path to the file in the filesystem. | |
* | |
* @var string | |
*/ | |
protected $path; | |
/** | |
* @param string $path | |
* @return void | |
*/ | |
public function __construct(string $path) | |
{ | |
if (!is_writable(dirname($path))) { | |
throw new InvalidArgumentException('Folder does not exist or is not writeable.'); | |
} | |
$this->path = $path; | |
} | |
/** | |
* Save the persisable object to the Filesystem. Returns true on success, false on error. | |
* | |
* @return bool | |
*/ | |
public function save(Persistable $persistable) : bool | |
{ | |
return file_put_contents($this->path, serialize($persistable), LOCK_EX) ? true : false; | |
} | |
/** | |
* Restore the persistable object from the filesystem. | |
* | |
* @throws \RuntimeException | |
* @return \Rubix\Engine\Persistable | |
*/ | |
public function load() : Persistable | |
{ | |
if (!file_exists($this->path) || !is_readable($this->path)) { | |
throw new RuntimeException('File ' . basename($path) . ' cannot be opened.'); | |
} | |
$persistable = unserialize(file_get_contents($this->path)); | |
if ($persistable === false) { | |
throw new RuntimeException('Object could not be reconstituted.'); | |
} | |
return $persistable; | |
} | |
} |