From d4d84fb9b85f0216bd611946d59054a606c71960 Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Sat, 19 Sep 2015 21:56:11 +0200 Subject: [PATCH] cleanups --- composer.json | 2 +- composer.lock | 34 +++++++------- lib/pq/Mapper/Map.php | 47 +------------------ lib/pq/Mapper/MapInterface.php | 27 ++++++++++- lib/pq/Mapper/Mapper.php | 2 +- .../{ObjectCache.php => ObjectManager.php} | 2 +- lib/pq/Mapper/Property/All.php | 35 -------------- lib/pq/Mapper/Property/Ref.php | 37 ++++----------- lib/pq/Mapper/Storage.php | 32 ++++--------- lib/pq/Mapper/StorageInterface.php | 9 +--- 10 files changed, 64 insertions(+), 163 deletions(-) rename lib/pq/Mapper/{ObjectCache.php => ObjectManager.php} (99%) 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/ObjectManager.php similarity index 99% rename from lib/pq/Mapper/ObjectCache.php rename to lib/pq/Mapper/ObjectManager.php index 777dd51..557d494 100644 --- a/lib/pq/Mapper/ObjectCache.php +++ b/lib/pq/Mapper/ObjectManager.php @@ -6,7 +6,7 @@ use OutOfBoundsException; use pq\Exception\BadMethodCallException; use pq\Gateway\Row; -class ObjectCache +class ObjectManager { private $map; private $obj = []; 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); +} -- 2.30.2