ILIAS  trunk Revision v12.0_alpha-1540-g00f839d5fa1
IRSSStreamHandler.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\WebDAV\Objects;
22
28use ILIAS\WebDAV\DataCheck;
30
35{
36 use DataCheck;
37
40 private \ilObjFileStakeholder $stakeholder;
41
42 public function __construct(
43 private ?ResourceIdentification $resource_identification
44 ) {
45 global $DIC; // TODO remove Service Locator
46 $this->manager = $DIC->resourceStorage()->manage();
47 $this->consumer = $DIC->resourceStorage()->consume();
48 $this->stakeholder = new \ilObjFileStakeholder();
49 }
50
51 public function get(): ?FileStream
52 {
53 if ($this->resource_identification === null) {
54 return null;
55 }
56 try {
57 return $this->consumer->stream($this->resource_identification)->getStream();
58 } catch (\Throwable) {
59 return null;
60 }
61 }
62
63 public function put(string $title, mixed $data, bool $publish): bool
64 {
65 if (is_resource($data)) {
66 $stream = Streams::ofResource($data);
67 } elseif (is_string($data) && $data !== '') {
68 $stream = Streams::ofString($data);
69 } else {
70 return false;
71 }
72
73 // Detect the empty `_Empty` placeholder revision created in
74 // TreeProxyRepository::createObject(). If the resource currently holds
75 // only that single DRAFT placeholder, remove it after the real
76 // revision has been published so the file does not appear with two
77 // versions (one empty, one with content).
78 $placeholder_revision_number = null;
79 $resource = $this->manager->getResource($this->resource_identification);
80 if (count($resource->getAllRevisionsIncludingDraft()) === 1) {
81 $only = $resource->getCurrentRevisionIncludingDraft();
82 if ($only->getStatus() === RevisionStatus::DRAFT) {
83 $placeholder_revision_number = $only->getVersionNumber();
84 }
85 }
86
87 $this->manager->appendNewRevisionFromStream(
88 $this->resource_identification,
89 $stream,
90 $this->stakeholder,
91 $title,
92 true
93 );
94
95 if ($publish) {
96 $this->manager->publish($this->resource_identification);
97 }
98
99 if ($placeholder_revision_number !== null && $publish) {
100 $this->manager->removeRevision(
101 $this->resource_identification,
102 $placeholder_revision_number
103 );
104 }
105
106 return true;
107 }
108
109 public function publish(): void
110 {
111 if ($this->manager->getCurrentRevisionIncludingDraft(
112 $this->resource_identification
113 )->getStatus() === RevisionStatus::DRAFT
114 ) {
115 $this->manager->publish($this->resource_identification);
116 }
117 }
118
119}
Stream factory which enables the user to create streams without the knowledge of the concrete class.
Definition: Streams.php:32
static ofString(string $string)
Creates a new stream with an initial value.
Definition: Streams.php:41
static ofResource($resource)
Wraps an already created resource with the stream abstraction.
Definition: Streams.php:64
__construct(private ?ResourceIdentification $resource_identification)
put(string $title, mixed $data, bool $publish)
The base interface for all filesystem streams.
Definition: FileStream.php:32
global $DIC
Definition: shib_login.php:26