From: Michael Wallner Date: Sat, 19 Sep 2015 19:56:11 +0000 (+0200) Subject: cleanups X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=d4d84fb9b85f0216bd611946d59054a606c71960;p=m6w6%2Fpq-gateway cleanups --- diff --git a/composer.json b/composer.json index 5c87f36..e936303 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ "psr-0": { "pq\\Gateway": "lib", "pq\\Query": "lib", - "pq\\Data": "lib" + "pq\\Mapper": "lib" } }, "require-dev": { diff --git a/composer.lock b/composer.lock index 7489369..e9cdcea 100644 --- a/composer.lock +++ b/composer.lock @@ -1,37 +1,38 @@ { "_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": { @@ -59,24 +60,23 @@ "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": { @@ -103,11 +103,11 @@ "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": [], diff --git a/lib/pq/Mapper/Map.php b/lib/pq/Mapper/Map.php index 834ffb1..b63ba46 100644 --- a/lib/pq/Mapper/Map.php +++ b/lib/pq/Mapper/Map.php @@ -21,7 +21,7 @@ class Map implements MapInterface foreach ($properties as $property) { $property->setContainer($this); } - $this->objects = new ObjectCache($this); + $this->objects = new ObjectManager($this); } function getClass() { @@ -48,52 +48,7 @@ class Map implements MapInterface $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) { diff --git a/lib/pq/Mapper/MapInterface.php b/lib/pq/Mapper/MapInterface.php index 0c3430c..0e0db3a 100644 --- a/lib/pq/Mapper/MapInterface.php +++ b/lib/pq/Mapper/MapInterface.php @@ -28,6 +28,29 @@ interface MapInterface */ 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 @@ -35,7 +58,7 @@ interface MapInterface function map(Row $row); /** - * @param \pq\Mapper\Rowset $rows + * @param Rowset $rows * @return array */ function mapAll(Rowset $rows); @@ -45,4 +68,4 @@ interface MapInterface * @return Row */ function unmap($object); -} \ No newline at end of file +} diff --git a/lib/pq/Mapper/Mapper.php b/lib/pq/Mapper/Mapper.php index 725a50c..1c70f72 100644 --- a/lib/pq/Mapper/Mapper.php +++ b/lib/pq/Mapper/Mapper.php @@ -53,7 +53,7 @@ class Mapper * @return \pq\Mapper\Storage */ function createStorage($class) { - return new Storage($this, $class); + return new Storage($this->mapOf($class)); } /** diff --git a/lib/pq/Mapper/ObjectCache.php b/lib/pq/Mapper/ObjectCache.php deleted file mode 100644 index 777dd51..0000000 --- a/lib/pq/Mapper/ObjectCache.php +++ /dev/null @@ -1,133 +0,0 @@ -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 diff --git a/lib/pq/Mapper/ObjectManager.php b/lib/pq/Mapper/ObjectManager.php new file mode 100644 index 0000000..557d494 --- /dev/null +++ b/lib/pq/Mapper/ObjectManager.php @@ -0,0 +1,133 @@ +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 diff --git a/lib/pq/Mapper/Property/All.php b/lib/pq/Mapper/Property/All.php index b53efdd..0f45174 100644 --- a/lib/pq/Mapper/Property/All.php +++ b/lib/pq/Mapper/Property/All.php @@ -39,21 +39,6 @@ class All implements RefPropertyInterface $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) { @@ -72,24 +57,4 @@ class All implements RefPropertyInterface } 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 diff --git a/lib/pq/Mapper/Property/Ref.php b/lib/pq/Mapper/Property/Ref.php index 851df41..84f39f4 100644 --- a/lib/pq/Mapper/Property/Ref.php +++ b/lib/pq/Mapper/Property/Ref.php @@ -6,6 +6,7 @@ use pq\Gateway\Row; use pq\Mapper\Mapper; use pq\Mapper\RefProperty; use pq\Mapper\RefPropertyInterface; +use UnexpectedValueException; class Ref implements RefPropertyInterface { @@ -29,7 +30,13 @@ 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); @@ -44,32 +51,4 @@ class Ref implements RefPropertyInterface 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 diff --git a/lib/pq/Mapper/Storage.php b/lib/pq/Mapper/Storage.php index a27ea68..8e0fcb4 100644 --- a/lib/pq/Mapper/Storage.php +++ b/lib/pq/Mapper/Storage.php @@ -4,53 +4,37 @@ namespace pq\Mapper; 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 diff --git a/lib/pq/Mapper/StorageInterface.php b/lib/pq/Mapper/StorageInterface.php index db16df5..188318a 100644 --- a/lib/pq/Mapper/StorageInterface.php +++ b/lib/pq/Mapper/StorageInterface.php @@ -4,12 +4,7 @@ namespace pq\Mapper; 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); +}