annotate core/lib/Drupal/Core/StreamWrapper/ReadOnlyStream.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Core\StreamWrapper;
Chris@0 4
Chris@0 5 /**
Chris@0 6 * Defines a read-only Drupal stream wrapper base class.
Chris@0 7 *
Chris@0 8 * This class provides a minimal-read only stream wrapper implementation.
Chris@0 9 * Specifically, it only implements the writing classes and read classes where
Chris@0 10 * we need to restrict 'write-capable' arguments.
Chris@0 11 *
Chris@0 12 * Drupal\Core\StreamWrapper\ReadOnlyStream implementations need to implement
Chris@0 13 * all the read-related classes.
Chris@0 14 */
Chris@0 15 abstract class ReadOnlyStream implements StreamWrapperInterface {
Chris@0 16 /**
Chris@0 17 * Stream context resource.
Chris@0 18 *
Chris@0 19 * @var resource
Chris@0 20 */
Chris@0 21 public $context;
Chris@0 22
Chris@0 23 /**
Chris@0 24 * A generic resource handle.
Chris@0 25 *
Chris@0 26 * @var resource
Chris@0 27 */
Chris@0 28 public $handle = NULL;
Chris@0 29
Chris@0 30 /**
Chris@0 31 * Instance URI (stream).
Chris@0 32 *
Chris@0 33 * A stream is referenced as "scheme://target".
Chris@0 34 *
Chris@0 35 * @var string
Chris@0 36 */
Chris@0 37 protected $uri;
Chris@0 38
Chris@0 39 /**
Chris@0 40 * {@inheritdoc}
Chris@0 41 */
Chris@0 42 public function setUri($uri) {
Chris@0 43 $this->uri = $uri;
Chris@0 44 }
Chris@0 45
Chris@0 46 /**
Chris@0 47 * {@inheritdoc}
Chris@0 48 */
Chris@0 49 public function getUri() {
Chris@0 50 return $this->uri;
Chris@0 51 }
Chris@0 52
Chris@0 53 /**
Chris@0 54 * Support for fopen(), file_get_contents(), etc.
Chris@0 55 *
Chris@0 56 * Any write modes will be rejected, as this is a read-only stream wrapper.
Chris@0 57 *
Chris@0 58 * @param string $uri
Chris@0 59 * A string containing the URI to the file to open.
Chris@0 60 * @param int $mode
Chris@0 61 * The file mode, only strict readonly modes are supported.
Chris@0 62 * @param int $options
Chris@0 63 * A bit mask of STREAM_USE_PATH and STREAM_REPORT_ERRORS.
Chris@0 64 * @param string $opened_path
Chris@0 65 * A string containing the path actually opened.
Chris@0 66 *
Chris@0 67 * @return bool
Chris@0 68 * TRUE if $mode denotes a readonly mode and the file was opened
Chris@0 69 * successfully, FALSE otherwise.
Chris@0 70 *
Chris@0 71 * @see http://php.net/manual/streamwrapper.stream-open.php
Chris@0 72 */
Chris@0 73 public function stream_open($uri, $mode, $options, &$opened_path) {
Chris@0 74 if (!in_array($mode, ['r', 'rb', 'rt'])) {
Chris@0 75 if ($options & STREAM_REPORT_ERRORS) {
Chris@0 76 trigger_error('stream_open() write modes not supported for read-only stream wrappers', E_USER_WARNING);
Chris@0 77 }
Chris@0 78 return FALSE;
Chris@0 79 }
Chris@0 80
Chris@0 81 $this->uri = $uri;
Chris@0 82 $path = $this->getLocalPath();
Chris@0 83 $this->handle = ($options & STREAM_REPORT_ERRORS) ? fopen($path, $mode) : @fopen($path, $mode);
Chris@0 84
Chris@0 85 if ($this->handle !== FALSE && ($options & STREAM_USE_PATH)) {
Chris@0 86 $opened_path = $path;
Chris@0 87 }
Chris@0 88
Chris@0 89 return (bool) $this->handle;
Chris@0 90 }
Chris@0 91
Chris@0 92 /**
Chris@0 93 * Support for flock().
Chris@0 94 *
Chris@0 95 * An exclusive lock attempt will be rejected, as this is a read-only stream
Chris@0 96 * wrapper.
Chris@0 97 *
Chris@0 98 * @param int $operation
Chris@0 99 * One of the following:
Chris@0 100 * - LOCK_SH to acquire a shared lock (reader).
Chris@0 101 * - LOCK_EX to acquire an exclusive lock (writer).
Chris@0 102 * - LOCK_UN to release a lock (shared or exclusive).
Chris@0 103 * - LOCK_NB if you don't want flock() to block while locking (not
Chris@0 104 * supported on Windows).
Chris@0 105 *
Chris@0 106 * @return bool
Chris@0 107 * Return FALSE for an exclusive lock (writer), as this is a read-only
Chris@0 108 * stream wrapper. Return the result of flock() for other valid operations.
Chris@0 109 * Defaults to TRUE if an invalid operation is passed.
Chris@0 110 *
Chris@0 111 * @see http://php.net/manual/streamwrapper.stream-lock.php
Chris@0 112 */
Chris@0 113 public function stream_lock($operation) {
Chris@0 114 if (in_array($operation, [LOCK_EX, LOCK_EX | LOCK_NB])) {
Chris@0 115 trigger_error('stream_lock() exclusive lock operations not supported for read-only stream wrappers', E_USER_WARNING);
Chris@0 116 return FALSE;
Chris@0 117 }
Chris@0 118 if (in_array($operation, [LOCK_SH, LOCK_UN, LOCK_SH | LOCK_NB])) {
Chris@0 119 return flock($this->handle, $operation);
Chris@0 120 }
Chris@0 121
Chris@0 122 return TRUE;
Chris@0 123 }
Chris@0 124
Chris@0 125 /**
Chris@0 126 * Support for fwrite(), file_put_contents() etc.
Chris@0 127 *
Chris@0 128 * Data will not be written as this is a read-only stream wrapper.
Chris@0 129 *
Chris@0 130 * @param string $data
Chris@0 131 * The string to be written.
Chris@0 132 *
Chris@0 133 * @return bool
Chris@0 134 * FALSE as data will not be written.
Chris@0 135 *
Chris@0 136 * @see http://php.net/manual/streamwrapper.stream-write.php
Chris@0 137 */
Chris@0 138 public function stream_write($data) {
Chris@0 139 trigger_error('stream_write() not supported for read-only stream wrappers', E_USER_WARNING);
Chris@0 140 return FALSE;
Chris@0 141 }
Chris@0 142
Chris@0 143 /**
Chris@0 144 * Support for fflush().
Chris@0 145 *
Chris@0 146 * Nothing will be output to the file, as this is a read-only stream wrapper.
Chris@0 147 * However as stream_flush is called during stream_close we should not trigger
Chris@0 148 * an error.
Chris@0 149 *
Chris@0 150 * @return bool
Chris@0 151 * FALSE, as no data will be stored.
Chris@0 152 *
Chris@0 153 * @see http://php.net/manual/streamwrapper.stream-flush.php
Chris@0 154 */
Chris@0 155 public function stream_flush() {
Chris@0 156 return FALSE;
Chris@0 157 }
Chris@0 158
Chris@0 159 /**
Chris@0 160 * {@inheritdoc}
Chris@0 161 *
Chris@0 162 * Does not change meta data as this is a read-only stream wrapper.
Chris@0 163 */
Chris@0 164 public function stream_metadata($uri, $option, $value) {
Chris@0 165 trigger_error('stream_metadata() not supported for read-only stream wrappers', E_USER_WARNING);
Chris@0 166 return FALSE;
Chris@0 167 }
Chris@0 168
Chris@0 169 /**
Chris@0 170 * {@inheritdoc}
Chris@0 171 */
Chris@0 172 public function stream_truncate($new_size) {
Chris@0 173 trigger_error('stream_truncate() not supported for read-only stream wrappers', E_USER_WARNING);
Chris@0 174 return FALSE;
Chris@0 175 }
Chris@0 176
Chris@0 177 /**
Chris@0 178 * Support for unlink().
Chris@0 179 *
Chris@0 180 * The file will not be deleted from the stream as this is a read-only stream
Chris@0 181 * wrapper.
Chris@0 182 *
Chris@0 183 * @param string $uri
Chris@0 184 * A string containing the uri to the resource to delete.
Chris@0 185 *
Chris@0 186 * @return bool
Chris@0 187 * TRUE so that file_delete() will remove db reference to file. File is not
Chris@0 188 * actually deleted.
Chris@0 189 *
Chris@0 190 * @see http://php.net/manual/streamwrapper.unlink.php
Chris@0 191 */
Chris@0 192 public function unlink($uri) {
Chris@0 193 trigger_error('unlink() not supported for read-only stream wrappers', E_USER_WARNING);
Chris@0 194 return TRUE;
Chris@0 195 }
Chris@0 196
Chris@0 197 /**
Chris@0 198 * Support for rename().
Chris@0 199 *
Chris@0 200 * This file will not be renamed as this is a read-only stream wrapper.
Chris@0 201 *
Chris@0 202 * @param string $from_uri
Chris@0 203 * The uri to the file to rename.
Chris@0 204 * @param string $to_uri
Chris@0 205 * The new uri for file.
Chris@0 206 *
Chris@0 207 * @return bool
Chris@0 208 * FALSE as file will never be renamed.
Chris@0 209 *
Chris@0 210 * @see http://php.net/manual/streamwrapper.rename.php
Chris@0 211 */
Chris@0 212 public function rename($from_uri, $to_uri) {
Chris@0 213 trigger_error('rename() not supported for read-only stream wrappers', E_USER_WARNING);
Chris@0 214 return FALSE;
Chris@0 215 }
Chris@0 216
Chris@0 217 /**
Chris@0 218 * Support for mkdir().
Chris@0 219 *
Chris@0 220 * Directory will never be created as this is a read-only stream wrapper.
Chris@0 221 *
Chris@0 222 * @param string $uri
Chris@0 223 * A string containing the URI to the directory to create.
Chris@0 224 * @param int $mode
Chris@0 225 * Permission flags - see mkdir().
Chris@0 226 * @param int $options
Chris@0 227 * A bit mask of STREAM_REPORT_ERRORS and STREAM_MKDIR_RECURSIVE.
Chris@0 228 *
Chris@0 229 * @return bool
Chris@0 230 * FALSE as directory will never be created.
Chris@0 231 *
Chris@0 232 * @see http://php.net/manual/streamwrapper.mkdir.php
Chris@0 233 */
Chris@0 234 public function mkdir($uri, $mode, $options) {
Chris@0 235 trigger_error('mkdir() not supported for read-only stream wrappers', E_USER_WARNING);
Chris@0 236 return FALSE;
Chris@0 237 }
Chris@0 238
Chris@0 239 /**
Chris@0 240 * Support for rmdir().
Chris@0 241 *
Chris@0 242 * Directory will never be deleted as this is a read-only stream wrapper.
Chris@0 243 *
Chris@0 244 * @param string $uri
Chris@0 245 * A string containing the URI to the directory to delete.
Chris@0 246 * @param int $options
Chris@0 247 * A bit mask of STREAM_REPORT_ERRORS.
Chris@0 248 *
Chris@0 249 * @return bool
Chris@0 250 * FALSE as directory will never be deleted.
Chris@0 251 *
Chris@0 252 * @see http://php.net/manual/streamwrapper.rmdir.php
Chris@0 253 */
Chris@0 254 public function rmdir($uri, $options) {
Chris@0 255 trigger_error('rmdir() not supported for read-only stream wrappers', E_USER_WARNING);
Chris@0 256 return FALSE;
Chris@0 257 }
Chris@0 258
Chris@0 259 }