ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
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 
42  public function __construct(protected \ilDBInterface $db)
43  {
44  }
45 
49  public function getNamesForLocking(): array
50  {
51  return [self::TABLE_NAME];
52  }
53 
57  public function blank(
58  ResourceIdentification $identification,
59  ResourceType $type = ResourceType::SINGLE_FILE
60  ): StorableResource {
61  return match ($type) {
62  ResourceType::SINGLE_FILE => new StorableFileResource($identification),
63  ResourceType::CONTAINER => new StorableContainerResource($identification),
64  };
65  }
66 
70  public function get(ResourceIdentification $identification): StorableResource
71  {
72  $rid = $identification->serialize();
73 
74  if (isset($this->cache[$rid])) {
75  return $this->cache[$rid];
76  }
77 
78  $q = "SELECT storage_id, rtype FROM " . self::TABLE_NAME . " WHERE " . self::IDENTIFICATION . " = %s";
79  $r = $this->db->queryF($q, ['text'], [$rid]);
80  $d = $this->db->fetchObject($r);
81 
82  $resource = $this->blank($identification, ResourceType::from($d->rtype));
83  $resource->setStorageID($d->storage_id);
84 
85  $this->cache[$rid] = $resource;
86 
87  return $resource;
88  }
89 
93  public function has(ResourceIdentification $identification): bool
94  {
95  $rid = $identification->serialize();
96 
97  if (isset($this->cache[$rid])) {
98  return true;
99  }
100 
101  $q = "SELECT EXISTS(SELECT 1 FROM " . self::TABLE_NAME . " WHERE " . self::IDENTIFICATION . " = %s) AS found";
102  $r = $this->db->queryF($q, ['text'], [$rid]);
103  $d = $this->db->fetchAssoc($r);
104 
105  return (bool) $d['found'];
106  }
107 
111  public function store(StorableResource $resource): void
112  {
113  $rid = $resource->getIdentification()->serialize();
114 
115  $this->db->replace(
116  self::TABLE_NAME,
117  [
118  self::IDENTIFICATION => ['text', $rid]
119  ],
120  [
121  'storage_id' => ['text', $resource->getStorageID()],
122  'rtype' => ['text', $resource->getType()->value],
123  ]
124  );
125 
126  $this->cache[$rid] = $resource;
127  }
128 
132  public function delete(StorableResource $resource): void
133  {
134  $rid = $resource->getIdentification()->serialize();
135  $this->db->manipulateF(
136  "DELETE FROM " . self::TABLE_NAME . " WHERE " . self::IDENTIFICATION . " = %s",
137  ['text'],
138  [$rid]
139  );
140  unset($this->cache[$rid]);
141  }
142 
146  public function getAll(): \Generator
147  {
148  yield from [];
149  }
150 
151  public function preload(array $identification_strings): void
152  {
153  $r = $this->db->query(
154  "SELECT rid, storage_id, rtype FROM " . self::TABLE_NAME . " WHERE "
155  . $this->db->in(self::IDENTIFICATION, $identification_strings, false, 'text')
156  );
157  while ($d = $this->db->fetchAssoc($r)) {
158  $this->populateFromArray($d);
159  }
160  }
161 
162  public function populateFromArray(array $data): void
163  {
164  $resource = $this->blank(
165  new ResourceIdentification($data['rid']),
166  ResourceType::from($data['rtype'] ?? ResourceType::SINGLE_FILE->value)
167  );
168  $resource->setStorageID($data['storage_id']);
169  $this->cache[$data['rid']] = $resource;
170  }
171 }
blank(ResourceIdentification $identification, ResourceType $type=ResourceType::SINGLE_FILE)
$q
Definition: shib_logout.php:23
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$r