Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace VWA;
- class Database
- {
- private $system, $host, $port, $name, $user, $pass, $pdo;
- public function __construct($system, $host, $port, $name, $user, $pass)
- {
- $this->system = $system;
- $this->host = $host;
- $this->port = $port;
- $this->name = $name;
- $this->user = $user;
- $this->pass = $pass;
- $this->pdo = new \PDO($system.':host='.$host.';port='.$port.';dbname='.$name.';', $user, $pass);
- }
- public function getPDO() { return $this->pdo; }
- public function getSystem() { return $this->system; }
- public static function createParameterList($count)
- {
- if ($count == 0)
- {
- return '';
- }
- else if ($count == 1)
- {
- return '?';
- }
- else
- {
- $output = '?';
- for ($i = 1; $i < $count; $i++)
- {
- $output .= ', ?';
- }
- return $output;
- }
- }
- public function execute($query, $parameters = [])
- {
- $statement = $this->pdo->prepare($query);
- if (count($parameters) > 0)
- {
- $statement->execute($parameters);
- }
- else
- {
- $statement->execute();
- }
- echo '<br>';
- echo $query;
- echo '<br>';
- return $statement;
- }
- public function select($table, $columns, $condition = '', $parameters = [])
- {
- $query = 'SELECT ';
- $first = true;
- foreach ($columns as $column)
- {
- if ($first)
- {
- $query .= $column;
- $first = false;
- }
- else
- {
- $query .= ', '.$column;
- }
- }
- $query .= ' FROM '.$table;
- if ($condition != '')
- {
- $query .= ' WHERE '.$condition;
- }
- return $this->execute($query, $parameters)->fetchAll(\PDO::FETCH_ASSOC);
- }
- public function selectUnique($table, $columns, $targetColumn, $parameter)
- {
- $result = $this->database->select('users', ['*'], $targetColumn.' = ?', [$parameter]);
- if (count($result) == 0)
- {
- return null;
- }
- else
- {
- return $result[0];
- }
- }
- public function insert($table, $columns, $parameters = [])
- {
- return $this->insertMany($table, $columns, [$parameters]);
- }
- public function insertMany($table, $columns, $rows = [[]])
- {
- $query = 'INSERT INTO '.$table.' (';
- $first = true;
- foreach ($columns as $column)
- {
- if ($first)
- {
- $query .= $column;
- $first = false;
- }
- else
- {
- $query .= ', '.$column;
- }
- }
- $query .= ') VALUES ';
- $outputRow = function($row)
- {
- $output = '(';
- $first = true;
- foreach ($row as $value)
- {
- if ($first)
- {
- $output .= '?';
- $first = false;
- }
- else
- {
- $output .= ', ?';
- }
- }
- $output .= ')';
- return $output;
- };
- $parameters = [];
- $first = true;
- foreach ($rows as $row)
- {
- if ($first)
- {
- $query .= $outputRow($row);
- $first = false;
- }
- else
- {
- $query .= ', '.$outputRow($row);
- }
- foreach ($row as $value)
- {
- $parameters[] = $value;
- }
- }
- echo '<br>START<br>';
- echo $query;
- echo '<br>';
- print_r($parameters);
- $this->execute($query, $parameters);
- return $this->pdo->lastInsertId();
- }
- }
- class DataCache
- {
- private $fullGet, $fullGetKey, $objects, $attachedCaches;
- public function __construct($fullGet, $fullGetKey = null)
- {
- if ($fullGetKey == null)
- {
- $this->fullGetKey = function($object) { return $object->getID(); };
- }
- else
- {
- $this->fullGetKey = $fullGetKey;
- }
- $this->fullGet = $fullGet;
- $this->objects = [];
- $this->attachedCaches = [];
- }
- public function attach($caches)
- {
- foreach ($caches as $cache)
- {
- $this->attachedCaches[] = $cache;
- }
- }
- public function getKey($object)
- {
- return call_user_func($this->fullGetKey, $object);
- }
- public function get($key)
- {
- return $this->getFirstOrNull($this->getMultiple([$key]));
- }
- public function getMultiple($keys)
- {
- $objects = [];
- $uncachedKeys = [];
- foreach ($keys as $key)
- {
- if (array_key_exists($key, $this->objects))
- {
- $objects[] = $this->objects[$key];
- }
- else
- {
- $uncachedKeys[] = $key;
- }
- }
- if (count($uncachedKeys) > 0)
- {
- $loadedObjects = call_user_func($this->fullGet, $keys);
- $this->putInCache($loadedObjects);
- $objects = array_merge($objects, $loadedObjects);
- }
- return $objects;
- }
- protected function getFirstOrNull($collection)
- {
- if (count($collection) == 0)
- {
- return null;
- }
- else
- {
- return $collection[0];
- }
- }
- protected function putInCache($objects)
- {
- foreach ($objects as $object)
- {
- $this->objects[$this->getKey($object)] = $object;
- foreach ($this->attachedCaches as $cache)
- {
- $cache->objects[$cache->getKey($object)] = $object;
- }
- }
- }
- }
- class FullDataCache extends DataCache
- {
- private $fullCreate;
- public function __construct($fullCreate, $fullGet, $fullGetKey = null)
- {
- DataCache::__construct($fullGet, $fullGetKey);
- $this->fullCreate = $fullCreate;
- }
- public function create($args)
- {
- return $this->getFirstOrNull($this->createMultiple([$args]));
- }
- public function createMultiple($argLists)
- {
- $objects = call_user_func($this->fullCreate, $argLists);
- $this->putInCache($objects);
- return $objects;
- }
- }
- abstract class DataSource
- {
- public function accessUsers() { return $this->users; }
- public function accessUsersByEmail() { return $this->usersByEmail; }
- public function createUser($name, $email, $pass) { return $this->users->create(['name' => $name, 'email' => $email, 'pass' => $pass]); }
- protected abstract function createUsers($argLists);
- protected abstract function getUsers($ids);
- protected abstract function getUsersByEmail($emails);
- private $users, $usersByEmail;
- public function accessTypes() { return $this->types; }
- public function createType($name) { return $this->types->create(['name' => $name]); }
- protected abstract function createTypes($argLists);
- protected abstract function getTypes($ids);
- private $types;
- public function accessFields() { return $this->fields; }
- public function createField($typeID, $name, $fieldType) { return $this->fields->create(['typeID' => $typeID, 'name' => $name, 'fieldType' => $fieldType]); }
- protected abstract function createFields($argLists);
- protected abstract function getFields($ids);
- private $fields;
- public function accessFieldLists() { return $this->fieldLists; }
- protected abstract function getFieldLists($typeIDs);
- private $fieldLists;
- public function accessViews() { return $this->views; }
- public function createView($typeID, $name) { return $this->views->create(['typeID' => $typeID, 'name' => $name]); }
- protected abstract function createViews($argLists);
- protected abstract function getViews($ids);
- private $views;
- public function accessViewLists() { return $this->viewLists; }
- protected abstract function getViewLists($typeIDs);
- private $viewLists;
- protected function initialize()
- {
- $this->users = new FullDataCache(function($argLists) { return $this->createUsers($argLists); }, function($ids) { return $this->getUsers($ids); });
- $this->usersByEmail = new FullDataCache(function($argLists) { return $this->createUsers($argLists); }, function($emails) { return $this->getUsersByEmail($emails); }, function($user) { return $user->getEmail(); });
- $this->users->attach([$this->usersByEmail]);
- $this->usersByEmail->attach([$this->users]);
- $this->types = new FullDataCache(function($argLists) { return $this->createTypes($argLists); }, function($ids) { return $this->getTypes($ids); });
- $this->fields = new FullDataCache(function($argLists) { return $this->createFields($argLists); }, function($ids) { return $this->getFields($ids); });
- $this->fieldLists = new DataCache(function($typeIDs) { return $this->getFieldLists($typeIDs); });
- $this->views = new FullDataCache(function($argLists) { return $this->createViews($argLists); }, function($ids) { return $this->getViews($ids); });
- $this->viewLists = new DataCache(function($typeIDs) { return $this->getViewLists($typeIDs); });
- }
- }
- class TemporaryDataSource extends DataSource
- {
- public function __construct()
- {
- $this->initialize();
- }
- private $userCount = 1;
- protected function createUsers($argLists)
- {
- $users = [];
- foreach ($argLists as $argList)
- {
- $users[] = new User($this->userCount++, $argList['name'], $argList['email'], password_hash($argList['pass'], \PASSWORD_BCRYPT, ['cost' => 12]));
- }
- return $users;
- }
- protected function getUsers($ids) { return array_fill(0, count($ids), null); }
- protected function getUsersByEmail($emails) { return array_fill(0, count($emails), null); }
- private $typeCount = 1;
- protected function createTypes($argLists)
- {
- $types = [];
- foreach ($argLists as $argList)
- {
- $types[] = new Type($this->typeCount++, $argList['name']);
- }
- $typeIDs = array_map(function($type) { return $type->getID(); }, $types);
- $fieldLists = $this->getFieldLists($typeIDs);
- for ($i = 0; $i < count($types); $i++)
- {
- foreach ($fieldLists[$i] as $fieldList)
- {
- foreach ($fieldList as $fieldID)
- {
- $types[$i]->_addFieldID($fieldID);
- }
- }
- }
- $viewLists = $this->getViewLists($typeIDs);
- for ($i = 0; $i < count($types); $i++)
- {
- foreach ($viewLists[$i] as $viewList)
- {
- foreach ($viewList as $viewID)
- {
- $types[$i]->_addViewID($viewID);
- }
- }
- }
- return $types;
- }
- protected function getTypes($ids) { return array_fill(0, count($ids), null); }
- private $fieldCount = 1;
- protected function createFields($argLists)
- {
- $fields = [];
- foreach ($argLists as $argList)
- {
- $field = new Field($argList['typeID'], $this->fieldCount++, $argList['name'], $argList['fieldType']);
- Type::fromID($argList['typeID'])->_addFieldID($field->getID());
- $fields[] = $field;
- }
- return $fields;
- }
- protected function getFields($ids) { return array_fill(0, count($ids), null); }
- protected function getFieldLists($typeIDs) { return array_fill(0, count($typeIDs), []); }
- private $viewCount = 1;
- protected function createViews($argLists)
- {
- $views = [];
- foreach ($argLists as $argList)
- {
- $view = new View($argList['typeID'], $this->viewCount++, $argList['name']);
- View::fromID($argList['typeID'])->_addViewID($view->getID());
- $views[] = $view;
- }
- return $views;
- }
- protected function getViews($ids) { return array_fill(0, count($ids), null); }
- protected function getViewLists($typeIDs) { return array_fill(0, count($typeIDs), []); }
- }
- /*class DatabaseDataSource extends DataSource
- {
- private $database;
- public function __construct($database)
- {
- $this->initialize();
- $this->database = $database;
- }
- protected function loadUsersByID($ids)
- {
- $result = $this->database->select('users', ['name', 'email', 'pass'], 'id = IN('.createParameterList(count($ids)).')', $ids);
- if ($result == null)
- {
- return null;
- }
- else
- {
- return ['key' => $id, 'object' => new User($id, $result['name'], $result['email'], $result['pass'])];
- }
- }
- protected function loadUsersByEmail($emails)
- {
- $result = $this->database->selectUnique('users', ['id', 'name', 'pass'], 'email', $email);
- if ($result == null)
- {
- return null;
- }
- else
- {
- return ['key' => $email, 'object' => new User($result['id'], $result['name'], $email, $result['pass'])];
- }
- }
- protected function loadClassesByID($ids)
- {
- $result = $this->database->selectUnique('classes', ['name'], 'id', $id);
- if ($result == null)
- {
- return null;
- }
- $class = new WebClass($result[0]['id'], $name);
- $result = $this->database->select('fields', ['id', 'name', 'type'], 'class = ?', [$class->getID()]);
- foreach ($result as $row)
- {
- $class->internalAddField(new Field($class, $row['id'], $row['name'], $row['type']));
- }
- return $class;
- }
- protected function constructClasses($names)
- {
- }
- }*/
- class Core
- {
- private static $instance;
- public static function getInstance()
- {
- return Core::$instance;
- }
- private $data;
- public function __construct($data)
- {
- Core::$instance = $this;
- $this->data = $data;
- }
- public function getData()
- {
- return $this->data;
- }
- public function login($email, $pass)
- {
- $user = User::fromEmail($email);
- if ($user == null)
- {
- return LoginResponse::UnknownEmail;
- }
- else if (!password_verify($pass, $user->getPassHash()))
- {
- return LoginResponse::InvalidPass;
- }
- else
- {
- session_destroy();
- session_start();
- $_SESSION['userID'] = $user->getID();
- return LoginResponse::Success;
- }
- }
- public function logout()
- {
- session_destroy();
- $_SESSION = [];
- }
- public function getCurrentUser()
- {
- if (!isset($_SESSION['userID']))
- {
- return null;
- }
- else
- {
- return User::fromID($_SESSION['userID']);
- }
- }
- }
- abstract class LoginResponse
- {
- const UnknownEmail = 0;
- const InvalidPass = 1;
- const Success = 2;
- }
- class User
- {
- public static function fromID($id)
- {
- return Core::getInstance()->getData()->accessUsers()->get($id);
- }
- public static function fromEmail($email)
- {
- return Core::getInstance()->getData()->accessUsersByEmail()->get($email);
- }
- public static function create($name, $email, $pass)
- {
- return Core::getInstance()->getData()->createUser($name, $email, $pass);
- }
- private $id, $name, $email, $passHash;
- public function __construct($id, $name, $email, $passHash)
- {
- $this->id = $id;
- $this->name = $name;
- $this->email = $email;
- $this->passHash = $passHash;
- }
- public function getID() { return $this->id; }
- public function getName() { return $this->name; }
- public function getEmail() { return $this->email; }
- public function getPassHash() { return $this->passHash; }
- }
- class Type
- {
- public static function fromID($id)
- {
- return Core::getInstance()->getData()->accessTypes()->get($id);
- }
- public static function create($name)
- {
- return Core::getInstance()->getData()->createType($name);
- }
- private $id, $name, $fieldIDs, $viewIDs;
- public function __construct($id, $name)
- {
- $this->id = $id;
- $this->name = $name;
- $this->fieldIDs = [];
- $this->viewIDs = [];
- }
- public function getID() { return $this->id; }
- public function getName() { return $this->name; }
- public function getFieldIDs() { return $this->fieldIDs; }
- public function getViewIDs() { return $this->viewIDs; }
- public function getFields()
- {
- $fields = [];
- foreach ($this->fieldIDs as $fieldID)
- {
- $fields[] = Field::fromID($fieldID);
- }
- return $fields;
- }
- public function _addFieldID($fieldID)
- {
- $this->fieldIDs[] = $fieldID;
- }
- public function _removeFieldID($fieldID)
- {
- array_splice($this->fieldIDs, array_search($fieldID, $this->fieldIDs), 1);
- }
- public function getViews()
- {
- $views = [];
- foreach ($this->viewIDs as $viewID)
- {
- $views[] = View::fromID($viewID);
- }
- return $views;
- }
- public function _addViewID($viewID)
- {
- $this->viewIDs[] = $viewID;
- }
- public function _removeViewID($fieldID)
- {
- array_splice($this->viewIDs, array_search($viewID, $this->viewIDs), 1);
- }
- }
- class Field
- {
- public static function fromID($id)
- {
- return Core::getInstance()->getData()->accessFields()->get($id);
- }
- public static function create($typeID, $name, $fieldType)
- {
- return Core::getInstance()->getData()->createField($typeID, $name, $fieldType);
- }
- private $typeID, $id, $name, $fieldType;
- public function __construct($typeID, $id, $name, $fieldType)
- {
- $this->typeID = $typeID;
- $this->id = $id;
- $this->name = $name;
- $this->fieldType = $fieldType;
- }
- public function getTypeID() { return $this->typeID; }
- public function getType() { return Type::fromID($this->typeID); }
- public function getID() { return $this->id; }
- public function getName() { return $this->name; }
- public function getFieldType() { return $this->fieldType; }
- }
- abstract class FieldType
- {
- const Boolean = 0;
- const Integer = 1;
- const BigInteger = 2;
- const Number = 3;
- const PreciseNumber = 4;
- const Text = 5;
- const Enumeration = 6;
- const Object = 7;
- }
- class View
- {
- public static function fromID($id)
- {
- return Core::getInstance()->getData()->accessViews()->get($id);
- }
- public static function create($typeID, $name)
- {
- return Core::getInstance()->getData()->createView($typeID, $name);
- }
- private $typeID, $id, $name;
- public function __construct($typeID, $id, $name)
- {
- $this->typeID = $typeID;
- $this->id = $id;
- $this->name = $name;
- }
- public function getTypeID() { return $this->typeID; }
- public function getType() { return Type::fromID($this->typeID); }
- public function getID() { return $this->id; }
- public function getName() { return $this->name; }
- }
- class Entity
- {
- private $typeID, $id, $fields;
- public function __construct($typeID, $id)
- {
- $this->typeID = $typeID;
- $this->id = $id;
- $this->fields = [];
- }
- public function getTypeID() { return $this->typeID; }
- public function getType() { return Type::fromID($this->typeID); }
- public function getID() { return $this->id; }
- public function getFields() { return $this->fields; }
- }
- $vwa = new Core(new TemporaryDataSource());
- $id = Type::create('Person')->getID();
- Field::create($id, 'Name', FieldType::Text);
- Field::create($id, 'Age', FieldType::Integer);
- View::create($id, 'default');
- print_r(Type::fromID(1)->getViews());
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement