therselman
7 years ago
5 changed files with 137 additions and 160 deletions
@ -1,145 +0,0 @@ |
|||||||
<?php |
|
||||||
|
|
||||||
namespace Twister; |
|
||||||
|
|
||||||
//@param \Doctrine\DBAL\Connection $conn |
|
||||||
|
|
||||||
class EntityManager |
|
||||||
{ |
|
||||||
protected $config = null; |
|
||||||
|
|
||||||
/** |
|
||||||
* The database connection used by the EntityManager. |
|
||||||
* |
|
||||||
* @var \Doctrine\DBAL\Connection |
|
||||||
*/ |
|
||||||
protected $conn = null; |
|
||||||
|
|
||||||
/** |
|
||||||
* Whether the EntityManager is closed or not. |
|
||||||
* |
|
||||||
* @var bool |
|
||||||
*/ |
|
||||||
private $closed = false; |
|
||||||
|
|
||||||
/** |
|
||||||
* Collection of query filters. |
|
||||||
* |
|
||||||
* @var \Doctrine\ORM\Query\FilterCollection |
|
||||||
*/ |
|
||||||
private $filterCollection; |
|
||||||
|
|
||||||
public function __construct($conn, array $properties = null) |
|
||||||
{ |
|
||||||
$this->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; |
|
||||||
} |
|
||||||
} |
|
@ -1,17 +1,8 @@ |
|||||||
<?php |
<?php |
||||||
|
|
||||||
namespace Twister; |
namespace Twister\ORM; |
||||||
|
|
||||||
/** |
class EntityManager |
||||||
* An EntityRepository serves as a repository for entities with generic as well as |
|
||||||
* business specific methods for retrieving entities. |
|
||||||
* |
|
||||||
* This class is designed for inheritance and users can subclass this class to |
|
||||||
* write their own repositories with business-specific methods to locate entities. |
|
||||||
* |
|
||||||
* @author Trevor Herselman <therselman@gmail.com> |
|
||||||
*/ |
|
||||||
class EntityRepository |
|
||||||
{ |
{ |
||||||
/** |
/** |
||||||
* The database connection used by the EntityManager. |
* The database connection used by the EntityManager. |
@ -0,0 +1,131 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace Twister\ORM; |
||||||
|
|
||||||
|
/** |
||||||
|
* A Repository serves as a repository for entities with generic as well as |
||||||
|
* business specific methods for retrieving entities. |
||||||
|
* |
||||||
|
* This class is designed for inheritance and users can subclass this class to |
||||||
|
* write their own repositories with business-specific methods to locate entities. |
||||||
|
* |
||||||
|
* @author Trevor Herselman <therselman@gmail.com> |
||||||
|
*/ |
||||||
|
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!" |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue