api de gestion de ticket, basé sur php-crud-api. Le but est de décorrélé les outils de gestion des données, afin
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ReflectionService.php 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace Tqdev\PhpCrudApi\Column;
  3. use Tqdev\PhpCrudApi\Cache\Cache;
  4. use Tqdev\PhpCrudApi\Column\Reflection\ReflectedDatabase;
  5. use Tqdev\PhpCrudApi\Column\Reflection\ReflectedTable;
  6. use Tqdev\PhpCrudApi\Database\GenericDB;
  7. class ReflectionService
  8. {
  9. private $db;
  10. private $cache;
  11. private $ttl;
  12. private $tables;
  13. public function __construct(GenericDB $db, Cache $cache, int $ttl)
  14. {
  15. $this->db = $db;
  16. $this->cache = $cache;
  17. $this->ttl = $ttl;
  18. $this->tables = $this->loadTables(true);
  19. }
  20. private function loadTables(bool $useCache): ReflectedDatabase
  21. {
  22. $data = $useCache ? $this->cache->get('ReflectedDatabase') : '';
  23. if ($data != '') {
  24. $tables = ReflectedDatabase::fromJson(json_decode(gzuncompress($data)));
  25. } else {
  26. $tables = ReflectedDatabase::fromReflection($this->db->reflection());
  27. $data = gzcompress(json_encode($tables, JSON_UNESCAPED_UNICODE));
  28. $this->cache->set('ReflectedDatabase', $data, $this->ttl);
  29. }
  30. return $tables;
  31. }
  32. public function refresh()
  33. {
  34. $this->tables = $this->loadTables(false);
  35. }
  36. public function hasTable(String $table): bool
  37. {
  38. return $this->tables->exists($table);
  39. }
  40. public function getTable(String $table): ReflectedTable
  41. {
  42. return $this->tables->get($table);
  43. }
  44. public function getDatabase(): ReflectedDatabase
  45. {
  46. return $this->tables;
  47. }
  48. }