"psr-0": {
"pq\\Gateway": "lib",
"pq\\Query": "lib",
- "pq\\Data": "lib"
+ "pq\\Mapper": "lib"
}
},
"require-dev": {
{
"_readme": [
"This file locks the dependencies of your project to a known state",
- "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
+ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
- "hash": "813817d14a924e8b4189c1365120cf86",
+ "hash": "206c8b9f89c5af3066d5ecc320c866be",
+ "content-hash": "2106329c981b9ba6675481473db7f850",
"packages": [],
"packages-dev": [
{
"name": "amphp/amp",
- "version": "v1.0.0-beta4",
+ "version": "v1.0.4",
"source": {
"type": "git",
"url": "https://github.com/amphp/amp.git",
- "reference": "9fa6010f192f82a81381ae2dd372e1e75107d332"
+ "reference": "880c965fbdad9d6bcc382b7484c00173ee17df13"
},
"dist": {
"type": "zip",
- "url": "https://api.github.com/repos/amphp/amp/zipball/9fa6010f192f82a81381ae2dd372e1e75107d332",
- "reference": "9fa6010f192f82a81381ae2dd372e1e75107d332",
+ "url": "https://api.github.com/repos/amphp/amp/zipball/880c965fbdad9d6bcc382b7484c00173ee17df13",
+ "reference": "880c965fbdad9d6bcc382b7484c00173ee17df13",
"shasum": ""
},
"require": {
"php": ">=5.5"
},
"require-dev": {
- "phpunit/phpunit": "~4.4.0"
+ "fabpot/php-cs-fixer": "~1.9",
+ "phpunit/phpunit": "~4.8"
},
"type": "library",
"extra": {
"branch-alias": {
- "dev-master": "2.0.0-dev",
- "dev-v1.0.x": "1.0.0-dev"
+ "dev-master": "1.0.x-dev"
}
},
"autoload": {
"async",
"concurrency",
"event",
- "future",
"non-blocking",
"promise"
],
- "time": "2015-05-20 03:12:32"
+ "time": "2015-09-10 21:19:43"
},
{
"name": "react/promise",
- "version": "v2.2.0",
+ "version": "v2.2.1",
"source": {
"type": "git",
"url": "https://github.com/reactphp/promise.git",
- "reference": "365fcee430dfa4ace1fbc75737ca60ceea7eeeef"
+ "reference": "3b6fca09c7d56321057fa8867c8dbe1abf648627"
},
"dist": {
"type": "zip",
- "url": "https://api.github.com/repos/reactphp/promise/zipball/365fcee430dfa4ace1fbc75737ca60ceea7eeeef",
- "reference": "365fcee430dfa4ace1fbc75737ca60ceea7eeeef",
+ "url": "https://api.github.com/repos/reactphp/promise/zipball/3b6fca09c7d56321057fa8867c8dbe1abf648627",
+ "reference": "3b6fca09c7d56321057fa8867c8dbe1abf648627",
"shasum": ""
},
"require": {
"authors": [
{
"name": "Jan Sorgalla",
- "email": "jsorgalla@googlemail.com"
+ "email": "jsorgalla@gmail.com"
}
],
"description": "A lightweight implementation of CommonJS Promises/A for PHP",
- "time": "2014-12-30 13:32:42"
+ "time": "2015-07-03 13:48:55"
}
],
"aliases": [],
foreach ($properties as $property) {
$property->setContainer($this);
}
- $this->objects = new ObjectCache($this);
+ $this->objects = new ObjectManager($this);
}
function getClass() {
$this->properties[] = $property;
return $this;
}
-/*
- function idOf(Row $row, $check = false) {
- $identity = $row->getIdentity();
- if (is_scalar($identity)) {
- return $identity;
- }
-
- if ($check && !isset($identity)) {
- return false;
- }
-
- if (is_array($identity)) {
- if ($check && array_search(null, $identity, true)) {
- return false;
- }
- /* one level is better than no level * /
- asort($identity);
- }
- return json_encode($identity);
- }
-
- function objectOf(Row $row) {
- $id = $this->idOf($row);
-
- if (isset($this->objects["obj"][$id])) {
- $obj = $this->objects["obj"][$id];
- } else {
- $obj = new $this->class;
- $this->objects["obj"][$id] = $obj;
- $this->objects["row"][spl_object_hash($obj)] = $row;
- }
- return $obj;
- }
- function rowOf($object) {
- $id = spl_object_hash($object);
-
- if (isset($this->objects["row"][$id])) {
- $row = $this->objects["row"][$id];
- } else {
- $row = new Row($this->gateway);
- $this->objects["row"][$id] = $row;
- }
- return $row;
- }
-*/
function allOf(Row $row, $refName, &$objects = null) {
/* apply objectOf to populate the object cache */
return $this->gateway->of($row, $refName)->apply(function($row) use(&$objects) {
*/
function addProperty(PropertyInterface $property);
+ /**
+ * @param Row $row
+ * @param string $refName
+ * @param array $objects
+ * @return Rowset
+ */
+ function allOf(Row $row, $refName, &$objects = null);
+
+ /**
+ * @param Row $row
+ * @param string $refName
+ * @param array $objects
+ * @return Rowset
+ */
+ function refOf(Row $row, $refName, &$objects = null);
+
+ /**
+ * @param MapInterface $map origin
+ * @param string $refName relations reference name
+ * @return array relation reference
+ */
+ function relOf(MapInterface $map, $refName);
+
/**
* @param Row $row
* @return object
function map(Row $row);
/**
- * @param \pq\Mapper\Rowset $rows
+ * @param Rowset $rows
* @return array
*/
function mapAll(Rowset $rows);
* @return Row
*/
function unmap($object);
-}
\ No newline at end of file
+}
* @return \pq\Mapper\Storage
*/
function createStorage($class) {
- return new Storage($this, $class);
+ return new Storage($this->mapOf($class));
}
/**
+++ /dev/null
-<?php
-
-namespace pq\Mapper;
-
-use OutOfBoundsException;
-use pq\Exception\BadMethodCallException;
-use pq\Gateway\Row;
-
-class ObjectCache
-{
- private $map;
- private $obj = [];
- private $row = [];
-
- function __construct(MapInterface $map) {
- $this->map = $map;
- }
-
- function reset() {
- $this->obj = [];
- $this->row = [];
- }
-
- function rowId(Row $row, $check = false) {
- try {
- $identity = $row->getIdentity();
- } catch (OutOfBoundsException $e) {
- return false;
- }
- return $this->serializeRowId($identity, $check);
- }
-
- function objectId($object) {
- return spl_object_hash($object);
- }
-
- function extractRowId($object) {
- $id = [];
- foreach ($this->map->getGateway()->getIdentity() as $col) {
- foreach ($this->map->getProperties() as $property) {
- if ($property->exposes($col)) {
- $id[$col] = $property->extract($object);
- }
- }
- }
- return $this->serializeRowId($id, true);
- }
-
- function serializeRowId($identity, $check = false) {
- if (is_scalar($identity)) {
- return $identity;
- }
-
- if ($check && !isset($identity)) {
- return false;
- }
-
- if (is_array($identity)) {
- if ($check && array_search(null, $identity, true)) {
- return false;
- }
- /* one level is better than no level */
- asort($identity);
- }
- return json_encode($identity);
- }
-
- function hasObject($row_id) {
- return isset($this->obj[$row_id]);
- }
-
- function createObject(Row $row) {
- $rid = $this->rowId($row);
- $cls = $this->map->getClass();
- $obj = new $cls;
- $oid = $this->objectId($obj);
- $this->obj[$rid] = $obj;
- $this->row[$oid] = $row;
- return $obj;
- }
-
- function resetObject(Row $row) {
- unset($this->obj[$this->rowId($row)]);
- }
-
- function getObject(Row $row) {
- $id = $this->rowId($row);
- return $this->getObjectById($id);
- }
-
- function getObjectById($row_id) {
- if (!$this->hasObject($row_id)) {
- throw new BadMethodCallException("Object of row with id $row_id does not exist");
- }
- return $this->obj[$row_id];
- }
-
- function asObject(Row $row){
- return $this->hasObject($this->rowId($row))
- ? $this->getObject($row)
- : $this->createObject($row);
- }
-
- function hasRow($obj_id) {
- return isset($this->row[$obj_id]);
- }
-
- function createRow($object) {
- $oid = $this->objectId($object);
- $row = new Row($this->map->getGateway());
- $this->row[$oid] = $row;
- return $row;
- }
-
- function resetRow($object) {
- unset($this->row [$this->objectId($object)]);
- }
-
- function getRow($object) {
- $id = $this->objectId($object);
-
- if (!$this->hasRow($id)) {
- throw new BadMethodCallException("Row for object with id $id does not exist");
- }
- return $this->row[$id];
- }
-
- function asRow($object) {
- return $this->hasRow($this->objectId($object))
- ? $this->getRow($object)
- : $this->createRow($object);
- }
-}
\ No newline at end of file
--- /dev/null
+<?php
+
+namespace pq\Mapper;
+
+use OutOfBoundsException;
+use pq\Exception\BadMethodCallException;
+use pq\Gateway\Row;
+
+class ObjectManager
+{
+ private $map;
+ private $obj = [];
+ private $row = [];
+
+ function __construct(MapInterface $map) {
+ $this->map = $map;
+ }
+
+ function reset() {
+ $this->obj = [];
+ $this->row = [];
+ }
+
+ function rowId(Row $row, $check = false) {
+ try {
+ $identity = $row->getIdentity();
+ } catch (OutOfBoundsException $e) {
+ return false;
+ }
+ return $this->serializeRowId($identity, $check);
+ }
+
+ function objectId($object) {
+ return spl_object_hash($object);
+ }
+
+ function extractRowId($object) {
+ $id = [];
+ foreach ($this->map->getGateway()->getIdentity() as $col) {
+ foreach ($this->map->getProperties() as $property) {
+ if ($property->exposes($col)) {
+ $id[$col] = $property->extract($object);
+ }
+ }
+ }
+ return $this->serializeRowId($id, true);
+ }
+
+ function serializeRowId($identity, $check = false) {
+ if (is_scalar($identity)) {
+ return $identity;
+ }
+
+ if ($check && !isset($identity)) {
+ return false;
+ }
+
+ if (is_array($identity)) {
+ if ($check && array_search(null, $identity, true)) {
+ return false;
+ }
+ /* one level is better than no level */
+ asort($identity);
+ }
+ return json_encode($identity);
+ }
+
+ function hasObject($row_id) {
+ return isset($this->obj[$row_id]);
+ }
+
+ function createObject(Row $row) {
+ $rid = $this->rowId($row);
+ $cls = $this->map->getClass();
+ $obj = new $cls;
+ $oid = $this->objectId($obj);
+ $this->obj[$rid] = $obj;
+ $this->row[$oid] = $row;
+ return $obj;
+ }
+
+ function resetObject(Row $row) {
+ unset($this->obj[$this->rowId($row)]);
+ }
+
+ function getObject(Row $row) {
+ $id = $this->rowId($row);
+ return $this->getObjectById($id);
+ }
+
+ function getObjectById($row_id) {
+ if (!$this->hasObject($row_id)) {
+ throw new BadMethodCallException("Object of row with id $row_id does not exist");
+ }
+ return $this->obj[$row_id];
+ }
+
+ function asObject(Row $row){
+ return $this->hasObject($this->rowId($row))
+ ? $this->getObject($row)
+ : $this->createObject($row);
+ }
+
+ function hasRow($obj_id) {
+ return isset($this->row[$obj_id]);
+ }
+
+ function createRow($object) {
+ $oid = $this->objectId($object);
+ $row = new Row($this->map->getGateway());
+ $this->row[$oid] = $row;
+ return $row;
+ }
+
+ function resetRow($object) {
+ unset($this->row [$this->objectId($object)]);
+ }
+
+ function getRow($object) {
+ $id = $this->objectId($object);
+
+ if (!$this->hasRow($id)) {
+ throw new BadMethodCallException("Row for object with id $id does not exist");
+ }
+ return $this->row[$id];
+ }
+
+ function asRow($object) {
+ return $this->hasRow($this->objectId($object))
+ ? $this->getRow($object)
+ : $this->createRow($object);
+ }
+}
\ No newline at end of file
$map->unmap($ref);
}
};
-
- if (!$this->container->getObjects()->rowId($rowToUpdate, true)) {
- return [$this, "write"];
- } else {
- /* $object = User */
- /* $refs = array(Email) */
- /* $property = Property\Ref(Email::$user)->to(User)->by("email_user") */
- /* now update array(Email) with id of User, i.e. $ref->user_id = $object->id */
- $map = $this->mapper->mapOf($this->refClass);
- $refs = $this->extract($object);
- foreach ($refs as $ref) {
- $property->assign($ref, $object);
- $map->unmap($ref);
- }
- }
}
private function findRefProperty($object) {
}
return current($property);
}
-
- function read2(RowGateway $row) {
- #echo __METHOD__." ".$this;
- $ref = $this->getRefMap()->ref($row, $this->refName);
- $value = $this->mapper->map($ref, $this->refClass);
- return [$this->property => $value];
- }
-
- function write2($object) {
- #echo __METHOD__." ".$this;
- $value = $this->extract($object);
- foreach ($value as $ref) {
- $this->mapper->queue(function() use(&$object, &$ref) {
- $map = $this->getRefMap()->getRefMapping($this->refName);
- $map->assign($ref, $object);
- $this->mapper->unmap($ref, $this->getRefMap());
- });
- }
- return [];
- }
}
\ No newline at end of file
use pq\Mapper\Mapper;
use pq\Mapper\RefProperty;
use pq\Mapper\RefPropertyInterface;
+use UnexpectedValueException;
class Ref implements RefPropertyInterface
{
function write($object, Row $rowToUpdate) {
$map = $this->mapper->mapOf($this->refClass);
$ref = $this->extract($object);
- $rel = $map->relOf($this->container, $this->refName);
+ if (!$rel = $map->relOf($this->container, $this->refName)) {
+ throw new UnexpectedValueException(
+ sprintf("Unrelated reference from %s to %s with name %s",
+ $this->container->getGateway()->getName(),
+ $map->getGateway()->getName(),
+ $this->refName));
+ }
foreach ($rel as $fgn => $col) {
foreach ($this->findFieldProperty($col) as $property) {
$value = $property->extract($ref);
return $property->exposes($col);
});
}
-
-
- function read2(RowGateway $row) {
- #echo __METHOD__." ".$this;
- $map = $this->getRefMap();
- $rel = $this->container->getGateway()->getRelation(
- $map->getGateway()->getName(), $this->refName);
- $key = array_combine($rel->referencedColumns, array_map(function($c) use($row) {
- return $row->$c->get();
- }, $rel->foreignColumns));
- if (($obj = $this->mapper->objectOfRowId($this->refClass, $key))) {
- yield $this->property => $obj;
- } else foreach ($map->getGateway()->by($row, $this->refName) as $row) {
- yield $this->property => $this->mapper->objectOf($this->refClass, $row);
- }
- }
-
- function write2($object) {
- #echo __METHOD__." ".$this;
- $map = $this->getRefMap();
- $rel = $this->container->getGateway()->getRelation(
- $map->getGateway()->getName(), $this->refName);
- $ref = $this->extract($object);
- foreach ($rel as $fgn => $col) {
- $fld = $map->getFieldMapping($col);
- yield $fgn => $fld->extract($ref);
- }
- }
}
\ No newline at end of file
class Storage implements StorageInterface
{
- /**
- * @var string
- */
- private $class;
-
- /**
- * @var \pq\Mapper\Mapper
- */
- private $mapper;
-
/**
*
* @var pq\Mapper\MapInterface
*/
- private $mapping;
+ private $map;
/**
* @var \pq\Gateway\Table
*/
private $gateway;
- function __construct(Mapper $mapper, $class) {
- $this->class = $class;
- $this->mapper = $mapper;
- $this->mapping = $mapper->mapOf($class);
- $this->gateway = $this->mapping->getGateway();
- }
-
- function getMapper() {
- return $this->mapper;
+ function __construct(MapInterface $map) {
+ $this->map = $map;
+ $this->gateway = $map->getGateway();
}
function find($where = [], $order = null, $limit = null, $offset = null) {
/* @var pq\Gateway\Rowset $rowset */
$rowset = $this->gateway->find($where, $order, $limit, $offset);
- return $this->mapping->mapAll($rowset);
+ return $this->map->mapAll($rowset);
}
function delete($object) {
- $cache = $this->mapping->getObjects();
+ $cache = $this->map->getObjects();
$row = $cache->asRow($object)->delete();
$cache->resetObject($row);
$cache->resetRow($object);
}
- function persist($object) {
- $this->mapping->unmap($object);
+ function save($object) {
+ $this->map->unmap($object);
}
}
\ No newline at end of file
interface StorageInterface
{
- /**
- * @return pq\Mapper\Mapper
- */
- function getMapper();
-
function find($where, $order = null, $limit = null, $offset = null);
function delete($object);
- function persist($object);
-}
\ No newline at end of file
+ function save($object);
+}