Maslosoft Embedi API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
<?php
/**
* This software package is licensed under `AGPL, Commercial` license[s].
*
* @package maslosoft/embedi
* @license AGPL, Commercial
*
* @copyright Copyright (c) Peter Maselkowski <pmaselkowski@gmail.com>
*
*/
namespace Maslosoft\EmbeDi;
use Maslosoft\EmbeDi\Interfaces\MassAssignedInterface;
/**
* StoragePersister
*
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
class StoragePersister
{
/**
* Mass assigned storage interface
* @var MassAssignedInterface
*/
private $storage = null;
/**
* Peristence path
* @var string
*/
private $path = '';
public function __construct(MassAssignedInterface $storage, $path)
{
$this->storage = $storage;
$this->path = $path;
}
/**
* Save data to disk
*/
public function save()
{
$data = $this->storage->getAll();
$code = sprintf("<?php\n\nreturn %s;", var_export($data, true));
file_put_contents($this->_getFileName(), $code);
}
/**
* Read data from disk
*/
public function load()
{
// file exists not used here as it will exists in next run anyway
$data = (array)@include $this->_getFileName();
$this->storage->setAll($data);
}
/**
* Get storage filename
* @return string
*/
private function _getFileName()
{
return rtrim($this->path, '\\/') . '/' . str_replace('\\', '.', sprintf('%s-%s.php', get_class($this), get_class($this->storage)));
}
}
API documentation generated by ApiGen