ILIAS  trunk Revision v12.0_alpha-1540-g00f839d5fa1
CacheBackend.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\WebDAV\Lock;
22
24use Sabre\DAV\Locks\Backend\AbstractBackend;
28use Sabre\DAV\Locks\LockInfo;
32
36class CacheBackend extends AbstractBackend implements Request
37{
39
41
42 public function __construct(
43 private Factory $entity_factory
44 ) {
45 global $DIC;
46 $this->cache = $DIC->globalCache()->get($this);
47 $this->cache->flush();
48 $this->raw = new class () implements Transformation {
49 use DeriveApplyToFromTransform;
50 use DeriveInvokeFromTransform;
51
52 public function transform($from)
53 {
54 return $from;
55 }
56
57 };
58 }
59
60 public function getContainerKey(): string
61 {
62 return 'webdav:locks';
63 }
64
65 public function isForced(): bool
66 {
67 return true;
68 }
69
70 private function key(string $uri): string
71 {
72 return 'locks:' . md5($uri);
73 }
74
75 public function getLocks($uri, $returnChildLocks): array
76 {
77 $key = $this->key($uri);
78 if ($key === null) {
79 return [];
80 }
81
82 if (!$this->cache->has($key)) {
83 return [];
84 }
85
86 $locks = [];
87 foreach ($this->cache->get($key, $this->raw) as $item) {
88 $locks[] = $this->asLockInfo($item);
89 }
90
91 return $locks;
92 }
93
94 private function asArray(LockInfo $lockInfo): array
95 {
96 return [
97 'owner' => $lockInfo->owner,
98 'token' => $lockInfo->token,
99 'scope' => $lockInfo->scope,
100 'depth' => $lockInfo->depth,
101 'timeout' => $lockInfo->timeout,
102 'uri' => $lockInfo->uri,
103 'created' => $lockInfo->created,
104 ];
105 }
106
107 private function asLockInfo(array $item): LockInfo
108 {
109 $lock = new LockInfo();
110 $lock->owner = $item['owner'];
111 $lock->token = $item['token'];
112 $lock->scope = $item['scope'];
113 $lock->depth = $item['depth'];
114 $lock->timeout = $item['timeout'];
115 $lock->uri = $item['uri'];
116 $lock->created = $item['created'];
117
118 return $lock;
119 }
120
121 public function lock($uri, LockInfo $lockInfo): bool
122 {
123 $key = $this->key($uri);
124
125 $locks = $this->cache->get($key, $this->raw) ?? [];
126
127 $lock = new LockInfo();
128 $lock->owner = $lockInfo->owner;
129 $lock->token = $lockInfo->token;
130 $lock->scope = LockInfo::EXCLUSIVE;
131 $lock->depth = $lockInfo->depth;
132 $lock->timeout = $lockInfo->timeout;
133 $lock->uri = $uri;
134 $lock->created = $lockInfo->created ?? time();
135
136 $locks[] = $this->asArray($lock);
137
138 $this->cache->set($key, $locks);
139
140 return true;
141 }
142
143 public function unlock($uri, LockInfo $lockInfo): bool
144 {
145 $key = $this->key($uri);
146 if ($key === null) {
147 return false;
148 }
149 $this->cache->set($key, []);
150
151 $entity = $this->entity_factory->getByFullPath($uri);
152 $proxy = $entity?->getObjectProxy();
153 if ($proxy !== null && $proxy->getType() === Type::FILE) {
154 $proxy->getStreamHandler()->publish();
155 }
156
157 return true;
158 }
159}
asArray(LockInfo $lockInfo)
lock($uri, LockInfo $lockInfo)
__construct(private Factory $entity_factory)
getLocks($uri, $returnChildLocks)
unlock($uri, LockInfo $lockInfo)
A transformation is a function from one datatype to another.
global $DIC
Definition: shib_login.php:26