Skip to content

Instantly share code, notes, and snippets.

@nerdsrescueme
Created October 3, 2011 19:16
Show Gist options
  • Save nerdsrescueme/1259964 to your computer and use it in GitHub Desktop.
Save nerdsrescueme/1259964 to your computer and use it in GitHub Desktop.
Storage and Encoding
<?php
namespace Atom\Encoder;
use Atom\Encodable;
class Base64 implements Encodable {
public function decode($data)
{
return base64_decode(unserialize($data));
}
public function encode($data)
{
return base64_encode(serialize($data));
}
}
/* End of file: base64.php */
<?php
namespace Atom;
interface Encodable {
public function decode($data);
public function encode($data);
}
/* End of file: encodable.php */
<?php
namespace Atom;
class Encoder {
public static function get($encodable)
{
try
{
// @TODO Allow other namespaces.
$encoder = new '\\Atom\\Encoder\\'.ucfirst($encodable);
}
catch(\Exception $e)
{
throw new \OutOfBoundsException("Encoder [$encodable] does not exist.");
}
// @TODO New namespaces added, add instanceof \Atom\Encodable check
return $encoder;
}
}
/* End of file: encoder.php */
<?php
namespace Atom\Storage;
use Atom\Storable;
class File implements Storable {
protected $_path;
private $_append = false;
public function __construct($path, $create = false)
{
if(!file_exists($path))
{
if(!$create)
{
throw new \OutOfBoundsException('The file ['.str_replace(LIBRARY_PATH, 'LIBRARY_PATH', $path).'] does not exist.');
}
if(!touch($path))
{
throw new \Exception('The file ['.str_replace(LIBRARY_PATH, 'LIBRARY_PATH', $path).'] could not be created.');
}
}
$this->_path = $path;
}
public function append($data)
{
$this->_append = true;
return $this->write($data);
}
public function delete()
{
return unlink($this->path);
}
public function read()
{
return file_get_contents($this->_path);
}
public function write($data)
{
if(!is_writable($this->_path))
{
throw new \OutOfBoundsException('The file ['.str_replace(LIBRARY_PATH, 'LIBRARY_PATH', $this->_path).'] is not writable.');
}
$flags = LOCK_EX;
if($this->_append)
{
$flags = LOCK_EX | FILE_APPEND;
$this->_append = false; // Reset append
}
return file_put_contents($this->path, $data, $flags);
}
}
<?php
namespace Atom\Encoder;
use Atom\Encodable;
class Json implements Encodable {
public function decode($data)
{
return json_decode($data);
}
public function encode($data)
{
return json_encode($data);
}
}
/* End of file: json.php */
<?php
namespace Atom\Encoder;
use Atom\Encodable;
class Passthrough implements Encodable {
public function decode($data)
{
return $data;
}
public function encode($data)
{
return $data;
}
}
/* End of file: passthrough.php */
<?php
namespace Atom\Encoder;
use Atom\Encodable;
class Serialize implements Encodable {
public function decode($data)
{
return unserialize($data);
}
public function encode($data)
{
return serialize($data);
}
}
/* End of file: serialize.php */
<?php
namespace Atom;
interface Storable {
public function __construct($identifier, $create);
public function append($data);
public function delete();
public function read();
public function write($data);
}
/* End of file storable.php */
<?php
namespace Atom;
class Storage {
public static function get($storable, $identifier, $create = false)
{
try
{
// @TODO Allow other namespaces.
$storage = '\\Atom\\Storage\\'.ucfirst($storable);
$storage = new $storage($identifier, $create);
}
catch(\Exception $e)
{
throw new \OutOfBoundsException("Storage type [$storable] does not exist.");
}
// @TODO Namespaces added, add instanceof \Atom\Storable check.
return $storage;
}
}
/* End of file: storage.php */
@nerdsrescueme
Copy link
Author

Usage

    $storage = \Atom\Storage::get('file', 'path/to/file.ext');
    $value = $storage->read();

    $encoded_value = \Atom\Encoder::get('json')->encode($value);

    $storage->append('New value to the end of the file.');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment