Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save wouterj/4016844 to your computer and use it in GitHub Desktop.
Save wouterj/4016844 to your computer and use it in GitHub Desktop.
Storage Example
namespace Wj\Store\Exception;
class StorableNotFoundException extends \OutOfBoundsException
{
}
namespace Wj\Store\Storable;
/**
* This interfaces must be implemented by every object that can be stored
*
* @author Wouter J <http://wouterj.nl>
*/
interface Storable implements \Serializable
{
/**
* Gets the key on which it will be stored
*
* @return string|int $key
*/
public function getKey();
}
<?php
namespace Wj\Store\Storage;
use Wj\Store\Storable\StorableInterface;
/**
* An Interface that all Storage classes implements.
*
* @author Wouter J <http://wouterj.nl>
*/
interface StorageInterface
{
/**
* Creates a Storable.
*
* @param StorableInterface $object The Storable
*/
public function create(StorableInterface $object);
/**
* Gets a Storable.
*
* @param StorableInterface $object The Storable
*
* @return StorableInterface $object The Storable
*/
public function read(StorableInterface $object);
/**
* Updates a Storable.
*
* @param StorableInterface $object The Storable
*/
public function update(StorableInterface $object);
/**
* Deletes a Storable.
*
* @param StorableInterface $object The Storable
*/
public function delete(StorableInterface $object);
}
namespace Wj\Store\Storage\Database;
use Wj\Store\Storage\StorageInterface;
/**
* This class is the base for DatabaseStorage
*
* @author Wouter J <http://wouterj.nl>
*/
abstract class DatabaseStorage implements StorageInterface
{
// ... basis methods die bij elke adapter gelijk zijn
}
namespace Wj\Store\Storage\Database;
use Wj\Store\Storable\StorableInterface;
/**
* This class stores a Storable in a database with MySQLi
*
* @author Wouter J <http://wouterj.nl>
*/
class MySQLiDatabaseStorage extends DatabaseStorage
{
// ... MySQLi methods
}
namespace Wj\Store\Storage;
use Wj\Store\Storable\StorableInterface;
use Wj\Store\Exception\StorableNotfoundException;
/**
* This class stores the Storable in a session.
*
* @author Wouter J <http://wouterj.nl>
*/
class SessionStorage implements StorageInterface
{
public function __construct()
{
session_start();
}
/**
* {@inheritdoc}
*/
public function create(StorableInterface $object)
{
$_SESSION[$object->getKey()] = serialize($object);
}
/**
* {@inheritdoc}
*/
public function read(StorableInterface $object)
{
if (!isset($_SESSION[$object->getKey()])) {
throw new StorableNotFoundException(
sprintf(
'Storable "%s" does not exists, did you spell it correct?',
$object->getKey(),
)
);
}
return unserialize($_SESSION[$object->getKey()]);
}
/**
* {@inheritdoc}
*/
public function update(StorableInterface $object)
{
if (!isset($_SESSION[$object->getKey()])) {
throw new StorableNotFoundException(
sprintf(
'Storable "%s" does not exists, use the create method if you want to create a Storable.',
$object->getKey(),
)
);
}
$_SESSION[$object->getKey()] = serialize($object);
}
/**
* {@inheritdoc}
*/
public function delete(StorableInterface $object)
{
if (!isset($_SESSION[$object->getKey()])) {
throw new StorableNotFoundException(
sprintf(
'Storable "%s" does not exists, you can\'t delete an object if it is not created yet.',
$object->getKey(),
)
);
}
unset($_SESSION[$object->getKey()]);
}
}
namespace Wj\Store\Exception;
class StorableNotFoundException extends \OutOfBoundsException
{
}
namespace Wj\Store\Storable;
/**
* This interfaces must be implemented by every object that can be stored
*
* @author Wouter J <http://wouterj.nl>
*/
interface Storable implements \Serializable
{
/**
* Gets the key on which it will be stored
*
* @return string|int $key
*/
public function getKey();
}
<?php
namespace Wj\Store\Storage;
use Wj\Store\Storable\StorableInterface;
/**
* An Interface that all Storage classes implements.
*
* @author Wouter J <http://wouterj.nl>
*/
interface StorageInterface
{
/**
* Creates a Storable.
*
* @param StorableInterface $object The Storable
*/
public function create(StorableInterface $object);
/**
* Gets a Storable.
*
* @param StorableInterface $object The Storable
*
* @return StorableInterface $object The Storable
*/
public function read(StorableInterface $object);
/**
* Updates a Storable.
*
* @param StorableInterface $object The Storable
*/
public function update(StorableInterface $object);
/**
* Deletes a Storable.
*
* @param StorableInterface $object The Storable
*/
public function delete(StorableInterface $object);
}
namespace Wj\Store\Storage\Database;
use Wj\Store\Storage\StorageInterface;
/**
* This class is the base for DatabaseStorage
*
* @author Wouter J <http://wouterj.nl>
*/
abstract class DatabaseStorage implements StorageInterface
{
// ... basis methods die bij elke adapter gelijk zijn
}
namespace Wj\Store\Storage\Database;
use Wj\Store\Storage\DatabaseStorage;
/**
* This class stores a Storable in a database with MySQLi
*
* @author Wouter J <http://wouterj.nl>
*/
class MySQLiDatabaseStorage extends DatabaseStorage
{
// ... MySQLi methods
}
namespace Wj\Store\Storage;
use Wj\Store\Storable\StorableInterface;
use Wj\Store\Exception\StorableNotfoundException;
/**
* This class stores the Storable in a session.
*
* @author Wouter J <http://wouterj.nl>
*/
class SessionStorage implements StorageInterface
{
public function __construct()
{
session_start();
}
/**
* {@inheritdoc}
*/
public function create(StorableInterface $object)
{
$_SESSION[$object->getKey()] = serialize($object);
}
/**
* {@inheritdoc}
*/
public function read(StorableInterface $object)
{
if (!isset($_SESSION[$object->getKey()])) {
throw new StorableNotFoundException(
sprintf(
'Storable "%s" does not exists, did you spell it correct?',
$object->getKey(),
)
);
}
return unserialize($_SESSION[$object->getKey()]);
}
/**
* {@inheritdoc}
*/
public function update(StorableInterface $object)
{
if (!isset($_SESSION[$object->getKey()])) {
throw new StorableNotFoundException(
sprintf(
'Storable "%s" does not exists, use the create method if you want to create a Storable.',
$object->getKey(),
)
);
}
$_SESSION[$object->getKey()] = serialize($object);
}
/**
* {@inheritdoc}
*/
public function delete(StorableInterface $object)
{
if (!isset($_SESSION[$object->getKey()])) {
throw new StorableNotFoundException(
sprintf(
'Storable "%s" does not exists, you can\'t delete an object if it is not created yet.',
$object->getKey(),
)
);
}
unset($_SESSION[$object->getKey()]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment