annotate core/lib/Drupal/Core/File/FileSystemInterface.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Core\File;
Chris@0 4
Chris@0 5 /**
Chris@0 6 * Provides an interface for helpers that operate on files and stream wrappers.
Chris@0 7 */
Chris@0 8 interface FileSystemInterface {
Chris@0 9
Chris@0 10 /**
Chris@18 11 * Flag for dealing with existing files: Appends number until name is unique.
Chris@18 12 */
Chris@18 13 const EXISTS_RENAME = 0;
Chris@18 14
Chris@18 15 /**
Chris@18 16 * Flag for dealing with existing files: Replace the existing file.
Chris@18 17 */
Chris@18 18 const EXISTS_REPLACE = 1;
Chris@18 19
Chris@18 20 /**
Chris@18 21 * Flag for dealing with existing files: Do nothing and return FALSE.
Chris@18 22 */
Chris@18 23 const EXISTS_ERROR = 2;
Chris@18 24
Chris@18 25 /**
Chris@18 26 * Flag used by ::prepareDirectory() -- create directory if not present.
Chris@18 27 */
Chris@18 28 const CREATE_DIRECTORY = 1;
Chris@18 29
Chris@18 30 /**
Chris@18 31 * Flag used by ::prepareDirectory() -- file permissions may be changed.
Chris@18 32 */
Chris@18 33 const MODIFY_PERMISSIONS = 2;
Chris@18 34
Chris@18 35 /**
Chris@0 36 * Moves an uploaded file to a new location.
Chris@0 37 *
Chris@0 38 * PHP's move_uploaded_file() does not properly support streams if
Chris@0 39 * open_basedir is enabled, so this function fills that gap.
Chris@0 40 *
Chris@0 41 * Compatibility: normal paths and stream wrappers.
Chris@0 42 *
Chris@0 43 * @param string $filename
Chris@0 44 * The filename of the uploaded file.
Chris@0 45 * @param string $uri
Chris@0 46 * A string containing the destination URI of the file.
Chris@0 47 *
Chris@0 48 * @return bool
Chris@0 49 * TRUE on success, or FALSE on failure.
Chris@0 50 *
Chris@0 51 * @see move_uploaded_file()
Chris@0 52 * @see https://www.drupal.org/node/515192
Chris@0 53 * @ingroup php_wrappers
Chris@0 54 */
Chris@0 55 public function moveUploadedFile($filename, $uri);
Chris@0 56
Chris@0 57 /**
Chris@0 58 * Sets the permissions on a file or directory.
Chris@0 59 *
Chris@0 60 * This function will use the file_chmod_directory and
Chris@0 61 * file_chmod_file settings for the default modes for directories
Chris@0 62 * and uploaded/generated files. By default these will give everyone read
Chris@0 63 * access so that users accessing the files with a user account without the
Chris@0 64 * webserver group (e.g. via FTP) can read these files, and give group write
Chris@0 65 * permissions so webserver group members (e.g. a vhost account) can alter
Chris@0 66 * files uploaded and owned by the webserver.
Chris@0 67 *
Chris@0 68 * PHP's chmod does not support stream wrappers so we use our wrapper
Chris@0 69 * implementation which interfaces with chmod() by default. Contrib wrappers
Chris@0 70 * may override this behavior in their implementations as needed.
Chris@0 71 *
Chris@0 72 * @param string $uri
Chris@0 73 * A string containing a URI file, or directory path.
Chris@0 74 * @param int $mode
Chris@0 75 * Integer value for the permissions. Consult PHP chmod() documentation for
Chris@0 76 * more information.
Chris@0 77 *
Chris@0 78 * @return bool
Chris@0 79 * TRUE for success, FALSE in the event of an error.
Chris@0 80 *
Chris@0 81 * @ingroup php_wrappers
Chris@0 82 */
Chris@0 83 public function chmod($uri, $mode = NULL);
Chris@0 84
Chris@0 85 /**
Chris@0 86 * Deletes a file.
Chris@0 87 *
Chris@0 88 * PHP's unlink() is broken on Windows, as it can fail to remove a file when
Chris@0 89 * it has a read-only flag set.
Chris@0 90 *
Chris@0 91 * @param string $uri
Chris@0 92 * A URI or pathname.
Chris@0 93 * @param resource $context
Chris@0 94 * Refer to http://php.net/manual/ref.stream.php
Chris@0 95 *
Chris@0 96 * @return bool
Chris@0 97 * Boolean TRUE on success, or FALSE on failure.
Chris@0 98 *
Chris@0 99 * @see unlink()
Chris@0 100 * @ingroup php_wrappers
Chris@0 101 */
Chris@0 102 public function unlink($uri, $context = NULL);
Chris@0 103
Chris@0 104 /**
Chris@0 105 * Resolves the absolute filepath of a local URI or filepath.
Chris@0 106 *
Chris@0 107 * The use of this method is discouraged, because it does not work for
Chris@0 108 * remote URIs. Except in rare cases, URIs should not be manually resolved.
Chris@0 109 *
Chris@0 110 * Only use this function if you know that the stream wrapper in the URI uses
Chris@0 111 * the local file system, and you need to pass an absolute path to a function
Chris@0 112 * that is incompatible with stream URIs.
Chris@0 113 *
Chris@0 114 * @param string $uri
Chris@0 115 * A stream wrapper URI or a filepath, possibly including one or more
Chris@0 116 * symbolic links.
Chris@0 117 *
Chris@0 118 * @return string|false
Chris@0 119 * The absolute local filepath (with no symbolic links) or FALSE on failure.
Chris@0 120 *
Chris@0 121 * @see \Drupal\Core\StreamWrapper\StreamWrapperInterface::realpath()
Chris@0 122 * @see http://php.net/manual/function.realpath.php
Chris@0 123 * @ingroup php_wrappers
Chris@0 124 */
Chris@0 125 public function realpath($uri);
Chris@0 126
Chris@0 127 /**
Chris@0 128 * Gets the name of the directory from a given path.
Chris@0 129 *
Chris@0 130 * PHP's dirname() does not properly pass streams, so this function fills that
Chris@0 131 * gap. It is backwards compatible with normal paths and will use PHP's
Chris@0 132 * dirname() as a fallback.
Chris@0 133 *
Chris@0 134 * Compatibility: normal paths and stream wrappers.
Chris@0 135 *
Chris@0 136 * @param string $uri
Chris@0 137 * A URI or path.
Chris@0 138 *
Chris@0 139 * @return string
Chris@0 140 * A string containing the directory name.
Chris@0 141 *
Chris@0 142 * @see dirname()
Chris@0 143 * @see https://www.drupal.org/node/515192
Chris@0 144 * @ingroup php_wrappers
Chris@0 145 */
Chris@0 146 public function dirname($uri);
Chris@0 147
Chris@0 148 /**
Chris@0 149 * Gets the filename from a given path.
Chris@0 150 *
Chris@0 151 * PHP's basename() does not properly support streams or filenames beginning
Chris@0 152 * with a non-US-ASCII character.
Chris@0 153 *
Chris@0 154 * @see http://bugs.php.net/bug.php?id=37738
Chris@0 155 * @see basename()
Chris@0 156 *
Chris@0 157 * @ingroup php_wrappers
Chris@0 158 */
Chris@0 159 public function basename($uri, $suffix = NULL);
Chris@0 160
Chris@0 161 /**
Chris@0 162 * Creates a directory, optionally creating missing components in the path to
Chris@0 163 * the directory.
Chris@0 164 *
Chris@0 165 * When PHP's mkdir() creates a directory, the requested mode is affected by
Chris@0 166 * the process's umask. This function overrides the umask and sets the mode
Chris@0 167 * explicitly for all directory components created.
Chris@0 168 *
Chris@0 169 * @param string $uri
Chris@0 170 * A URI or pathname.
Chris@0 171 * @param int $mode
Chris@0 172 * Mode given to created directories. Defaults to the directory mode
Chris@0 173 * configured in the Drupal installation. It must have a leading zero.
Chris@0 174 * @param bool $recursive
Chris@0 175 * Create directories recursively, defaults to FALSE. Cannot work with a
Chris@0 176 * mode which denies writing or execution to the owner of the process.
Chris@0 177 * @param resource $context
Chris@0 178 * Refer to http://php.net/manual/ref.stream.php
Chris@0 179 *
Chris@0 180 * @return bool
Chris@0 181 * Boolean TRUE on success, or FALSE on failure.
Chris@0 182 *
Chris@0 183 * @see mkdir()
Chris@0 184 * @see https://www.drupal.org/node/515192
Chris@0 185 * @ingroup php_wrappers
Chris@0 186 *
Chris@0 187 * @todo Update with open_basedir compatible recursion logic from
Chris@0 188 * \Drupal\Component\PhpStorage\FileStorage::ensureDirectory().
Chris@0 189 */
Chris@0 190 public function mkdir($uri, $mode = NULL, $recursive = FALSE, $context = NULL);
Chris@0 191
Chris@0 192 /**
Chris@0 193 * Removes a directory.
Chris@0 194 *
Chris@0 195 * PHP's rmdir() is broken on Windows, as it can fail to remove a directory
Chris@0 196 * when it has a read-only flag set.
Chris@0 197 *
Chris@0 198 * @param string $uri
Chris@0 199 * A URI or pathname.
Chris@0 200 * @param resource $context
Chris@0 201 * Refer to http://php.net/manual/ref.stream.php
Chris@0 202 *
Chris@0 203 * @return bool
Chris@0 204 * Boolean TRUE on success, or FALSE on failure.
Chris@0 205 *
Chris@0 206 * @see rmdir()
Chris@0 207 * @ingroup php_wrappers
Chris@0 208 */
Chris@0 209 public function rmdir($uri, $context = NULL);
Chris@0 210
Chris@0 211 /**
Chris@0 212 * Creates a file with a unique filename in the specified directory.
Chris@0 213 *
Chris@0 214 * PHP's tempnam() does not return a URI like we want. This function will
Chris@0 215 * return a URI if given a URI, or it will return a filepath if given a
Chris@0 216 * filepath.
Chris@0 217 *
Chris@0 218 * Compatibility: normal paths and stream wrappers.
Chris@0 219 *
Chris@0 220 * @param string $directory
Chris@0 221 * The directory where the temporary filename will be created.
Chris@0 222 * @param string $prefix
Chris@0 223 * The prefix of the generated temporary filename.
Chris@0 224 * Note: Windows uses only the first three characters of prefix.
Chris@0 225 *
Chris@0 226 * @return string|bool
Chris@0 227 * The new temporary filename, or FALSE on failure.
Chris@0 228 *
Chris@0 229 * @see tempnam()
Chris@0 230 * @see https://www.drupal.org/node/515192
Chris@0 231 * @ingroup php_wrappers
Chris@0 232 */
Chris@0 233 public function tempnam($directory, $prefix);
Chris@0 234
Chris@0 235 /**
Chris@0 236 * Returns the scheme of a URI (e.g. a stream).
Chris@0 237 *
Chris@0 238 * @param string $uri
Chris@0 239 * A stream, referenced as "scheme://target" or "data:target".
Chris@0 240 *
Chris@0 241 * @return string|bool
Chris@0 242 * A string containing the name of the scheme, or FALSE if none. For
Chris@0 243 * example, the URI "public://example.txt" would return "public".
Chris@0 244 *
Chris@0 245 * @see file_uri_target()
Chris@0 246 */
Chris@0 247 public function uriScheme($uri);
Chris@0 248
Chris@0 249 /**
Chris@0 250 * Checks that the scheme of a stream URI is valid.
Chris@0 251 *
Chris@0 252 * Confirms that there is a registered stream handler for the provided scheme
Chris@0 253 * and that it is callable. This is useful if you want to confirm a valid
Chris@0 254 * scheme without creating a new instance of the registered handler.
Chris@0 255 *
Chris@0 256 * @param string $scheme
Chris@0 257 * A URI scheme, a stream is referenced as "scheme://target".
Chris@0 258 *
Chris@0 259 * @return bool
Chris@0 260 * Returns TRUE if the string is the name of a validated stream, or FALSE if
Chris@0 261 * the scheme does not have a registered handler.
Chris@0 262 */
Chris@0 263 public function validScheme($scheme);
Chris@0 264
Chris@18 265 /**
Chris@18 266 * Copies a file to a new location without invoking the file API.
Chris@18 267 *
Chris@18 268 * This is a powerful function that in many ways performs like an advanced
Chris@18 269 * version of copy().
Chris@18 270 * - Checks if $source and $destination are valid and readable/writable.
Chris@18 271 * - If file already exists in $destination either the call will error out,
Chris@18 272 * replace the file or rename the file based on the $replace parameter.
Chris@18 273 * - If the $source and $destination are equal, the behavior depends on the
Chris@18 274 * $replace parameter. FILE_EXISTS_REPLACE will error out.
Chris@18 275 * FILE_EXISTS_RENAME will rename the file until the $destination is unique.
Chris@18 276 * - Provides a fallback using realpaths if the move fails using stream
Chris@18 277 * wrappers. This can occur because PHP's copy() function does not properly
Chris@18 278 * support streams if open_basedir is enabled. See
Chris@18 279 * https://bugs.php.net/bug.php?id=60456
Chris@18 280 *
Chris@18 281 * @param string $source
Chris@18 282 * A string specifying the filepath or URI of the source file.
Chris@18 283 * @param string $destination
Chris@18 284 * A URI containing the destination that $source should be copied to. The
Chris@18 285 * URI may be a bare filepath (without a scheme).
Chris@18 286 * @param int $replace
Chris@18 287 * Replace behavior when the destination file already exists:
Chris@18 288 * - FileManagerInterface::FILE_EXISTS_REPLACE - Replace the existing file.
Chris@18 289 * - FileManagerInterface::FILE_EXISTS_RENAME - Append _{incrementing
Chris@18 290 * number} until the filename is unique.
Chris@18 291 * - FileManagerInterface::FILE_EXISTS_ERROR - Throw an exception.
Chris@18 292 *
Chris@18 293 * @return string
Chris@18 294 * The path to the new file.
Chris@18 295 *
Chris@18 296 * @throws \Drupal\Core\File\Exception\FileException
Chris@18 297 * Implementation may throw FileException or its subtype on failure.
Chris@18 298 */
Chris@18 299 public function copy($source, $destination, $replace = self::EXISTS_RENAME);
Chris@18 300
Chris@18 301 /**
Chris@18 302 * Deletes a file without database changes or hook invocations.
Chris@18 303 *
Chris@18 304 * This function should be used when the file to be deleted does not have an
Chris@18 305 * entry recorded in the files table.
Chris@18 306 *
Chris@18 307 * @param string $path
Chris@18 308 * A string containing a file path or (streamwrapper) URI.
Chris@18 309 *
Chris@18 310 * @throws \Drupal\Core\File\Exception\FileException
Chris@18 311 * Implementation may throw FileException or its subtype on failure.
Chris@18 312 */
Chris@18 313 public function delete($path);
Chris@18 314
Chris@18 315 /**
Chris@18 316 * Deletes all files and directories in the specified filepath recursively.
Chris@18 317 *
Chris@18 318 * If the specified path is a directory then the function is called
Chris@18 319 * recursively to process the contents. Once the contents have been removed
Chris@18 320 * the directory is also removed.
Chris@18 321 *
Chris@18 322 * If the specified path is a file then it will be processed with delete()
Chris@18 323 * method.
Chris@18 324 *
Chris@18 325 * Note that this only deletes visible files with write permission.
Chris@18 326 *
Chris@18 327 * @param string $path
Chris@18 328 * A string containing either an URI or a file or directory path.
Chris@18 329 * @param callable|null $callback
Chris@18 330 * Callback function to run on each file prior to deleting it and on each
Chris@18 331 * directory prior to traversing it. For example, can be used to modify
Chris@18 332 * permissions.
Chris@18 333 *
Chris@18 334 * @throws \Drupal\Core\File\Exception\FileException
Chris@18 335 * Implementation may throw FileException or its subtype on failure.
Chris@18 336 */
Chris@18 337 public function deleteRecursive($path, callable $callback = NULL);
Chris@18 338
Chris@18 339 /**
Chris@18 340 * Moves a file to a new location without database changes or hook invocation.
Chris@18 341 *
Chris@18 342 * This is a powerful function that in many ways performs like an advanced
Chris@18 343 * version of rename().
Chris@18 344 * - Checks if $source and $destination are valid and readable/writable.
Chris@18 345 * - Checks that $source is not equal to $destination; if they are an error
Chris@18 346 * is reported.
Chris@18 347 * - If file already exists in $destination either the call will error out,
Chris@18 348 * replace the file or rename the file based on the $replace parameter.
Chris@18 349 * - Works around a PHP bug where rename() does not properly support streams
Chris@18 350 * if safe_mode or open_basedir are enabled.
Chris@18 351 *
Chris@18 352 * @param string $source
Chris@18 353 * A string specifying the filepath or URI of the source file.
Chris@18 354 * @param string $destination
Chris@18 355 * A URI containing the destination that $source should be moved to. The
Chris@18 356 * URI may be a bare filepath (without a scheme) and in that case the
Chris@18 357 * default scheme (public://) will be used.
Chris@18 358 * @param int $replace
Chris@18 359 * Replace behavior when the destination file already exists:
Chris@18 360 * - FILE_EXISTS_REPLACE - Replace the existing file.
Chris@18 361 * - FILE_EXISTS_RENAME - Append _{incrementing number} until the filename
Chris@18 362 * is unique.
Chris@18 363 * - FILE_EXISTS_ERROR - Do nothing and return FALSE.
Chris@18 364 *
Chris@18 365 * @return string
Chris@18 366 * The path to the new file.
Chris@18 367 *
Chris@18 368 * @throws \Drupal\Core\File\Exception\FileException
Chris@18 369 * Implementation may throw FileException or its subtype on failure.
Chris@18 370 *
Chris@18 371 * @see https://bugs.php.net/bug.php?id=60456
Chris@18 372 */
Chris@18 373 public function move($source, $destination, $replace = self::EXISTS_RENAME);
Chris@18 374
Chris@18 375 /**
Chris@18 376 * Saves a file to the specified destination without invoking file API.
Chris@18 377 *
Chris@18 378 * This function is identical to file_save_data() except the file will not be
Chris@18 379 * saved to the {file_managed} table and none of the file_* hooks will be
Chris@18 380 * called.
Chris@18 381 *
Chris@18 382 * @param string $data
Chris@18 383 * A string containing the contents of the file.
Chris@18 384 * @param string $destination
Chris@18 385 * A string containing the destination location. This must be a stream
Chris@18 386 * wrapper URI.
Chris@18 387 * @param int $replace
Chris@18 388 * Replace behavior when the destination file already exists:
Chris@18 389 * - FILE_EXISTS_REPLACE - Replace the existing file.
Chris@18 390 * - FILE_EXISTS_RENAME - Append _{incrementing number} until the filename
Chris@18 391 * is unique.
Chris@18 392 * - FILE_EXISTS_ERROR - Do nothing and return FALSE.
Chris@18 393 *
Chris@18 394 * @return string
Chris@18 395 * A string with the path of the resulting file, or FALSE on error.
Chris@18 396 *
Chris@18 397 * @throws \Drupal\Core\File\Exception\FileException
Chris@18 398 * Implementation may throw FileException or its subtype on failure.
Chris@18 399 *
Chris@18 400 * @see file_save_data()
Chris@18 401 */
Chris@18 402 public function saveData($data, $destination, $replace = self::EXISTS_RENAME);
Chris@18 403
Chris@18 404 /**
Chris@18 405 * Checks that the directory exists and is writable.
Chris@18 406 *
Chris@18 407 * Directories need to have execute permissions to be considered a directory
Chris@18 408 * by FTP servers, etc.
Chris@18 409 *
Chris@18 410 * @param string $directory
Chris@18 411 * A string reference containing the name of a directory path or URI. A
Chris@18 412 * trailing slash will be trimmed from a path.
Chris@18 413 * @param int $options
Chris@18 414 * A bitmask to indicate if the directory should be created if it does
Chris@18 415 * not exist (FileSystemInterface::CREATE_DIRECTORY) or made writable if it
Chris@18 416 * is read-only (FileSystemInterface::MODIFY_PERMISSIONS).
Chris@18 417 *
Chris@18 418 * @return bool
Chris@18 419 * TRUE if the directory exists (or was created) and is writable. FALSE
Chris@18 420 * otherwise.
Chris@18 421 */
Chris@18 422 public function prepareDirectory(&$directory, $options = self::MODIFY_PERMISSIONS);
Chris@18 423
Chris@18 424 /**
Chris@18 425 * Creates a full file path from a directory and filename.
Chris@18 426 *
Chris@18 427 * If a file with the specified name already exists, an alternative will be
Chris@18 428 * used.
Chris@18 429 *
Chris@18 430 * @param string $basename
Chris@18 431 * The filename.
Chris@18 432 * @param string $directory
Chris@18 433 * The directory or parent URI.
Chris@18 434 *
Chris@18 435 * @return string
Chris@18 436 * File path consisting of $directory and a unique filename based off
Chris@18 437 * of $basename.
Chris@18 438 *
Chris@18 439 * @throws \Drupal\Core\File\Exception\FileException
Chris@18 440 * Implementation may throw FileException or its subtype on failure.
Chris@18 441 */
Chris@18 442 public function createFilename($basename, $directory);
Chris@18 443
Chris@18 444 /**
Chris@18 445 * Determines the destination path for a file.
Chris@18 446 *
Chris@18 447 * @param string $destination
Chris@18 448 * The desired final URI or filepath.
Chris@18 449 * @param int $replace
Chris@18 450 * Replace behavior when the destination file already exists.
Chris@18 451 * - FileSystemInterface::EXISTS_REPLACE - Replace the existing file.
Chris@18 452 * - FileSystemInterface::EXISTS_RENAME - Append _{incrementing number}
Chris@18 453 * until the filename is unique.
Chris@18 454 * - FileSystemInterface::EXISTS_ERROR - Do nothing and return FALSE.
Chris@18 455 *
Chris@18 456 * @return string|bool
Chris@18 457 * The destination filepath, or FALSE if the file already exists
Chris@18 458 * and FileSystemInterface::EXISTS_ERROR is specified.
Chris@18 459 *
Chris@18 460 * @throws \Drupal\Core\File\Exception\FileException
Chris@18 461 * Implementation may throw FileException or its subtype on failure.
Chris@18 462 */
Chris@18 463 public function getDestinationFilename($destination, $replace);
Chris@18 464
Chris@0 465 }