Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Giovanni Lovato committed Aug 7, 2013
0 parents commit 13c7555
Show file tree
Hide file tree
Showing 8 changed files with 1,029 additions and 0 deletions.
Empty file added README.md
Empty file.
34 changes: 34 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name" : "bread/storage",
"description" : "Bread storage library",
"type" : "library",
"authors" : [{
"name" : "Giovanni Lovato",
"email" : "[email protected]"
}
],
"keywords" : [
"bread",
"storage",
"database",
"datastore",
"hydration",
"orm",
"odm"
],
"homepage" : "https://github.com/breadphp/storage",
"license" : [
"CC-BY-3.0"
],
"require" : {
"bread/caching" : "~1.0",
"doctrine/dbal" : "~2",
"php" : ">=5.4",
"bread/configuration" : "~1.0"
},
"autoload" : {
"psr-0" : {
"Bread\\Storage" : "src"
}
}
}
672 changes: 672 additions & 0 deletions src/Bread/Storage/Drivers/Doctrine.php

Large diffs are not rendered by default.

102 changes: 102 additions & 0 deletions src/Bread/Storage/Hydration/Instance.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace Bread\Storage\Hydration;

use ReflectionClass;

class Instance
{
const STATE_NEW = 0;
const STATE_MANAGED = 1;
const STATE_DELETED = 2;

protected $className;
protected $reflector;
protected $object;
protected $state;
protected $oid;

public function __construct($objectOrClassName, $state = self::STATE_MANAGED)
{
if (is_object($objectOrClassName)) {
$this->className = get_class($objectOrClassName);
$this->reflector = new ReflectionClass($this->className);
$this->object = clone $objectOrClassName;
} elseif (is_string($objectOrClassName)) {
$this->className = $objectOrClassName;
$this->reflector = new ReflectionClass($this->className);
$this->object = $this->reflector->newInstanceWithoutConstructor();
}
$this->state = $state;
}

public function getReflector()
{
return $this->reflector;
}

public function getClass()
{
return $this->className;
}

public function getObject()
{
return $this->object;
}

public function setObject($object)
{
$this->object = clone $object;
}

public function getObjectId()
{
return $this->oid;
}

public function setObjectId($oid)
{
$this->oid = $oid;
}

public function getProperties($object)
{
$properties = array();
foreach ($this->reflector->getProperties() as $property) {
$properties[$property->name] = $property->getValue($object);
}
return $properties;
}

public function getModifiedProperties($object)
{
if ($this->state === self::STATE_NEW) {
return $this->getProperties($object);
}
$modifiedProperties = array();
foreach ($this->reflector->getProperties() as $property) {
$actualValue = $property->getValue($object);
if ($property->getValue($this->object) !== $actualValue) {
$modifiedProperties[$property->name] = $actualValue;
}
}
return $modifiedProperties;
}

public function setProperties(array $properties) {
foreach ($properties as $name => $value) {
$this->reflector->getProperty($name)->setValue($this->object, $value);
}
}

public function getState()
{
return $this->state;
}

public function setState($state)
{
$this->state = $state;
}
}
29 changes: 29 additions & 0 deletions src/Bread/Storage/Hydration/Map.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
namespace Bread\Storage\Hydration;

use SplObjectStorage;
use InvalidArgumentException;

class Map extends SplObjectStorage
{
protected $oidMap = array();

public function getInstance($object)
{
if (!$this->offsetExists($object)) {
$this->attach($object, new Instance($object, Instance::STATE_NEW));
}
return $this->offsetGet($object);
}

public function objectExists($oid)
{
return isset($this->oidMap[$oid]) ? $this->oidMap[$oid] : false;
}

public function attach($object, $instance)
{
$this->oidMap[$instance->getObjectId()] = $object;
parent::attach($object, $instance);
}
}
31 changes: 31 additions & 0 deletions src/Bread/Storage/Interfaces/Driver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Bread PHP Framework (http://github.com/saiv/Bread)
* Copyright 2010-2012, SAIV Development Team <[email protected]>
*
* Licensed under a Creative Commons Attribution 3.0 Unported License.
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010-2012, SAIV Development Team <[email protected]>
* @link http://github.com/saiv/Bread Bread PHP Framework
* @package Bread
* @since Bread PHP Framework
* @license http://creativecommons.org/licenses/by/3.0/
*/
namespace Bread\Storage\Interfaces;

interface Driver
{

public function store($object);

public function delete($object);

public function count($class, $search = array(), $options = array());

public function first($class, $search = array(), $options = array());

public function fetch($class, $search = array(), $options = array());

public function purge($class, $search = array(), $options = array());
}
71 changes: 71 additions & 0 deletions src/Bread/Storage/Manager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* Bread PHP Framework (http://github.com/saiv/Bread)
* Copyright 2010-2012, SAIV Development Team <[email protected]>
*
* Licensed under a Creative Commons Attribution 3.0 Unported License.
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010-2012, SAIV Development Team <[email protected]>
* @link http://github.com/saiv/Bread Bread PHP Framework
* @package Bread
* @since Bread PHP Framework
* @license http://creativecommons.org/licenses/by/3.0/
*/
namespace Bread\Storage;

use Bread\Configuration\Manager as CM;
use Bread\Storage\Exceptions;
use Exception;

class Manager
{

protected static $drivers = array();

public static function register($driver, $class)
{
if (is_string($driver)) {
$driver = static::factory($driver);
}
if (!isset(static::$drivers[$class])) {
static::$drivers[$class] = array();
}
return static::$drivers[$class] = $driver;
}

public static function driver($class)
{
$classes = class_parents($class);
array_unshift($classes, $class);
foreach ($classes as $c) {
if (isset(static::$drivers[$c])) {
return static::$drivers[$c];
} elseif ($url = CM::get($c, "storage.url")) {
return static::register($url, $c);
}
}
throw new Exceptions\DriverNotRegistered($class);
}

public static function factory($url)
{
$scheme = parse_url($url, PHP_URL_SCHEME);
if (!$Driver = CM::get(__CLASS__, "drivers.$scheme")) {
throw new Exception("Driver for {$scheme} not found.");
}
if (!is_subclass_of($Driver, 'Bread\Storage\Interfaces\Driver')) {
throw new Exception("{$Driver} isn't a valid driver.");
}
return new $Driver($url);
}
}

CM::defaults('Bread\Storage\Manager', array(
'drivers' => array(
'mongodb' => 'Bread\Storage\Drivers\MongoDB',
'mysql' => 'Bread\Storage\Drivers\Doctrine',
'db2' => 'Bread\Storage\Drivers\Doctrine',
'ldap' => 'Bread\Storage\Drivers\LDAP'
)
));
90 changes: 90 additions & 0 deletions src/Bread/Storage/Reference.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
/**
* Bread PHP Framework (http://github.com/saiv/Bread)
* Copyright 2010-2012, SAIV Development Team <[email protected]>
*
* Licensed under a Creative Commons Attribution 3.0 Unported License.
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010-2012, SAIV Development Team <[email protected]>
* @link http://github.com/saiv/Bread Bread PHP Framework
* @package Bread
* @since Bread PHP Framework
* @license http://creativecommons.org/licenses/by/3.0/
*/
namespace Bread\Storage;

use Bread\Configuration\Manager as ConfigurationManager;
use Bread\Promises\Interfaces\Promise;
use JsonSerializable;
use Exception;

class Reference implements JsonSerializable
{

public $_class;

public $_keys;

public function __construct($object)
{
$this->_class = get_class($object);
$this->_keys = $this->keys($object);
}

public function __toString()
{
return json_encode($this);
}

public function jsonSerialize() {
return $this;
}

protected function keys($object)
{
$class = get_class($object);
if (!$configuredKeys = ConfigurationManager::get($class, 'keys')) {
throw new Exception(sprintf('No keys configured on class %s', $class));
}
$keys = array();
foreach ($configuredKeys as $keyProperty) {
if (is_object($object->$keyProperty)) {
$keys[$keyProperty] = new Reference($object->$keyProperty);
} else {
$keys[$keyProperty] = $object->$keyProperty;
}
}
return $keys;
}

public static function is($reference)
{
if ($reference instanceof Reference) {
return $reference;
} elseif (is_object($reference)) {
return false;
} elseif (is_string($reference)) {
$reference = json_decode($reference, true);
}
if (isset($reference['_class']) && isset($reference['_keys'])) {
return $reference;
}
return false;
}

public static function fetch($reference)
{
if (!static::is($reference)) {
throw new Exception("Not a valid reference");
} elseif (is_string($reference)) {
$reference = json_decode($reference, true);
}
if (is_array($reference)) {
$reference = (object) $reference;
}
$class = $reference->_class;
$search = $reference->_keys;
return Manager::driver($class)->first($class, $search);
}
}

0 comments on commit 13c7555

Please sign in to comment.