3 namespace Drupal\Core\StreamWrapper;
6 * Defines a read-only Drupal stream wrapper base class for local files.
8 * This class extends the complete stream wrapper implementation in LocalStream.
9 * URIs such as "public://example.txt" are expanded to a normal filesystem path
10 * such as "sites/default/files/example.txt" and then PHP filesystem functions
13 * Drupal\Core\StreamWrapper\LocalReadOnlyStream implementations need to
14 * implement at least the getDirectoryPath() and getExternalUrl() methods.
16 abstract class LocalReadOnlyStream extends LocalStream {
21 public static function getType() {
22 return StreamWrapperInterface::READ_VISIBLE | StreamWrapperInterface::LOCAL;
28 public function stream_open($uri, $mode, $options, &$opened_path) {
29 if (!in_array($mode, ['r', 'rb', 'rt'])) {
30 if ($options & STREAM_REPORT_ERRORS) {
31 trigger_error('stream_open() write modes not supported for read-only stream wrappers', E_USER_WARNING);
35 return parent::stream_open($uri, $mode, $options, $opened_path);
39 * Support for flock().
41 * An exclusive lock attempt will be rejected, as this is a read-only stream
44 * @param int $operation
45 * One of the following:
46 * - LOCK_SH to acquire a shared lock (reader).
47 * - LOCK_EX to acquire an exclusive lock (writer).
48 * - LOCK_UN to release a lock (shared or exclusive).
49 * - LOCK_NB added as a bitmask if you don't want flock() to block while
50 * locking (not supported on Windows).
53 * Return FALSE for an exclusive lock (writer), as this is a read-only
54 * stream wrapper. Return the result of flock() for other valid operations.
55 * Defaults to TRUE if an invalid operation is passed.
57 * @see http://php.net/manual/streamwrapper.stream-lock.php
59 public function stream_lock($operation) {
60 // Disallow exclusive lock or non-blocking lock requests
61 if (in_array($operation, [LOCK_EX, LOCK_EX | LOCK_NB])) {
62 trigger_error('stream_lock() exclusive lock operations not supported for read-only stream wrappers', E_USER_WARNING);
65 if (in_array($operation, [LOCK_SH, LOCK_UN, LOCK_SH | LOCK_NB])) {
66 return flock($this->handle, $operation);
74 * Support for fwrite(), file_put_contents() etc.
76 * Data will not be written as this is a read-only stream wrapper.
79 * The string to be written.
82 * FALSE as data will not be written.
84 * @see http://php.net/manual/streamwrapper.stream-write.php
86 public function stream_write($data) {
87 trigger_error('stream_write() not supported for read-only stream wrappers', E_USER_WARNING);
92 * Support for fflush().
94 * Nothing will be output to the file, as this is a read-only stream wrapper.
95 * However as stream_flush is called during stream_close we should not trigger
99 * FALSE, as no data will be stored.
101 * @see http://php.net/manual/streamwrapper.stream-flush.php
103 public function stream_flush() {
110 * Does not change meta data as this is a read-only stream wrapper.
112 public function stream_metadata($uri, $option, $value) {
113 trigger_error('stream_metadata() not supported for read-only stream wrappers', E_USER_WARNING);
120 public function stream_truncate($new_size) {
121 trigger_error('stream_truncate() not supported for read-only stream wrappers', E_USER_WARNING);
126 * Support for unlink().
128 * The file will not be deleted from the stream as this is a read-only stream
132 * A string containing the uri to the resource to delete.
135 * TRUE so that file_delete() will remove db reference to file. File is not
138 * @see http://php.net/manual/streamwrapper.unlink.php
140 public function unlink($uri) {
141 trigger_error('unlink() not supported for read-only stream wrappers', E_USER_WARNING);
146 * Support for rename().
148 * The file will not be renamed as this is a read-only stream wrapper.
150 * @param string $from_uri,
151 * The uri to the file to rename.
152 * @param string $to_uri
153 * The new uri for file.
156 * FALSE as file will never be renamed.
158 * @see http://php.net/manual/streamwrapper.rename.php
160 public function rename($from_uri, $to_uri) {
161 trigger_error('rename() not supported for read-only stream wrappers', E_USER_WARNING);
166 * Support for mkdir().
168 * Directory will never be created as this is a read-only stream wrapper.
171 * A string containing the URI to the directory to create.
173 * Permission flags - see mkdir().
174 * @param int $options
175 * A bit mask of STREAM_REPORT_ERRORS and STREAM_MKDIR_RECURSIVE.
178 * FALSE as directory will never be created.
180 * @see http://php.net/manual/streamwrapper.mkdir.php
182 public function mkdir($uri, $mode, $options) {
183 trigger_error('mkdir() not supported for read-only stream wrappers', E_USER_WARNING);
188 * Support for rmdir().
190 * Directory will never be deleted as this is a read-only stream wrapper.
193 * A string containing the URI to the directory to delete.
194 * @param int $options
195 * A bit mask of STREAM_REPORT_ERRORS.
198 * FALSE as directory will never be deleted.
200 * @see http://php.net/manual/streamwrapper.rmdir.php
202 public function rmdir($uri, $options) {
203 trigger_error('rmdir() not supported for read-only stream wrappers', E_USER_WARNING);