This commit is contained in:
therselman 2017-08-22 21:21:12 +02:00
parent f88add942c
commit 47f66a2260
3 changed files with 191 additions and 4 deletions

View File

@ -9,9 +9,12 @@ namespace Twister;
class Entity
{
protected $container = null;
protected $properties = null;
public function __construct(array $properties = null)
public $id = 0;
public function __construct(Container &$c, array $properties = null)
{
$this->properties =& $properties;
}

145
src/EntityManager.php Normal file
View File

@ -0,0 +1,145 @@
<?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;
}
}

View File

@ -7,13 +7,19 @@ class User
private $container = null;
public $id = 0;
private $_properties = null;
private $_db = null;
private $db = null;
private $_permissions = null;
public function __construct(Container &$c, array $properties = null)
{
$this->properties =& $properties;
}
function __construct(Container &$c, $id = 0)
{
$this->container = $c;
$this->_db = $c->db;
$this->db = $c->db;
$this->id = $id;
@ -31,6 +37,11 @@ class User
*/
public function __get($key)
{
switch ($key)
{
case 'managers': return $this->_properties[$key] = new Managers::factory();
}
return $this->_properties[$key];
}
@ -46,6 +57,34 @@ class User
$this->_properties[$key] = $value;
}
public function worlds($key, $value)
{
if ( ! isset($this->_properties['worlds']))
{
$sql = 'SELECT * FROM user_worlds WHERE user_id = ' . $this->id;
$this->db->get_array($sql);
}
$this->_properties[$key] = $value;
}
public function getWorlds($key, $value)
{
$this->_properties[$key] = $value;
}
public function isManager($id)
{
return $this->getManagers()
}
public function getManagers()
{
if ( ! isset($this->_properties['managers']))
{
$sql = 'SELECT * FROM user_managers WHERE user_id = ' . $this->id;
$this->_properties['managers'] = $this->db->get_array($sql);
}
return $this->_properties['managers'];
}
function __isset($key)
{
return isset($this->_properties[$key]);
@ -57,7 +96,7 @@ class User
private function load_permissions()
{
$this->_permissions = $this->_db->get_array( 'SELECT SQL_CACHE ' . // cached because these tables are less frequenty updated!
$this->_permissions = $this->db->get_array( 'SELECT SQL_CACHE ' . // cached because these tables are less frequenty updated!
'g.alias as g_alias,' .
'p.alias as p_alias,' .
'acl.object_id' .