diff --git a/src/Entity.php b/src/Entity.php index 36edbc6..9308d6d 100644 --- a/src/Entity.php +++ b/src/Entity.php @@ -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; } diff --git a/src/EntityManager.php b/src/EntityManager.php new file mode 100644 index 0000000..743a476 --- /dev/null +++ b/src/EntityManager.php @@ -0,0 +1,145 @@ +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/User.php b/src/User.php index 40bccf6..d8576ca 100644 --- a/src/User.php +++ b/src/User.php @@ -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' .