3 namespace Drupal\Core\StreamWrapper;
6 * Defines a read-only Drupal stream wrapper base class.
8 * This class provides a minimal-read only stream wrapper implementation.
9 * Specifically, it only implements the writing classes and read classes where
10 * we need to restrict 'write-capable' arguments.
12 * Drupal\Core\StreamWrapper\ReadOnlyStream implementations need to implement
13 * all the read-related classes.
15 abstract class ReadOnlyStream implements StreamWrapperInterface {
17 * Stream context resource.
24 * A generic resource handle.
28 public $handle = NULL;
31 * Instance URI (stream).
33 * A stream is referenced as "scheme://target".
42 public function setUri($uri) {
49 public function getUri() {
54 * Support for fopen(), file_get_contents(), etc.
56 * Any write modes will be rejected, as this is a read-only stream wrapper.
59 * A string containing the URI to the file to open.
61 * The file mode, only strict readonly modes are supported.
63 * A bit mask of STREAM_USE_PATH and STREAM_REPORT_ERRORS.
64 * @param string $opened_path
65 * A string containing the path actually opened.
68 * TRUE if $mode denotes a readonly mode and the file was opened
69 * successfully, FALSE otherwise.
71 * @see http://php.net/manual/streamwrapper.stream-open.php
73 public function stream_open($uri, $mode, $options, &$opened_path) {
74 if (!in_array($mode, ['r', 'rb', 'rt'])) {
75 if ($options & STREAM_REPORT_ERRORS) {
76 trigger_error('stream_open() write modes not supported for read-only stream wrappers', E_USER_WARNING);
82 $path = $this->getLocalPath();
83 $this->handle = ($options & STREAM_REPORT_ERRORS) ? fopen($path, $mode) : @fopen($path, $mode);
85 if ($this->handle !== FALSE && ($options & STREAM_USE_PATH)) {
89 return (bool) $this->handle;
93 * Support for flock().
95 * An exclusive lock attempt will be rejected, as this is a read-only stream
98 * @param int $operation
99 * One of the following:
100 * - LOCK_SH to acquire a shared lock (reader).
101 * - LOCK_EX to acquire an exclusive lock (writer).
102 * - LOCK_UN to release a lock (shared or exclusive).
103 * - LOCK_NB if you don't want flock() to block while locking (not
104 * supported on Windows).
107 * Return FALSE for an exclusive lock (writer), as this is a read-only
108 * stream wrapper. Return the result of flock() for other valid operations.
109 * Defaults to TRUE if an invalid operation is passed.
111 * @see http://php.net/manual/streamwrapper.stream-lock.php
113 public function stream_lock($operation) {
114 if (in_array($operation, [LOCK_EX, LOCK_EX | LOCK_NB])) {
115 trigger_error('stream_lock() exclusive lock operations not supported for read-only stream wrappers', E_USER_WARNING);
118 if (in_array($operation, [LOCK_SH, LOCK_UN, LOCK_SH | LOCK_NB])) {
119 return flock($this->handle, $operation);
126 * Support for fwrite(), file_put_contents() etc.
128 * Data will not be written as this is a read-only stream wrapper.
130 * @param string $data
131 * The string to be written.
134 * FALSE as data will not be written.
136 * @see http://php.net/manual/streamwrapper.stream-write.php
138 public function stream_write($data) {
139 trigger_error('stream_write() not supported for read-only stream wrappers', E_USER_WARNING);
144 * Support for fflush().
146 * Nothing will be output to the file, as this is a read-only stream wrapper.
147 * However as stream_flush is called during stream_close we should not trigger
151 * FALSE, as no data will be stored.
153 * @see http://php.net/manual/streamwrapper.stream-flush.php
155 public function stream_flush() {
162 * Does not change meta data as this is a read-only stream wrapper.
164 public function stream_metadata($uri, $option, $value) {
165 trigger_error('stream_metadata() not supported for read-only stream wrappers', E_USER_WARNING);
172 public function stream_truncate($new_size) {
173 trigger_error('stream_truncate() not supported for read-only stream wrappers', E_USER_WARNING);
178 * Support for unlink().
180 * The file will not be deleted from the stream as this is a read-only stream
184 * A string containing the uri to the resource to delete.
187 * TRUE so that file_delete() will remove db reference to file. File is not
190 * @see http://php.net/manual/streamwrapper.unlink.php
192 public function unlink($uri) {
193 trigger_error('unlink() not supported for read-only stream wrappers', E_USER_WARNING);
198 * Support for rename().
200 * This file will not be renamed as this is a read-only stream wrapper.
202 * @param string $from_uri,
203 * The uri to the file to rename.
204 * @param string $to_uri
205 * The new uri for file.
208 * FALSE as file will never be renamed.
210 * @see http://php.net/manual/streamwrapper.rename.php
212 public function rename($from_uri, $to_uri) {
213 trigger_error('rename() not supported for read-only stream wrappers', E_USER_WARNING);
218 * Support for mkdir().
220 * Directory will never be created as this is a read-only stream wrapper.
223 * A string containing the URI to the directory to create.
225 * Permission flags - see mkdir().
226 * @param int $options
227 * A bit mask of STREAM_REPORT_ERRORS and STREAM_MKDIR_RECURSIVE.
230 * FALSE as directory will never be created.
232 * @see http://php.net/manual/streamwrapper.mkdir.php
234 public function mkdir($uri, $mode, $options) {
235 trigger_error('mkdir() not supported for read-only stream wrappers', E_USER_WARNING);
240 * Support for rmdir().
242 * Directory will never be deleted as this is a read-only stream wrapper.
245 * A string containing the URI to the directory to delete.
246 * @param int $options
247 * A bit mask of STREAM_REPORT_ERRORS.
250 * FALSE as directory will never be deleted.
252 * @see http://php.net/manual/streamwrapper.rmdir.php
254 public function rmdir($uri, $options) {
255 trigger_error('rmdir() not supported for read-only stream wrappers', E_USER_WARNING);