ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
ResourceDBRepository.php
Go to the documentation of this file.
1 <?php
2 
20 
26 
33 {
34  public const TABLE_NAME = 'il_resource';
35  public const IDENTIFICATION = 'rid';
36 
40  protected array $cache = [];
41  protected \ilDBInterface $db;
42 
43  public function __construct(\ilDBInterface $db)
44  {
45  $this->db = $db;
46  }
47 
51  public function getNamesForLocking(): array
52  {
53  return [self::TABLE_NAME];
54  }
55 
59  public function blank(
60  ResourceIdentification $identification,
61  ResourceType $type = ResourceType::SINGLE_FILE
62  ): StorableResource {
63  return match ($type) {
64  ResourceType::SINGLE_FILE => new StorableFileResource($identification),
65  ResourceType::CONTAINER => new StorableContainerResource($identification),
66  };
67  }
68 
72  public function get(ResourceIdentification $identification): StorableResource
73  {
74  $rid = $identification->serialize();
75 
76  if (isset($this->cache[$rid])) {
77  return $this->cache[$rid];
78  }
79 
80  $q = "SELECT storage_id, rtype FROM " . self::TABLE_NAME . " WHERE " . self::IDENTIFICATION . " = %s";
81  $r = $this->db->queryF($q, ['text'], [$rid]);
82  $d = $this->db->fetchObject($r);
83 
84  $resource = $this->blank($identification, ResourceType::from($d->rtype));
85  $resource->setStorageID($d->storage_id);
86 
87  $this->cache[$rid] = $resource;
88 
89  return $resource;
90  }
91 
95  public function has(ResourceIdentification $identification): bool
96  {
97  $rid = $identification->serialize();
98 
99  if (isset($this->cache[$rid])) {
100  return true;
101  }
102 
103  $q = "SELECT EXISTS(SELECT 1 FROM " . self::TABLE_NAME . " WHERE " . self::IDENTIFICATION . " = %s) AS found";
104  $r = $this->db->queryF($q, ['text'], [$rid]);
105  $d = $this->db->fetchAssoc($r);
106 
107  return (bool) $d['found'];
108  }
109 
113  public function store(StorableResource $resource): void
114  {
115  $rid = $resource->getIdentification()->serialize();
116 
117  $this->db->replace(
118  self::TABLE_NAME,
119  [
120  self::IDENTIFICATION => ['text', $rid]
121  ],
122  [
123  'storage_id' => ['text', $resource->getStorageID()],
124  'rtype' => ['text', $resource->getType()->value],
125  ]
126  );
127 
128  $this->cache[$rid] = $resource;
129  }
130 
134  public function delete(StorableResource $resource): void
135  {
136  $rid = $resource->getIdentification()->serialize();
137  $this->db->manipulateF(
138  "DELETE FROM " . self::TABLE_NAME . " WHERE " . self::IDENTIFICATION . " = %s",
139  ['text'],
140  [$rid]
141  );
142  unset($this->cache[$rid]);
143  }
144 
148  public function getAll(): \Generator
149  {
150  yield from [];
151  }
152 
153  public function preload(array $identification_strings): void
154  {
155  $r = $this->db->query(
156  "SELECT rid, storage_id, rtype FROM " . self::TABLE_NAME . " WHERE "
157  . $this->db->in(self::IDENTIFICATION, $identification_strings, false, 'text')
158  );
159  while ($d = $this->db->fetchAssoc($r)) {
160  $this->populateFromArray($d);
161  }
162  }
163 
164  public function populateFromArray(array $data): void
165  {
166  $resource = $this->blank(
167  new ResourceIdentification($data['rid']),
168  ResourceType::from($data['rtype'] ?? ResourceType::SINGLE_FILE->value)
169  );
170  $resource->setStorageID($data['storage_id']);
171  $this->cache[$data['rid']] = $resource;
172  }
173 }
blank(ResourceIdentification $identification, ResourceType $type=ResourceType::SINGLE_FILE)
$q
Definition: shib_logout.php:18
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$r