Advertisement
Guest User

Untitled

a guest
Dec 21st, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.43 KB | None | 0 0
  1. <?php
  2.  
  3. namespace VWA;
  4.  
  5. class Database
  6. {
  7. private $system, $host, $port, $name, $user, $pass, $pdo;
  8.  
  9. public function __construct($system, $host, $port, $name, $user, $pass)
  10. {
  11. $this->system = $system;
  12. $this->host = $host;
  13. $this->port = $port;
  14. $this->name = $name;
  15. $this->user = $user;
  16. $this->pass = $pass;
  17. $this->pdo = new \PDO($system.':host='.$host.';port='.$port.';dbname='.$name.';', $user, $pass);
  18. }
  19.  
  20. public function getPDO() { return $this->pdo; }
  21. public function getSystem() { return $this->system; }
  22.  
  23. public static function createParameterList($count)
  24. {
  25. if ($count == 0)
  26. {
  27. return '';
  28. }
  29. else if ($count == 1)
  30. {
  31. return '?';
  32. }
  33. else
  34. {
  35. $output = '?';
  36. for ($i = 1; $i < $count; $i++)
  37. {
  38. $output .= ', ?';
  39. }
  40.  
  41. return $output;
  42. }
  43. }
  44.  
  45. public function execute($query, $parameters = [])
  46. {
  47. $statement = $this->pdo->prepare($query);
  48.  
  49. if (count($parameters) > 0)
  50. {
  51. $statement->execute($parameters);
  52. }
  53. else
  54. {
  55. $statement->execute();
  56. }
  57.  
  58. echo '<br>';
  59. echo $query;
  60. echo '<br>';
  61.  
  62. return $statement;
  63. }
  64.  
  65. public function select($table, $columns, $condition = '', $parameters = [])
  66. {
  67. $query = 'SELECT ';
  68.  
  69. $first = true;
  70. foreach ($columns as $column)
  71. {
  72. if ($first)
  73. {
  74. $query .= $column;
  75. $first = false;
  76. }
  77. else
  78. {
  79. $query .= ', '.$column;
  80. }
  81. }
  82.  
  83. $query .= ' FROM '.$table;
  84.  
  85. if ($condition != '')
  86. {
  87. $query .= ' WHERE '.$condition;
  88. }
  89.  
  90. return $this->execute($query, $parameters)->fetchAll(\PDO::FETCH_ASSOC);
  91. }
  92.  
  93. public function selectUnique($table, $columns, $targetColumn, $parameter)
  94. {
  95. $result = $this->database->select('users', ['*'], $targetColumn.' = ?', [$parameter]);
  96. if (count($result) == 0)
  97. {
  98. return null;
  99. }
  100. else
  101. {
  102. return $result[0];
  103. }
  104. }
  105.  
  106. public function insert($table, $columns, $parameters = [])
  107. {
  108. return $this->insertMany($table, $columns, [$parameters]);
  109. }
  110.  
  111. public function insertMany($table, $columns, $rows = [[]])
  112. {
  113. $query = 'INSERT INTO '.$table.' (';
  114.  
  115. $first = true;
  116. foreach ($columns as $column)
  117. {
  118. if ($first)
  119. {
  120. $query .= $column;
  121. $first = false;
  122. }
  123. else
  124. {
  125. $query .= ', '.$column;
  126. }
  127. }
  128.  
  129. $query .= ') VALUES ';
  130.  
  131. $outputRow = function($row)
  132. {
  133. $output = '(';
  134. $first = true;
  135. foreach ($row as $value)
  136. {
  137. if ($first)
  138. {
  139. $output .= '?';
  140. $first = false;
  141. }
  142. else
  143. {
  144. $output .= ', ?';
  145. }
  146. }
  147.  
  148. $output .= ')';
  149.  
  150. return $output;
  151. };
  152.  
  153. $parameters = [];
  154. $first = true;
  155. foreach ($rows as $row)
  156. {
  157. if ($first)
  158. {
  159. $query .= $outputRow($row);
  160. $first = false;
  161. }
  162. else
  163. {
  164. $query .= ', '.$outputRow($row);
  165. }
  166.  
  167. foreach ($row as $value)
  168. {
  169. $parameters[] = $value;
  170. }
  171. }
  172.  
  173. echo '<br>START<br>';
  174.  
  175. echo $query;
  176. echo '<br>';
  177. print_r($parameters);
  178.  
  179. $this->execute($query, $parameters);
  180. return $this->pdo->lastInsertId();
  181. }
  182. }
  183.  
  184. class DataCache
  185. {
  186. private $fullGet, $fullGetKey, $objects, $attachedCaches;
  187.  
  188. public function __construct($fullGet, $fullGetKey = null)
  189. {
  190. if ($fullGetKey == null)
  191. {
  192. $this->fullGetKey = function($object) { return $object->getID(); };
  193. }
  194. else
  195. {
  196. $this->fullGetKey = $fullGetKey;
  197. }
  198.  
  199. $this->fullGet = $fullGet;
  200. $this->objects = [];
  201. $this->attachedCaches = [];
  202. }
  203.  
  204. public function attach($caches)
  205. {
  206. foreach ($caches as $cache)
  207. {
  208. $this->attachedCaches[] = $cache;
  209. }
  210. }
  211.  
  212. public function getKey($object)
  213. {
  214. return call_user_func($this->fullGetKey, $object);
  215. }
  216.  
  217. public function get($key)
  218. {
  219. return $this->getFirstOrNull($this->getMultiple([$key]));
  220. }
  221.  
  222. public function getMultiple($keys)
  223. {
  224. $objects = [];
  225. $uncachedKeys = [];
  226. foreach ($keys as $key)
  227. {
  228. if (array_key_exists($key, $this->objects))
  229. {
  230. $objects[] = $this->objects[$key];
  231. }
  232. else
  233. {
  234. $uncachedKeys[] = $key;
  235. }
  236. }
  237.  
  238. if (count($uncachedKeys) > 0)
  239. {
  240. $loadedObjects = call_user_func($this->fullGet, $keys);
  241. $this->putInCache($loadedObjects);
  242. $objects = array_merge($objects, $loadedObjects);
  243. }
  244.  
  245. return $objects;
  246. }
  247.  
  248. protected function getFirstOrNull($collection)
  249. {
  250. if (count($collection) == 0)
  251. {
  252. return null;
  253. }
  254. else
  255. {
  256. return $collection[0];
  257. }
  258. }
  259.  
  260. protected function putInCache($objects)
  261. {
  262. foreach ($objects as $object)
  263. {
  264. $this->objects[$this->getKey($object)] = $object;
  265. foreach ($this->attachedCaches as $cache)
  266. {
  267. $cache->objects[$cache->getKey($object)] = $object;
  268. }
  269. }
  270. }
  271. }
  272.  
  273. class FullDataCache extends DataCache
  274. {
  275. private $fullCreate;
  276.  
  277. public function __construct($fullCreate, $fullGet, $fullGetKey = null)
  278. {
  279. DataCache::__construct($fullGet, $fullGetKey);
  280. $this->fullCreate = $fullCreate;
  281. }
  282.  
  283. public function create($args)
  284. {
  285. return $this->getFirstOrNull($this->createMultiple([$args]));
  286. }
  287.  
  288. public function createMultiple($argLists)
  289. {
  290. $objects = call_user_func($this->fullCreate, $argLists);
  291. $this->putInCache($objects);
  292. return $objects;
  293. }
  294. }
  295.  
  296. abstract class DataSource
  297. {
  298. public function accessUsers() { return $this->users; }
  299. public function accessUsersByEmail() { return $this->usersByEmail; }
  300. public function createUser($name, $email, $pass) { return $this->users->create(['name' => $name, 'email' => $email, 'pass' => $pass]); }
  301. protected abstract function createUsers($argLists);
  302. protected abstract function getUsers($ids);
  303. protected abstract function getUsersByEmail($emails);
  304. private $users, $usersByEmail;
  305.  
  306. public function accessTypes() { return $this->types; }
  307. public function createType($name) { return $this->types->create(['name' => $name]); }
  308. protected abstract function createTypes($argLists);
  309. protected abstract function getTypes($ids);
  310. private $types;
  311.  
  312. public function accessFields() { return $this->fields; }
  313. public function createField($typeID, $name, $fieldType) { return $this->fields->create(['typeID' => $typeID, 'name' => $name, 'fieldType' => $fieldType]); }
  314. protected abstract function createFields($argLists);
  315. protected abstract function getFields($ids);
  316. private $fields;
  317.  
  318. public function accessFieldLists() { return $this->fieldLists; }
  319. protected abstract function getFieldLists($typeIDs);
  320. private $fieldLists;
  321.  
  322. public function accessViews() { return $this->views; }
  323. public function createView($typeID, $name) { return $this->views->create(['typeID' => $typeID, 'name' => $name]); }
  324. protected abstract function createViews($argLists);
  325. protected abstract function getViews($ids);
  326. private $views;
  327.  
  328. public function accessViewLists() { return $this->viewLists; }
  329. protected abstract function getViewLists($typeIDs);
  330. private $viewLists;
  331.  
  332. protected function initialize()
  333. {
  334. $this->users = new FullDataCache(function($argLists) { return $this->createUsers($argLists); }, function($ids) { return $this->getUsers($ids); });
  335. $this->usersByEmail = new FullDataCache(function($argLists) { return $this->createUsers($argLists); }, function($emails) { return $this->getUsersByEmail($emails); }, function($user) { return $user->getEmail(); });
  336.  
  337. $this->users->attach([$this->usersByEmail]);
  338. $this->usersByEmail->attach([$this->users]);
  339.  
  340. $this->types = new FullDataCache(function($argLists) { return $this->createTypes($argLists); }, function($ids) { return $this->getTypes($ids); });
  341.  
  342. $this->fields = new FullDataCache(function($argLists) { return $this->createFields($argLists); }, function($ids) { return $this->getFields($ids); });
  343. $this->fieldLists = new DataCache(function($typeIDs) { return $this->getFieldLists($typeIDs); });
  344.  
  345. $this->views = new FullDataCache(function($argLists) { return $this->createViews($argLists); }, function($ids) { return $this->getViews($ids); });
  346. $this->viewLists = new DataCache(function($typeIDs) { return $this->getViewLists($typeIDs); });
  347. }
  348. }
  349.  
  350. class TemporaryDataSource extends DataSource
  351. {
  352. public function __construct()
  353. {
  354. $this->initialize();
  355. }
  356.  
  357. private $userCount = 1;
  358.  
  359. protected function createUsers($argLists)
  360. {
  361. $users = [];
  362. foreach ($argLists as $argList)
  363. {
  364. $users[] = new User($this->userCount++, $argList['name'], $argList['email'], password_hash($argList['pass'], \PASSWORD_BCRYPT, ['cost' => 12]));
  365. }
  366.  
  367. return $users;
  368. }
  369.  
  370. protected function getUsers($ids) { return array_fill(0, count($ids), null); }
  371.  
  372. protected function getUsersByEmail($emails) { return array_fill(0, count($emails), null); }
  373.  
  374. private $typeCount = 1;
  375.  
  376. protected function createTypes($argLists)
  377. {
  378. $types = [];
  379. foreach ($argLists as $argList)
  380. {
  381. $types[] = new Type($this->typeCount++, $argList['name']);
  382. }
  383.  
  384. $typeIDs = array_map(function($type) { return $type->getID(); }, $types);
  385.  
  386. $fieldLists = $this->getFieldLists($typeIDs);
  387. for ($i = 0; $i < count($types); $i++)
  388. {
  389. foreach ($fieldLists[$i] as $fieldList)
  390. {
  391. foreach ($fieldList as $fieldID)
  392. {
  393. $types[$i]->_addFieldID($fieldID);
  394. }
  395. }
  396. }
  397.  
  398. $viewLists = $this->getViewLists($typeIDs);
  399. for ($i = 0; $i < count($types); $i++)
  400. {
  401. foreach ($viewLists[$i] as $viewList)
  402. {
  403. foreach ($viewList as $viewID)
  404. {
  405. $types[$i]->_addViewID($viewID);
  406. }
  407. }
  408. }
  409.  
  410. return $types;
  411. }
  412.  
  413. protected function getTypes($ids) { return array_fill(0, count($ids), null); }
  414.  
  415. private $fieldCount = 1;
  416.  
  417. protected function createFields($argLists)
  418. {
  419. $fields = [];
  420. foreach ($argLists as $argList)
  421. {
  422. $field = new Field($argList['typeID'], $this->fieldCount++, $argList['name'], $argList['fieldType']);
  423. Type::fromID($argList['typeID'])->_addFieldID($field->getID());
  424. $fields[] = $field;
  425. }
  426.  
  427. return $fields;
  428. }
  429.  
  430. protected function getFields($ids) { return array_fill(0, count($ids), null); }
  431.  
  432. protected function getFieldLists($typeIDs) { return array_fill(0, count($typeIDs), []); }
  433.  
  434. private $viewCount = 1;
  435.  
  436. protected function createViews($argLists)
  437. {
  438. $views = [];
  439. foreach ($argLists as $argList)
  440. {
  441. $view = new View($argList['typeID'], $this->viewCount++, $argList['name']);
  442. View::fromID($argList['typeID'])->_addViewID($view->getID());
  443. $views[] = $view;
  444. }
  445.  
  446. return $views;
  447. }
  448.  
  449. protected function getViews($ids) { return array_fill(0, count($ids), null); }
  450.  
  451. protected function getViewLists($typeIDs) { return array_fill(0, count($typeIDs), []); }
  452. }
  453.  
  454. /*class DatabaseDataSource extends DataSource
  455. {
  456. private $database;
  457.  
  458. public function __construct($database)
  459. {
  460. $this->initialize();
  461. $this->database = $database;
  462. }
  463.  
  464. protected function loadUsersByID($ids)
  465. {
  466. $result = $this->database->select('users', ['name', 'email', 'pass'], 'id = IN('.createParameterList(count($ids)).')', $ids);
  467. if ($result == null)
  468. {
  469. return null;
  470. }
  471. else
  472. {
  473. return ['key' => $id, 'object' => new User($id, $result['name'], $result['email'], $result['pass'])];
  474. }
  475. }
  476.  
  477. protected function loadUsersByEmail($emails)
  478. {
  479. $result = $this->database->selectUnique('users', ['id', 'name', 'pass'], 'email', $email);
  480. if ($result == null)
  481. {
  482. return null;
  483. }
  484. else
  485. {
  486. return ['key' => $email, 'object' => new User($result['id'], $result['name'], $email, $result['pass'])];
  487. }
  488. }
  489.  
  490. protected function loadClassesByID($ids)
  491. {
  492. $result = $this->database->selectUnique('classes', ['name'], 'id', $id);
  493. if ($result == null)
  494. {
  495. return null;
  496. }
  497.  
  498. $class = new WebClass($result[0]['id'], $name);
  499.  
  500. $result = $this->database->select('fields', ['id', 'name', 'type'], 'class = ?', [$class->getID()]);
  501. foreach ($result as $row)
  502. {
  503. $class->internalAddField(new Field($class, $row['id'], $row['name'], $row['type']));
  504. }
  505.  
  506. return $class;
  507. }
  508.  
  509. protected function constructClasses($names)
  510. {
  511.  
  512. }
  513. }*/
  514.  
  515. class Core
  516. {
  517. private static $instance;
  518.  
  519. public static function getInstance()
  520. {
  521. return Core::$instance;
  522. }
  523.  
  524. private $data;
  525.  
  526. public function __construct($data)
  527. {
  528. Core::$instance = $this;
  529. $this->data = $data;
  530. }
  531.  
  532. public function getData()
  533. {
  534. return $this->data;
  535. }
  536.  
  537. public function login($email, $pass)
  538. {
  539. $user = User::fromEmail($email);
  540. if ($user == null)
  541. {
  542. return LoginResponse::UnknownEmail;
  543. }
  544. else if (!password_verify($pass, $user->getPassHash()))
  545. {
  546. return LoginResponse::InvalidPass;
  547. }
  548. else
  549. {
  550. session_destroy();
  551. session_start();
  552. $_SESSION['userID'] = $user->getID();
  553. return LoginResponse::Success;
  554. }
  555. }
  556.  
  557. public function logout()
  558. {
  559. session_destroy();
  560. $_SESSION = [];
  561. }
  562.  
  563. public function getCurrentUser()
  564. {
  565. if (!isset($_SESSION['userID']))
  566. {
  567. return null;
  568. }
  569. else
  570. {
  571. return User::fromID($_SESSION['userID']);
  572. }
  573. }
  574. }
  575.  
  576. abstract class LoginResponse
  577. {
  578. const UnknownEmail = 0;
  579. const InvalidPass = 1;
  580. const Success = 2;
  581. }
  582.  
  583. class User
  584. {
  585. public static function fromID($id)
  586. {
  587. return Core::getInstance()->getData()->accessUsers()->get($id);
  588. }
  589.  
  590. public static function fromEmail($email)
  591. {
  592. return Core::getInstance()->getData()->accessUsersByEmail()->get($email);
  593. }
  594.  
  595. public static function create($name, $email, $pass)
  596. {
  597. return Core::getInstance()->getData()->createUser($name, $email, $pass);
  598. }
  599.  
  600. private $id, $name, $email, $passHash;
  601.  
  602. public function __construct($id, $name, $email, $passHash)
  603. {
  604. $this->id = $id;
  605. $this->name = $name;
  606. $this->email = $email;
  607. $this->passHash = $passHash;
  608. }
  609.  
  610. public function getID() { return $this->id; }
  611. public function getName() { return $this->name; }
  612. public function getEmail() { return $this->email; }
  613. public function getPassHash() { return $this->passHash; }
  614. }
  615.  
  616. class Type
  617. {
  618. public static function fromID($id)
  619. {
  620. return Core::getInstance()->getData()->accessTypes()->get($id);
  621. }
  622.  
  623. public static function create($name)
  624. {
  625. return Core::getInstance()->getData()->createType($name);
  626. }
  627.  
  628. private $id, $name, $fieldIDs, $viewIDs;
  629.  
  630. public function __construct($id, $name)
  631. {
  632. $this->id = $id;
  633. $this->name = $name;
  634. $this->fieldIDs = [];
  635. $this->viewIDs = [];
  636. }
  637.  
  638. public function getID() { return $this->id; }
  639. public function getName() { return $this->name; }
  640. public function getFieldIDs() { return $this->fieldIDs; }
  641. public function getViewIDs() { return $this->viewIDs; }
  642.  
  643. public function getFields()
  644. {
  645. $fields = [];
  646. foreach ($this->fieldIDs as $fieldID)
  647. {
  648. $fields[] = Field::fromID($fieldID);
  649. }
  650.  
  651. return $fields;
  652. }
  653.  
  654. public function _addFieldID($fieldID)
  655. {
  656. $this->fieldIDs[] = $fieldID;
  657. }
  658.  
  659. public function _removeFieldID($fieldID)
  660. {
  661. array_splice($this->fieldIDs, array_search($fieldID, $this->fieldIDs), 1);
  662. }
  663.  
  664. public function getViews()
  665. {
  666. $views = [];
  667. foreach ($this->viewIDs as $viewID)
  668. {
  669. $views[] = View::fromID($viewID);
  670. }
  671.  
  672. return $views;
  673. }
  674.  
  675. public function _addViewID($viewID)
  676. {
  677. $this->viewIDs[] = $viewID;
  678. }
  679.  
  680. public function _removeViewID($fieldID)
  681. {
  682. array_splice($this->viewIDs, array_search($viewID, $this->viewIDs), 1);
  683. }
  684. }
  685.  
  686. class Field
  687. {
  688. public static function fromID($id)
  689. {
  690. return Core::getInstance()->getData()->accessFields()->get($id);
  691. }
  692.  
  693. public static function create($typeID, $name, $fieldType)
  694. {
  695. return Core::getInstance()->getData()->createField($typeID, $name, $fieldType);
  696. }
  697.  
  698. private $typeID, $id, $name, $fieldType;
  699.  
  700. public function __construct($typeID, $id, $name, $fieldType)
  701. {
  702. $this->typeID = $typeID;
  703. $this->id = $id;
  704. $this->name = $name;
  705. $this->fieldType = $fieldType;
  706. }
  707.  
  708. public function getTypeID() { return $this->typeID; }
  709. public function getType() { return Type::fromID($this->typeID); }
  710. public function getID() { return $this->id; }
  711. public function getName() { return $this->name; }
  712. public function getFieldType() { return $this->fieldType; }
  713. }
  714.  
  715. abstract class FieldType
  716. {
  717. const Boolean = 0;
  718. const Integer = 1;
  719. const BigInteger = 2;
  720. const Number = 3;
  721. const PreciseNumber = 4;
  722. const Text = 5;
  723. const Enumeration = 6;
  724. const Object = 7;
  725. }
  726.  
  727. class View
  728. {
  729. public static function fromID($id)
  730. {
  731. return Core::getInstance()->getData()->accessViews()->get($id);
  732. }
  733.  
  734. public static function create($typeID, $name)
  735. {
  736. return Core::getInstance()->getData()->createView($typeID, $name);
  737. }
  738.  
  739. private $typeID, $id, $name;
  740.  
  741. public function __construct($typeID, $id, $name)
  742. {
  743. $this->typeID = $typeID;
  744. $this->id = $id;
  745. $this->name = $name;
  746. }
  747.  
  748. public function getTypeID() { return $this->typeID; }
  749. public function getType() { return Type::fromID($this->typeID); }
  750. public function getID() { return $this->id; }
  751. public function getName() { return $this->name; }
  752. }
  753.  
  754. class Entity
  755. {
  756. private $typeID, $id, $fields;
  757.  
  758. public function __construct($typeID, $id)
  759. {
  760. $this->typeID = $typeID;
  761. $this->id = $id;
  762. $this->fields = [];
  763. }
  764.  
  765. public function getTypeID() { return $this->typeID; }
  766. public function getType() { return Type::fromID($this->typeID); }
  767. public function getID() { return $this->id; }
  768. public function getFields() { return $this->fields; }
  769. }
  770.  
  771.  
  772.  
  773.  
  774. $vwa = new Core(new TemporaryDataSource());
  775.  
  776. $id = Type::create('Person')->getID();
  777. Field::create($id, 'Name', FieldType::Text);
  778. Field::create($id, 'Age', FieldType::Integer);
  779. View::create($id, 'default');
  780.  
  781. print_r(Type::fromID(1)->getViews());
  782.  
  783. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement