From 8920024065009aeb9c1bcf91af61ccd4ceb4a110 Mon Sep 17 00:00:00 2001 From: therselman Date: Wed, 23 Aug 2017 20:29:27 +0200 Subject: [PATCH] --- src/EntityManager.php | 145 ------------------ src/{ => ORM}/Collection.php | 4 +- src/{ => ORM}/Entity.php | 2 +- .../EntityManager.php} | 15 +- src/ORM/Repository.php | 131 ++++++++++++++++ 5 files changed, 137 insertions(+), 160 deletions(-) delete mode 100644 src/EntityManager.php rename src/{ => ORM}/Collection.php (90%) rename src/{ => ORM}/Entity.php (93%) rename src/{EntityRepository.php => ORM/EntityManager.php} (73%) create mode 100644 src/ORM/Repository.php diff --git a/src/EntityManager.php b/src/EntityManager.php deleted file mode 100644 index 743a476..0000000 --- a/src/EntityManager.php +++ /dev/null @@ -1,145 +0,0 @@ -properties =& $properties; - } - - - /** - * {@inheritDoc} - */ - public function getConnection() - { - return $this->conn; - } - - - /** - * {@inheritDoc} - */ - public function beginTransaction() - { - $this->conn->beginTransaction(); - } - - - /** - * {@inheritDoc} - */ - public function transaction($func) // AKA `transactional` in Doctrine - { - if (!is_callable($func)) { - throw new \InvalidArgumentException('Expected argument of type "callable", got "' . gettype($func) . '"'); - } - $this->conn->beginTransaction(); - try { - $return = call_user_func($func, $this); - $this->flush(); - $this->conn->commit(); - return $return ?: true; - } catch (Exception $e) { - $this->close(); - $this->conn->rollBack(); - throw $e; - } - } - - - /** - * {@inheritDoc} - */ - public function commit() - { - $this->conn->commit(); - } - - - /** - * {@inheritDoc} - */ - public function rollback() - { - $this->conn->rollBack(); - } - - - /** - * Get entity field/property - * - * @param string $name - * @return mixed - */ - public function __get($name) - { - $value = $this->properties[$name]; - return is_callable($value) ? $value($this) : $value; - } - - /** - * Set entity field/property - * - * @param string $name - * @param mixed $value - * @return void - */ - public function __set($name, $value) - { - $this->properties[$name] = $value; - } - - - public function __isset($name) - { - return isset($this->properties[$name]); - } - public function __unset($name) - { - unset($this->properties[$name]); - } - - - public function __call($method, ...$args) - { - array_unshift($args, $this); - return call_user_func($this->properties[$method], ...$args); - } - public function __invoke() - { - return $this->properties['__invoke']($this); - } - public function setMethod($method, callable $callable) - { - $this->properties[$method] = $callable; - return $this; - } -} diff --git a/src/Collection.php b/src/ORM/Collection.php similarity index 90% rename from src/Collection.php rename to src/ORM/Collection.php index acefae2..560fab0 100644 --- a/src/Collection.php +++ b/src/ORM/Collection.php @@ -5,10 +5,10 @@ * Similar functionality to an Array or Dictionary class * Typically holding a collection/array of Entity members * This class is not particularly useful compared to a standard array, - * but it's used to create `extended` functionality of standard arrays. + * but it's used to `extend` the functionality of standard arrays. */ -namespace Twister; +namespace Twister\ORM; class Collection implements \Iterator, \Countable, \ArrayAccess { diff --git a/src/Entity.php b/src/ORM/Entity.php similarity index 93% rename from src/Entity.php rename to src/ORM/Entity.php index 9308d6d..ab66bd3 100644 --- a/src/Entity.php +++ b/src/ORM/Entity.php @@ -5,7 +5,7 @@ * Properties can include callable functions, which can `lazy load` properties, or return dynamic/complex/calculated properties */ -namespace Twister; +namespace Twister\ORM; class Entity { diff --git a/src/EntityRepository.php b/src/ORM/EntityManager.php similarity index 73% rename from src/EntityRepository.php rename to src/ORM/EntityManager.php index 851d1b7..afa94e2 100644 --- a/src/EntityRepository.php +++ b/src/ORM/EntityManager.php @@ -1,17 +1,8 @@ - */ -class EntityRepository +namespace Twister\ORM; + +class EntityManager { /** * The database connection used by the EntityManager. diff --git a/src/ORM/Repository.php b/src/ORM/Repository.php new file mode 100644 index 0000000..606f0ad --- /dev/null +++ b/src/ORM/Repository.php @@ -0,0 +1,131 @@ + + */ +class Repository +{ + /** + * The database connection used by the EntityManager. + * + * @var \Doctrine\DBAL\Connection + */ + protected $em = null; + + + public function __construct(Container $c) + { + $this->conn = $c->db; + } + + + /** + * {@inheritDoc} + */ + public function getConnection() + { + return $this->conn; + } + + + /** + * {@inheritDoc} + */ + public function getRepository($entityName) + { + static $repos = null; + if ( ! isset($repos[$entityName])) { + $repoName = $entityName . 'Repository'; +echo 'loading: ' . $repoName; + $repos[$entityName] = new $repoName($this); + } + return $repos[$entityName]; + } + + + /** + * {@inheritDoc} + */ + public function beginTransaction() + { + $this->conn->beginTransaction(); + } + + + /** + * {@inheritDoc} + */ + public function transaction($func) // AKA `transactional` in Doctrine + { + if (!is_callable($func)) { + throw new \InvalidArgumentException('Expected argument of type "callable", got "' . gettype($func) . '"'); + } + $this->conn->beginTransaction(); + try { + $return = call_user_func($func, $this); + $this->flush(); + $this->conn->commit(); + return $return ?: true; + } catch (Exception $e) { + $this->close(); + $this->conn->rollBack(); + throw $e; + } + } + + + /** + * {@inheritDoc} + */ + public function commit() + { + $this->conn->commit(); + } + + + /** + * {@inheritDoc} + */ + public function rollback() + { + $this->conn->rollBack(); + } + + + /** + * Adds support for magic method calls. + * + * @param string $method + * @param array $args + * + * @return mixed The returned value from the resolved method. + * + * @throws ORMException + * @throws \BadMethodCallException If the method called is invalid + */ + public function __call($method, $args) + { + if (0 === strpos($method, 'findBy')) { + return $this->resolveMagicCall('findBy', substr($method, 6), $args); + } + if (0 === strpos($method, 'findOneBy')) { + return $this->resolveMagicCall('findOneBy', substr($method, 9), $args); + } + if (0 === strpos($method, 'countBy')) { + return $this->resolveMagicCall('count', substr($method, 7), $args); + } + throw new \BadMethodCallException( + "Undefined method '$method'. The method name must start with ". + "either findBy, findOneBy or countBy!" + ); + } + +}