Created
November 5, 2012 11:51
-
-
Save wouterj/4016844 to your computer and use it in GitHub Desktop.
Storage Example
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
namespace Wj\Store\Exception; | |
class StorableNotFoundException extends \OutOfBoundsException | |
{ | |
} |
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
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(); | |
} |
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 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); | |
} |
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
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 | |
} |
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
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 | |
} |
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
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()]); | |
} | |
} |
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
namespace Wj\Store\Exception; | |
class StorableNotFoundException extends \OutOfBoundsException | |
{ | |
} |
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
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(); | |
} |
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 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); | |
} |
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
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 | |
} |
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
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 | |
} |
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
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