Drupal 8.0.0 beta 12. More info: https://www.drupal.org/node/2514176

This commit is contained in:
Pantheon Automation 2015-08-17 17:00:26 -07:00 committed by Greg Anderson
commit 9921556621
13277 changed files with 1459781 additions and 0 deletions

View file

@ -0,0 +1,230 @@
<?php
/**
* @file
* Contains \Drupal\Core\StreamWrapper\LocalReadOnlyStream.
*/
namespace Drupal\Core\StreamWrapper;
/**
* Defines a read-only Drupal stream wrapper base class for local files.
*
* This class extends the complete stream wrapper implementation in LocalStream.
* URIs such as "public://example.txt" are expanded to a normal filesystem path
* such as "sites/default/files/example.txt" and then PHP filesystem functions
* are invoked.
*
* Drupal\Core\StreamWrapper\LocalReadOnlyStream implementations need to
* implement at least the getDirectoryPath() and getExternalUrl() methods.
*/
abstract class LocalReadOnlyStream extends LocalStream {
/**
* Support for fopen(), file_get_contents(), etc.
*
* Any write modes will be rejected, as this is a read-only stream wrapper.
*
* @param string $uri
* A string containing the URI to the file to open.
* @param int $mode
* The file mode, only strict readonly modes are supported.
* @param int $options
* A bit mask of STREAM_USE_PATH and STREAM_REPORT_ERRORS.
* @param string $opened_path
* A string containing the path actually opened.
*
* @return bool
* TRUE if $mode denotes a readonly mode and the file was opened
* successfully, FALSE otherwise.
*
* @see http://php.net/manual/streamwrapper.stream-open.php
*/
public function stream_open($uri, $mode, $options, &$opened_path) {
if (!in_array($mode, array('r', 'rb', 'rt'))) {
if ($options & STREAM_REPORT_ERRORS) {
trigger_error('stream_open() write modes not supported for read-only stream wrappers', E_USER_WARNING);
}
return FALSE;
}
$this->uri = $uri;
$path = $this->getLocalPath();
$this->handle = ($options & STREAM_REPORT_ERRORS) ? fopen($path, $mode) : @fopen($path, $mode);
if ($this->handle !== FALSE && ($options & STREAM_USE_PATH)) {
$opened_path = $path;
}
return (bool) $this->handle;
}
/**
* Support for flock().
*
* An exclusive lock attempt will be rejected, as this is a read-only stream
* wrapper.
*
* @param int $operation
* One of the following:
* - LOCK_SH to acquire a shared lock (reader).
* - LOCK_EX to acquire an exclusive lock (writer).
* - LOCK_UN to release a lock (shared or exclusive).
* - LOCK_NB added as a bitmask if you don't want flock() to block while
* locking (not supported on Windows).
*
* @return bool
* Return FALSE for an exclusive lock (writer), as this is a read-only
* stream wrapper. Return the result of flock() for other valid operations.
* Defaults to TRUE if an invalid operation is passed.
*
* @see http://php.net/manual/streamwrapper.stream-lock.php
*/
public function stream_lock($operation) {
// Disallow exclusive lock or non-blocking lock requests
if (in_array($operation, array(LOCK_EX, LOCK_EX|LOCK_NB))) {
trigger_error('stream_lock() exclusive lock operations not supported for read-only stream wrappers', E_USER_WARNING);
return FALSE;
}
if (in_array($operation, array(LOCK_SH, LOCK_UN, LOCK_SH|LOCK_NB))) {
return flock($this->handle, $operation);
}
return TRUE;
}
/**
* Support for fwrite(), file_put_contents() etc.
*
* Data will not be written as this is a read-only stream wrapper.
*
* @param string $data
* The string to be written.
*
* @return bool
* FALSE as data will not be written.
*
* @see http://php.net/manual/en/streamwrapper.stream-write.php
*/
public function stream_write($data) {
trigger_error('stream_write() not supported for read-only stream wrappers', E_USER_WARNING);
return FALSE;
}
/**
* Support for fflush().
*
* Nothing will be output to the file, as this is a read-only stream wrapper.
* However as stream_flush is called during stream_close we should not trigger
* an error.
*
* @return bool
* FALSE, as no data will be stored.
*
* @see http://php.net/manual/streamwrapper.stream-flush.php
*/
public function stream_flush() {
return FALSE;
}
/**
* {@inheritdoc}
*
* Does not change meta data as this is a read-only stream wrapper.
*/
public function stream_metadata($uri, $option, $value) {
trigger_error('stream_metadata() not supported for read-only stream wrappers', E_USER_WARNING);
return FALSE;
}
/**
* {@inheritdoc}
*/
public function stream_truncate($new_size) {
trigger_error('stream_truncate() not supported for read-only stream wrappers', E_USER_WARNING);
return FALSE;
}
/**
* Support for unlink().
*
* The file will not be deleted from the stream as this is a read-only stream
* wrapper.
*
* @param string $uri
* A string containing the uri to the resource to delete.
*
* @return bool
* TRUE so that file_delete() will remove db reference to file. File is not
* actually deleted.
*
* @see http://php.net/manual/en/streamwrapper.unlink.php
*/
public function unlink($uri) {
trigger_error('unlink() not supported for read-only stream wrappers', E_USER_WARNING);
return TRUE;
}
/**
* Support for rename().
*
* The file will not be renamed as this is a read-only stream wrapper.
*
* @param string $from_uri,
* The uri to the file to rename.
* @param string $to_uri
* The new uri for file.
*
* @return bool
* FALSE as file will never be renamed.
*
* @see http://php.net/manual/en/streamwrapper.rename.php
*/
public function rename($from_uri, $to_uri) {
trigger_error('rename() not supported for read-only stream wrappers', E_USER_WARNING);
return FALSE;
}
/**
* Support for mkdir().
*
* Directory will never be created as this is a read-only stream wrapper.
*
* @param string $uri
* A string containing the URI to the directory to create.
* @param int $mode
* Permission flags - see mkdir().
* @param int $options
* A bit mask of STREAM_REPORT_ERRORS and STREAM_MKDIR_RECURSIVE.
*
* @return bool
* FALSE as directory will never be created.
*
* @see http://php.net/manual/en/streamwrapper.mkdir.php
*/
public function mkdir($uri, $mode, $options) {
trigger_error('mkdir() not supported for read-only stream wrappers', E_USER_WARNING);
return FALSE;
}
/**
* Support for rmdir().
*
* Directory will never be deleted as this is a read-only stream wrapper.
*
* @param string $uri
* A string containing the URI to the directory to delete.
* @param int $options
* A bit mask of STREAM_REPORT_ERRORS.
*
* @return bool
* FALSE as directory will never be deleted.
*
* @see http://php.net/manual/en/streamwrapper.rmdir.php
*/
public function rmdir($uri, $options) {
trigger_error('rmdir() not supported for read-only stream wrappers', E_USER_WARNING);
return FALSE;
}
}

View file

@ -0,0 +1,566 @@
<?php
/**
* @file
* Contains \Drupal\Core\StreamWrapper\LocalStream.
*/
namespace Drupal\Core\StreamWrapper;
/**
* Defines a Drupal stream wrapper base class for local files.
*
* This class provides a complete stream wrapper implementation. URIs such as
* "public://example.txt" are expanded to a normal filesystem path such as
* "sites/default/files/example.txt" and then PHP filesystem functions are
* invoked.
*
* Drupal\Core\StreamWrapper\LocalStream implementations need to implement at least the
* getDirectoryPath() and getExternalUrl() methods.
*/
abstract class LocalStream implements StreamWrapperInterface {
/**
* Stream context resource.
*
* @var resource
*/
public $context;
/**
* A generic resource handle.
*
* @var resource
*/
public $handle = NULL;
/**
* Instance URI (stream).
*
* A stream is referenced as "scheme://target".
*
* @var string
*/
protected $uri;
/**
* {@inheritdoc}
*/
public static function getType() {
return StreamWrapperInterface::NORMAL;
}
/**
* Gets the path that the wrapper is responsible for.
*
* @todo Review this method name in D8 per https://www.drupal.org/node/701358.
*
* @return string
* String specifying the path.
*/
abstract function getDirectoryPath();
/**
* Implements Drupal\Core\StreamWrapper\StreamWrapperInterface::setUri().
*/
function setUri($uri) {
$this->uri = $uri;
}
/**
* Implements Drupal\Core\StreamWrapper\StreamWrapperInterface::getUri().
*/
function getUri() {
return $this->uri;
}
/**
* Returns the local writable target of the resource within the stream.
*
* This function should be used in place of calls to realpath() or similar
* functions when attempting to determine the location of a file. While
* functions like realpath() may return the location of a read-only file, this
* method may return a URI or path suitable for writing that is completely
* separate from the URI used for reading.
*
* @param string $uri
* Optional URI.
*
* @return string|bool
* Returns a string representing a location suitable for writing of a file,
* or FALSE if unable to write to the file such as with read-only streams.
*/
protected function getTarget($uri = NULL) {
if (!isset($uri)) {
$uri = $this->uri;
}
list(, $target) = explode('://', $uri, 2);
// Remove erroneous leading or trailing, forward-slashes and backslashes.
return trim($target, '\/');
}
/**
* Implements Drupal\Core\StreamWrapper\StreamWrapperInterface::realpath().
*/
function realpath() {
return $this->getLocalPath();
}
/**
* Returns the canonical absolute path of the URI, if possible.
*
* @param string $uri
* (optional) The stream wrapper URI to be converted to a canonical
* absolute path. This may point to a directory or another type of file.
*
* @return string|bool
* If $uri is not set, returns the canonical absolute path of the URI
* previously set by the
* Drupal\Core\StreamWrapper\StreamWrapperInterface::setUri() function.
* If $uri is set and valid for this class, returns its canonical absolute
* path, as determined by the realpath() function. If $uri is set but not
* valid, returns FALSE.
*/
protected function getLocalPath($uri = NULL) {
if (!isset($uri)) {
$uri = $this->uri;
}
$path = $this->getDirectoryPath() . '/' . $this->getTarget($uri);
$realpath = realpath($path);
if (!$realpath) {
// This file does not yet exist.
$realpath = realpath(dirname($path)) . '/' . drupal_basename($path);
}
$directory = realpath($this->getDirectoryPath());
if (!$realpath || !$directory || strpos($realpath, $directory) !== 0) {
return FALSE;
}
return $realpath;
}
/**
* Support for fopen(), file_get_contents(), file_put_contents() etc.
*
* @param string $uri
* A string containing the URI to the file to open.
* @param int $mode
* The file mode ("r", "wb" etc.).
* @param int $options
* A bit mask of STREAM_USE_PATH and STREAM_REPORT_ERRORS.
* @param string $opened_path
* A string containing the path actually opened.
*
* @return bool
* Returns TRUE if file was opened successfully.
*
* @see http://php.net/manual/streamwrapper.stream-open.php
*/
public function stream_open($uri, $mode, $options, &$opened_path) {
$this->uri = $uri;
$path = $this->getLocalPath();
$this->handle = ($options & STREAM_REPORT_ERRORS) ? fopen($path, $mode) : @fopen($path, $mode);
if ((bool) $this->handle && $options & STREAM_USE_PATH) {
$opened_path = $path;
}
return (bool) $this->handle;
}
/**
* Support for flock().
*
* @param int $operation
* One of the following:
* - LOCK_SH to acquire a shared lock (reader).
* - LOCK_EX to acquire an exclusive lock (writer).
* - LOCK_UN to release a lock (shared or exclusive).
* - LOCK_NB if you don't want flock() to block while locking (not
* supported on Windows).
*
* @return bool
* Always returns TRUE at the present time.
*
* @see http://php.net/manual/streamwrapper.stream-lock.php
*/
public function stream_lock($operation) {
if (in_array($operation, array(LOCK_SH, LOCK_EX, LOCK_UN, LOCK_NB))) {
return flock($this->handle, $operation);
}
return TRUE;
}
/**
* Support for fread(), file_get_contents() etc.
*
* @param int $count
* Maximum number of bytes to be read.
*
* @return string|bool
* The string that was read, or FALSE in case of an error.
*
* @see http://php.net/manual/streamwrapper.stream-read.php
*/
public function stream_read($count) {
return fread($this->handle, $count);
}
/**
* Support for fwrite(), file_put_contents() etc.
*
* @param string $data
* The string to be written.
*
* @return int
* The number of bytes written.
*
* @see http://php.net/manual/streamwrapper.stream-write.php
*/
public function stream_write($data) {
return fwrite($this->handle, $data);
}
/**
* Support for feof().
*
* @return bool
* TRUE if end-of-file has been reached.
*
* @see http://php.net/manual/streamwrapper.stream-eof.php
*/
public function stream_eof() {
return feof($this->handle);
}
/**
* {@inheritdoc}
*/
public function stream_seek($offset, $whence = SEEK_SET) {
// fseek returns 0 on success and -1 on a failure.
// stream_seek 1 on success and 0 on a failure.
return !fseek($this->handle, $offset, $whence);
}
/**
* Support for fflush().
*
* @return bool
* TRUE if data was successfully stored (or there was no data to store).
*
* @see http://php.net/manual/streamwrapper.stream-flush.php
*/
public function stream_flush() {
return fflush($this->handle);
}
/**
* Support for ftell().
*
* @return bool
* The current offset in bytes from the beginning of file.
*
* @see http://php.net/manual/streamwrapper.stream-tell.php
*/
public function stream_tell() {
return ftell($this->handle);
}
/**
* Support for fstat().
*
* @return bool
* An array with file status, or FALSE in case of an error - see fstat()
* for a description of this array.
*
* @see http://php.net/manual/streamwrapper.stream-stat.php
*/
public function stream_stat() {
return fstat($this->handle);
}
/**
* Support for fclose().
*
* @return bool
* TRUE if stream was successfully closed.
*
* @see http://php.net/manual/streamwrapper.stream-close.php
*/
public function stream_close() {
return fclose($this->handle);
}
/**
* {@inheritdoc}
*/
public function stream_cast($cast_as) {
return $this->handle ? $this->handle : FALSE;
}
/**
* {@inheritdoc}
*/
public function stream_metadata($uri, $option, $value) {
$target = $this->getLocalPath($uri);
$return = FALSE;
switch ($option) {
case STREAM_META_TOUCH:
if (!empty($value)) {
$return = touch($target, $value[0], $value[1]);
}
else {
$return = touch($target);
}
break;
case STREAM_META_OWNER_NAME:
case STREAM_META_OWNER:
$return = chown($target, $value);
break;
case STREAM_META_GROUP_NAME:
case STREAM_META_GROUP:
$return = chgrp($target, $value);
break;
case STREAM_META_ACCESS:
$return = chmod($target, $value);
break;
}
if ($return) {
// For convenience clear the file status cache of the underlying file,
// since metadata operations are often followed by file status checks.
clearstatcache(TRUE, $target);
}
return $return;
}
/**
* {@inheritdoc}
*
* Since Windows systems do not allow it and it is not needed for most use
* cases anyway, this method is not supported on local files and will trigger
* an error and return false. If needed, custom subclasses can provide
* OS-specific implementations for advanced use cases.
*/
public function stream_set_option($option, $arg1, $arg2) {
trigger_error('stream_set_option() not supported for local file based stream wrappers', E_USER_WARNING);
return FALSE;
}
/**
* {@inheritdoc}
*/
public function stream_truncate($new_size) {
return ftruncate($this->handle, $new_size);
}
/**
* Support for unlink().
*
* @param string $uri
* A string containing the URI to the resource to delete.
*
* @return bool
* TRUE if resource was successfully deleted.
*
* @see http://php.net/manual/streamwrapper.unlink.php
*/
public function unlink($uri) {
$this->uri = $uri;
return drupal_unlink($this->getLocalPath());
}
/**
* Support for rename().
*
* @param string $from_uri,
* The URI to the file to rename.
* @param string $to_uri
* The new URI for file.
*
* @return bool
* TRUE if file was successfully renamed.
*
* @see http://php.net/manual/streamwrapper.rename.php
*/
public function rename($from_uri, $to_uri) {
return rename($this->getLocalPath($from_uri), $this->getLocalPath($to_uri));
}
/**
* Gets the name of the directory from a given path.
*
* This method is usually accessed through drupal_dirname(), which wraps
* around the PHP dirname() function because it does not support stream
* wrappers.
*
* @param string $uri
* A URI or path.
*
* @return string
* A string containing the directory name.
*
* @see drupal_dirname()
*/
public function dirname($uri = NULL) {
list($scheme) = explode('://', $uri, 2);
$target = $this->getTarget($uri);
$dirname = dirname($target);
if ($dirname == '.') {
$dirname = '';
}
return $scheme . '://' . $dirname;
}
/**
* Support for mkdir().
*
* @param string $uri
* A string containing the URI to the directory to create.
* @param int $mode
* Permission flags - see mkdir().
* @param int $options
* A bit mask of STREAM_REPORT_ERRORS and STREAM_MKDIR_RECURSIVE.
*
* @return bool
* TRUE if directory was successfully created.
*
* @see http://php.net/manual/streamwrapper.mkdir.php
*/
public function mkdir($uri, $mode, $options) {
$this->uri = $uri;
$recursive = (bool) ($options & STREAM_MKDIR_RECURSIVE);
if ($recursive) {
// $this->getLocalPath() fails if $uri has multiple levels of directories
// that do not yet exist.
$localpath = $this->getDirectoryPath() . '/' . $this->getTarget($uri);
}
else {
$localpath = $this->getLocalPath($uri);
}
if ($options & STREAM_REPORT_ERRORS) {
return drupal_mkdir($localpath, $mode, $recursive);
}
else {
return @drupal_mkdir($localpath, $mode, $recursive);
}
}
/**
* Support for rmdir().
*
* @param string $uri
* A string containing the URI to the directory to delete.
* @param int $options
* A bit mask of STREAM_REPORT_ERRORS.
*
* @return bool
* TRUE if directory was successfully removed.
*
* @see http://php.net/manual/streamwrapper.rmdir.php
*/
public function rmdir($uri, $options) {
$this->uri = $uri;
if ($options & STREAM_REPORT_ERRORS) {
return drupal_rmdir($this->getLocalPath());
}
else {
return @drupal_rmdir($this->getLocalPath());
}
}
/**
* Support for stat().
*
* @param string $uri
* A string containing the URI to get information about.
* @param int $flags
* A bit mask of STREAM_URL_STAT_LINK and STREAM_URL_STAT_QUIET.
*
* @return array
* An array with file status, or FALSE in case of an error - see fstat()
* for a description of this array.
*
* @see http://php.net/manual/streamwrapper.url-stat.php
*/
public function url_stat($uri, $flags) {
$this->uri = $uri;
$path = $this->getLocalPath();
// Suppress warnings if requested or if the file or directory does not
// exist. This is consistent with PHP's plain filesystem stream wrapper.
if ($flags & STREAM_URL_STAT_QUIET || !file_exists($path)) {
return @stat($path);
}
else {
return stat($path);
}
}
/**
* Support for opendir().
*
* @param string $uri
* A string containing the URI to the directory to open.
* @param int $options
* Unknown (parameter is not documented in PHP Manual).
*
* @return bool
* TRUE on success.
*
* @see http://php.net/manual/streamwrapper.dir-opendir.php
*/
public function dir_opendir($uri, $options) {
$this->uri = $uri;
$this->handle = opendir($this->getLocalPath());
return (bool) $this->handle;
}
/**
* Support for readdir().
*
* @return string
* The next filename, or FALSE if there are no more files in the directory.
*
* @see http://php.net/manual/streamwrapper.dir-readdir.php
*/
public function dir_readdir() {
return readdir($this->handle);
}
/**
* Support for rewinddir().
*
* @return bool
* TRUE on success.
*
* @see http://php.net/manual/streamwrapper.dir-rewinddir.php
*/
public function dir_rewinddir() {
rewinddir($this->handle);
// We do not really have a way to signal a failure as rewinddir() does not
// have a return value and there is no way to read a directory handler
// without advancing to the next file.
return TRUE;
}
/**
* Support for closedir().
*
* @return bool
* TRUE on success.
*
* @see http://php.net/manual/streamwrapper.dir-closedir.php
*/
public function dir_closedir() {
closedir($this->handle);
// We do not really have a way to signal a failure as closedir() does not
// have a return value.
return TRUE;
}
}

View file

@ -0,0 +1,228 @@
<?php
/**
* @file
* Contains \Drupal\Core\StreamWrapper\PhpStreamWrapperInterface.
*/
namespace Drupal\Core\StreamWrapper;
/**
* Defines a generic PHP stream wrapper interface.
*
* @see http://www.php.net/manual/class.streamwrapper.php
*/
interface PhpStreamWrapperInterface {
/**
* @return bool
*/
public function dir_closedir();
/**
* @return bool
*/
public function dir_opendir($path, $options);
/**
* @return string
*/
public function dir_readdir();
/**
* @return bool
*/
public function dir_rewinddir();
/**
* @return bool
*/
public function mkdir($path, $mode, $options);
/**
* @return bool
*/
public function rename($path_from, $path_to);
/**
* @return bool
*/
public function rmdir($path, $options);
/**
* Retrieve the underlying stream resource.
*
* This method is called in response to stream_select().
*
* @param int $cast_as
* Can be STREAM_CAST_FOR_SELECT when stream_select() is calling
* stream_cast() or STREAM_CAST_AS_STREAM when stream_cast() is called for
* other uses.
*
* @return resource|false
* The underlying stream resource or FALSE if stream_select() is not
* supported.
*
* @see stream_select()
* @see http://php.net/manual/streamwrapper.stream-cast.php
*/
public function stream_cast($cast_as);
/**
* @return void
*/
public function stream_close();
/**
* @return bool
*/
public function stream_eof();
/**
* @return bool
*/
public function stream_flush();
/**
* @return bool
*/
public function stream_lock($operation);
/**
* Sets metadata on the stream.
*
* @param string $path
* A string containing the URI to the file to set metadata on.
* @param int $option
* One of:
* - STREAM_META_TOUCH: The method was called in response to touch().
* - STREAM_META_OWNER_NAME: The method was called in response to chown()
* with string parameter.
* - STREAM_META_OWNER: The method was called in response to chown().
* - STREAM_META_GROUP_NAME: The method was called in response to chgrp().
* - STREAM_META_GROUP: The method was called in response to chgrp().
* - STREAM_META_ACCESS: The method was called in response to chmod().
* @param mixed $value
* If option is:
* - STREAM_META_TOUCH: Array consisting of two arguments of the touch()
* function.
* - STREAM_META_OWNER_NAME or STREAM_META_GROUP_NAME: The name of the owner
* user/group as string.
* - STREAM_META_OWNER or STREAM_META_GROUP: The value of the owner
* user/group as integer.
* - STREAM_META_ACCESS: The argument of the chmod() as integer.
*
* @return bool
* Returns TRUE on success or FALSE on failure. If $option is not
* implemented, FALSE should be returned.
*
* @see http://www.php.net/manual/streamwrapper.stream-metadata.php
*/
public function stream_metadata($path, $option, $value);
/**
* @return bool
*/
public function stream_open($path, $mode, $options, &$opened_path);
/**
* @return string
*/
public function stream_read($count);
/**
* Seeks to specific location in a stream.
*
* This method is called in response to fseek().
*
* The read/write position of the stream should be updated according to the
* offset and whence.
*
* @param int $offset
* The byte offset to seek to.
* @param int $whence
* Possible values:
* - SEEK_SET: Set position equal to offset bytes.
* - SEEK_CUR: Set position to current location plus offset.
* - SEEK_END: Set position to end-of-file plus offset.
* Defaults to SEEK_SET.
*
* @return bool
* TRUE if the position was updated, FALSE otherwise.
*
* @see http://php.net/manual/streamwrapper.stream-seek.php
*/
public function stream_seek($offset, $whence = SEEK_SET);
/**
* Change stream options.
*
* This method is called to set options on the stream.
*
* @param int $option
* One of:
* - STREAM_OPTION_BLOCKING: The method was called in response to
* stream_set_blocking().
* - STREAM_OPTION_READ_TIMEOUT: The method was called in response to
* stream_set_timeout().
* - STREAM_OPTION_WRITE_BUFFER: The method was called in response to
* stream_set_write_buffer().
* @param int $arg1
* If option is:
* - STREAM_OPTION_BLOCKING: The requested blocking mode:
* - 1 means blocking.
* - 0 means not blocking.
* - STREAM_OPTION_READ_TIMEOUT: The timeout in seconds.
* - STREAM_OPTION_WRITE_BUFFER: The buffer mode, STREAM_BUFFER_NONE or
* STREAM_BUFFER_FULL.
* @param int $arg2
* If option is:
* - STREAM_OPTION_BLOCKING: This option is not set.
* - STREAM_OPTION_READ_TIMEOUT: The timeout in microseconds.
* - STREAM_OPTION_WRITE_BUFFER: The requested buffer size.
*
* @return bool
* TRUE on success, FALSE otherwise. If $option is not implemented, FALSE
* should be returned.
*/
public function stream_set_option($option, $arg1, $arg2);
/**
* @return array
*/
public function stream_stat();
/**
* @return int
*/
public function stream_tell();
/**
* Truncate stream.
*
* Will respond to truncation; e.g., through ftruncate().
*
* @param int $new_size
* The new size.
*
* @return bool
* TRUE on success, FALSE otherwise.
*/
public function stream_truncate($new_size);
/**
* @return int
*/
public function stream_write($data);
/**
* @return bool
*/
public function unlink($path);
/**
* @return array
*/
public function url_stat($path, $flags);
}

View file

@ -0,0 +1,69 @@
<?php
/**
* @file
* Contains \Drupal\Core\StreamWrapper\PrivateStream.
*/
namespace Drupal\Core\StreamWrapper;
use Drupal\Core\Routing\UrlGeneratorTrait;
use Drupal\Core\Site\Settings;
/**
* Drupal private (private://) stream wrapper class.
*
* Provides support for storing privately accessible files with the Drupal file
* interface.
*/
class PrivateStream extends LocalStream {
use UrlGeneratorTrait;
/**
* {@inheritdoc}
*/
public static function getType() {
return StreamWrapperInterface::LOCAL_NORMAL;
}
/**
* {@inheritdoc}
*/
public function getName() {
return t('Private files');
}
/**
* {@inheritdoc}
*/
public function getDescription() {
return t('Private local files served by Drupal.');
}
/**
* {@inheritdoc}
*/
public function getDirectoryPath() {
return static::basePath();
}
/**
* {@inheritdoc}
*/
public function getExternalUrl() {
$path = str_replace('\\', '/', $this->getTarget());
return $this->url('system.private_file_download', ['filepath' => $path], ['absolute' => TRUE]);
}
/**
* Returns the base path for private://.
*
* @return string
* The base path for private://.
*/
public static function basePath() {
return Settings::get('file_private_path');
}
}

View file

@ -0,0 +1,97 @@
<?php
/**
* @file
* Contains \Drupal\Core\StreamWrapper\PublicStream.
*/
namespace Drupal\Core\StreamWrapper;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\DrupalKernel;
use Drupal\Core\Site\Settings;
use Symfony\Component\HttpFoundation\Request;
/**
* Defines a Drupal public (public://) stream wrapper class.
*
* Provides support for storing publicly accessible files with the Drupal file
* interface.
*/
class PublicStream extends LocalStream {
/**
* {@inheritdoc}
*/
public static function getType() {
return StreamWrapperInterface::LOCAL_NORMAL;
}
/**
* {@inheritdoc}
*/
public function getName() {
return t('Public files');
}
/**
* {@inheritdoc}
*/
public function getDescription() {
return t('Public local files served by the webserver.');
}
/**
* {@inheritdoc}
*/
public function getDirectoryPath() {
return static::basePath();
}
/**
* {@inheritdoc}
*/
public function getExternalUrl() {
$path = str_replace('\\', '/', $this->getTarget());
return $GLOBALS['base_url'] . '/' . self::getDirectoryPath() . '/' . UrlHelper::encodePath($path);
}
/**
* Returns the base path for public://.
*
* If we have a setting for the public:// scheme's path, we use that.
* Otherwise we build a reasonable default based on the site.path service if
* it's available, or a default behavior based on the request.
*
* The site path is injectable from the site.path service:
* @code
* $base_path = PublicStream::basePath(\Drupal::service('site.path'));
* @endcode
*
* @param \SplString $site_path
* (optional) The site.path service parameter, which is typically the path
* to sites/ in a Drupal installation. This allows you to inject the site
* path using services from the caller. If omitted, this method will use the
* global service container or the kernel's default behavior to determine
* the site path.
*
* @return string
* The base path for public:// typically sites/default/files.
*/
public static function basePath(\SplString $site_path = NULL) {
if ($site_path === NULL) {
// Find the site path. Kernel service is not always available at this
// point, but is preferred, when available.
if (\Drupal::hasService('kernel')) {
$site_path = \Drupal::service('site.path');
}
else {
// If there is no kernel available yet, we call the static
// findSitePath().
$site_path = DrupalKernel::findSitePath(Request::createFromGlobals());
}
}
return Settings::get('file_public_path', $site_path . '/files');
}
}

View file

@ -0,0 +1,264 @@
<?php
/**
* @file
* Contains \Drupal\Core\StreamWrapper\ReadOnlyStream.
*/
namespace Drupal\Core\StreamWrapper;
/**
* Defines a read-only Drupal stream wrapper base class.
*
* This class provides a minimal-read only stream wrapper implementation.
* Specifically, it only implements the writing classes and read classes where
* we need to restrict 'write-capable' arguments.
*
* Drupal\Core\StreamWrapper\ReadOnlyStream implementations need to implement
* all the read-related classes.
*/
abstract class ReadOnlyStream implements StreamWrapperInterface {
/**
* Stream context resource.
*
* @var resource
*/
public $context;
/**
* A generic resource handle.
*
* @var resource
*/
public $handle = NULL;
/**
* Instance URI (stream).
*
* A stream is referenced as "scheme://target".
*
* @var string
*/
protected $uri;
/**
* Implements Drupal\Core\StreamWrapper\StreamWrapperInterface::setUri().
*/
function setUri($uri) {
$this->uri = $uri;
}
/**
* Implements Drupal\Core\StreamWrapper\StreamWrapperInterface::getUri().
*/
function getUri() {
return $this->uri;
}
/**
* Support for fopen(), file_get_contents(), etc.
*
* Any write modes will be rejected, as this is a read-only stream wrapper.
*
* @param string $uri
* A string containing the URI to the file to open.
* @param int $mode
* The file mode, only strict readonly modes are supported.
* @param int $options
* A bit mask of STREAM_USE_PATH and STREAM_REPORT_ERRORS.
* @param string $opened_path
* A string containing the path actually opened.
*
* @return bool
* TRUE if $mode denotes a readonly mode and the file was opened
* successfully, FALSE otherwise.
*
* @see http://php.net/manual/streamwrapper.stream-open.php
*/
public function stream_open($uri, $mode, $options, &$opened_path) {
if (!in_array($mode, array('r', 'rb', 'rt'))) {
if ($options & STREAM_REPORT_ERRORS) {
trigger_error('stream_open() write modes not supported for read-only stream wrappers', E_USER_WARNING);
}
return FALSE;
}
$this->uri = $uri;
$path = $this->getLocalPath();
$this->handle = ($options & STREAM_REPORT_ERRORS) ? fopen($path, $mode) : @fopen($path, $mode);
if ($this->handle !== FALSE && ($options & STREAM_USE_PATH)) {
$opened_path = $path;
}
return (bool) $this->handle;
}
/**
* Support for flock().
*
* An exclusive lock attempt will be rejected, as this is a read-only stream
* wrapper.
*
* @param int $operation
* One of the following:
* - LOCK_SH to acquire a shared lock (reader).
* - LOCK_EX to acquire an exclusive lock (writer).
* - LOCK_UN to release a lock (shared or exclusive).
* - LOCK_NB if you don't want flock() to block while locking (not
* supported on Windows).
*
* @return bool
* Return FALSE for an exclusive lock (writer), as this is a read-only
* stream wrapper. Return the result of flock() for other valid operations.
* Defaults to TRUE if an invalid operation is passed.
*
* @see http://php.net/manual/streamwrapper.stream-lock.php
*/
public function stream_lock($operation) {
if (in_array($operation, array(LOCK_EX, LOCK_EX|LOCK_NB))) {
trigger_error('stream_lock() exclusive lock operations not supported for read-only stream wrappers', E_USER_WARNING);
return FALSE;
}
if (in_array($operation, array(LOCK_SH, LOCK_UN, LOCK_SH|LOCK_NB))) {
return flock($this->handle, $operation);
}
return TRUE;
}
/**
* Support for fwrite(), file_put_contents() etc.
*
* Data will not be written as this is a read-only stream wrapper.
*
* @param string $data
* The string to be written.
*
* @return bool
* FALSE as data will not be written.
*
* @see http://php.net/manual/en/streamwrapper.stream-write.php
*/
public function stream_write($data) {
trigger_error('stream_write() not supported for read-only stream wrappers', E_USER_WARNING);
return FALSE;
}
/**
* Support for fflush().
*
* Nothing will be output to the file, as this is a read-only stream wrapper.
* However as stream_flush is called during stream_close we should not trigger
* an error.
*
* @return bool
* FALSE, as no data will be stored.
*
* @see http://php.net/manual/streamwrapper.stream-flush.php
*/
public function stream_flush() {
return FALSE;
}
/**
* {@inheritdoc}
*
* Does not change meta data as this is a read-only stream wrapper.
*/
public function stream_metadata($uri, $option, $value) {
trigger_error('stream_metadata() not supported for read-only stream wrappers', E_USER_WARNING);
return FALSE;
}
/**
* {@inheritdoc}
*/
public function stream_truncate($new_size) {
trigger_error('stream_truncate() not supported for read-only stream wrappers', E_USER_WARNING);
return FALSE;
}
/**
* Support for unlink().
*
* The file will not be deleted from the stream as this is a read-only stream
* wrapper.
*
* @param string $uri
* A string containing the uri to the resource to delete.
*
* @return bool
* TRUE so that file_delete() will remove db reference to file. File is not
* actually deleted.
*
* @see http://php.net/manual/en/streamwrapper.unlink.php
*/
public function unlink($uri) {
trigger_error('unlink() not supported for read-only stream wrappers', E_USER_WARNING);
return TRUE;
}
/**
* Support for rename().
*
* This file will not be renamed as this is a read-only stream wrapper.
*
* @param string $from_uri,
* The uri to the file to rename.
* @param string $to_uri
* The new uri for file.
*
* @return bool
* FALSE as file will never be renamed.
*
* @see http://php.net/manual/en/streamwrapper.rename.php
*/
public function rename($from_uri, $to_uri) {
trigger_error('rename() not supported for read-only stream wrappers', E_USER_WARNING);
return FALSE;
}
/**
* Support for mkdir().
*
* Directory will never be created as this is a read-only stream wrapper.
*
* @param string $uri
* A string containing the URI to the directory to create.
* @param int $mode
* Permission flags - see mkdir().
* @param int $options
* A bit mask of STREAM_REPORT_ERRORS and STREAM_MKDIR_RECURSIVE.
*
* @return bool
* FALSE as directory will never be created.
*
* @see http://php.net/manual/en/streamwrapper.mkdir.php
*/
public function mkdir($uri, $mode, $options) {
trigger_error('mkdir() not supported for read-only stream wrappers', E_USER_WARNING);
return FALSE;
}
/**
* Support for rmdir().
*
* Directory will never be deleted as this is a read-only stream wrapper.
*
* @param string $uri
* A string containing the URI to the directory to delete.
* @param int $options
* A bit mask of STREAM_REPORT_ERRORS.
*
* @return bool
* FALSE as directory will never be deleted.
*
* @see http://php.net/manual/en/streamwrapper.rmdir.php
*/
public function rmdir($uri, $options) {
trigger_error('rmdir() not supported for read-only stream wrappers', E_USER_WARNING);
return FALSE;
}
}

View file

@ -0,0 +1,189 @@
<?php
/**
* @file
* Contains \Drupal\Core\StreamWrapper\StreamWrapperInterface.
*
* Provides a Drupal interface and classes to implement PHP stream wrappers for
* public, private, and temporary files.
*
* A stream wrapper is an abstraction of a file system that allows Drupal to
* use the same set of methods to access both local files and remote resources.
*
* Note that PHP 5.2 fopen() only supports URIs of the form "scheme://target"
* despite the fact that according to RFC 3986 a URI's scheme component
* delimiter is in general just ":", not "://". Because of this PHP limitation
* and for consistency Drupal will only accept URIs of form "scheme://target".
*
* @see http://www.faqs.org/rfcs/rfc3986.html
* @see http://bugs.php.net/bug.php?id=47070
*/
namespace Drupal\Core\StreamWrapper;
/**
* Defines a Drupal stream wrapper extension.
*
* Extends the StreamWrapperInterface with methods expected by Drupal stream
* wrapper classes.
*/
interface StreamWrapperInterface extends PhpStreamWrapperInterface {
/**
* Stream wrapper bit flags that are the basis for composite types.
*
* Note that 0x0002 is skipped, because it was the value of a constant that
* has since been removed.
*/
/**
* A filter that matches all wrappers.
*/
const ALL = 0x0000;
/**
* Refers to a local file system location.
*/
const LOCAL = 0x0001;
/**
* Wrapper is readable (almost always true).
*/
const READ = 0x0004;
/**
* Wrapper is writeable.
*/
const WRITE = 0x0008;
/**
* Exposed in the UI and potentially web accessible.
*/
const VISIBLE = 0x0010;
/**
* Composite stream wrapper bit flags that are usually used as the types.
*/
/**
* Not visible in the UI or accessible via web, but readable and writable.
* E.g. the temporary directory for uploads.
*/
const HIDDEN = 0x000C;
/**
* Hidden, readable and writeable using local files.
*/
const LOCAL_HIDDEN = 0x000D;
/**
* Visible, readable and writeable.
*/
const WRITE_VISIBLE = 0x001C;
/**
* Visible and read-only.
*/
const READ_VISIBLE = 0x0014;
/**
* This is the default 'type' falg. This does not include
* StreamWrapperInterface::LOCAL, because PHP grants a greater trust level to
* local files (for example, they can be used in an "include" statement,
* regardless of the "allow_url_include" setting), so stream wrappers need to
* explicitly opt-in to this.
*/
const NORMAL = 0x001C;
/**
* Visible, readable and writeable using local files.
*/
const LOCAL_NORMAL = 0x001D;
/**
* Returns the type of stream wrapper.
*
* @return int
*/
public static function getType();
/**
* Returns the name of the stream wrapper for use in the UI.
*
* @return string
* The stream wrapper name.
*/
public function getName();
/**
* Returns the description of the stream wrapper for use in the UI.
*
* @return string
* The stream wrapper description.
*/
public function getDescription();
/**
* Sets the absolute stream resource URI.
*
* This allows you to set the URI. Generally is only called by the factory
* method.
*
* @param string $uri
* A string containing the URI that should be used for this instance.
*/
public function setUri($uri);
/**
* Returns the stream resource URI.
*
* @return string
* Returns the current URI of the instance.
*/
public function getUri();
/**
* Returns a web accessible URL for the resource.
*
* This function should return a URL that can be embedded in a web page
* and accessed from a browser. For example, the external URL of
* "youtube://xIpLd0WQKCY" might be
* "http://www.youtube.com/watch?v=xIpLd0WQKCY".
*
* @return string
* Returns a string containing a web accessible URL for the resource.
*/
public function getExternalUrl();
/**
* Returns canonical, absolute path of the resource.
*
* Implementation placeholder. PHP's realpath() does not support stream
* wrappers. We provide this as a default so that individual wrappers may
* implement their own solutions.
*
* @return string
* Returns a string with absolute pathname on success (implemented
* by core wrappers), or FALSE on failure or if the registered
* wrapper does not provide an implementation.
*/
public function realpath();
/**
* Gets the name of the directory from a given path.
*
* This method is usually accessed through drupal_dirname(), which wraps
* around the normal PHP dirname() function, which does not support stream
* wrappers.
*
* @param string $uri
* An optional URI.
*
* @return string
* A string containing the directory name, or FALSE if not applicable.
*
* @see drupal_dirname()
*/
public function dirname($uri = NULL);
}

View file

@ -0,0 +1,214 @@
<?php
/**
* @file
* Contains \Drupal\Core\StreamWrapper\StreamWrapperManager.
*/
namespace Drupal\Core\StreamWrapper;
use Symfony\Component\DependencyInjection\ContainerAware;
/**
* Provides a StreamWrapper manager.
*
* @see file_get_stream_wrappers()
* @see \Drupal\Core\StreamWrapper\StreamWrapperInterface
*/
class StreamWrapperManager extends ContainerAware implements StreamWrapperManagerInterface {
/**
* Contains stream wrapper info.
*
* An associative array where keys are scheme names and values are themselves
* associative arrays with the keys class, type and (optionally) service_id,
* and string values.
*
* @var array
*/
protected $info = array();
/**
* Contains collected stream wrappers.
*
* Keyed by filter, each value is itself an associative array keyed by scheme.
* Each of those values is an array representing a stream wrapper, with the
* following keys and values:
* - class: stream wrapper class name
* - type: a bitmask corresponding to the type constants in
* StreamWrapperInterface
* - service_id: name of service
*
* The array on key StreamWrapperInterface::ALL contains representations of
* all schemes and corresponding wrappers.
*
* @var array
*/
protected $wrappers = array();
/**
* {@inheritdoc}
*/
public function getWrappers($filter = StreamWrapperInterface::ALL) {
if (isset($this->wrappers[$filter])) {
return $this->wrappers[$filter];
}
else if (isset($this->wrappers[StreamWrapperInterface::ALL])) {
$this->wrappers[$filter] = array();
foreach ($this->wrappers[StreamWrapperInterface::ALL] as $scheme => $info) {
// Bit-wise filter.
if (($info['type'] & $filter) == $filter) {
$this->wrappers[$filter][$scheme] = $info;
}
}
return $this->wrappers[$filter];
}
else {
return array();
}
}
/**
* {@inheritdoc}
*/
public function getNames($filter = StreamWrapperInterface::ALL) {
$names = array();
foreach (array_keys($this->getWrappers($filter)) as $scheme) {
$names[$scheme] = $this->getViaScheme($scheme)->getName();
}
return $names;
}
/**
* {@inheritdoc}
*/
public function getDescriptions($filter = StreamWrapperInterface::ALL) {
$descriptions = array();
foreach (array_keys($this->getWrappers($filter)) as $scheme) {
$descriptions[$scheme] = $this->getViaScheme($scheme)->getDescription();
}
return $descriptions;
}
/**
* {@inheritdoc}
*/
public function getViaScheme($scheme) {
return $this->getWrapper($scheme, $scheme . '://');
}
/**
* {@inheritdoc}
*/
public function getViaUri($uri) {
$scheme = file_uri_scheme($uri);
return $this->getWrapper($scheme, $uri);
}
/**
* {@inheritdoc}
*/
public function getClass($scheme) {
if (isset($this->info[$scheme])) {
return $this->info[$scheme]['class'];
}
return FALSE;
}
/**
* Returns a stream wrapper instance.
*
* @param string $scheme
* The scheme of the desired stream wrapper.
* @param string $uri
* The URI of the stream.
*
* @return \Drupal\Core\StreamWrapper\StreamWrapperInterface|bool
* A stream wrapper object, or false if the scheme is not available.
*/
protected function getWrapper($scheme, $uri) {
if (isset($this->info[$scheme]['service_id'])) {
$instance = $this->container->get($this->info[$scheme]['service_id']);
$instance->setUri($uri);
return $instance;
}
return FALSE;
}
/**
* Adds a stream wrapper.
*
* Internal use only.
*
* @param string $service_id
* The service id.
* @param string $class
* The stream wrapper class.
* @param string $scheme
* The scheme for which the wrapper should be registered.
*/
public function addStreamWrapper($service_id, $class, $scheme) {
$this->info[$scheme] = array(
'class' => $class,
'type' => $class::getType(),
'service_id' => $service_id,
);
}
/**
* Registers the tagged stream wrappers.
*
* Internal use only.
*/
public function register() {
foreach ($this->info as $scheme => $info) {
$this->registerWrapper($scheme, $info['class'], $info['type']);
}
}
/**
* Unregisters the tagged stream wrappers.
*
* Internal use only.
*/
public function unregister() {
// Normally, there are definitely wrappers set for the ALL filter. However,
// in some cases involving many container rebuilds (e.g. WebTestBase),
// $this->wrappers may be empty although wrappers are still registered
// globally. Thus an isset() check is needed before iterating.
if (isset($this->wrappers[StreamWrapperInterface::ALL])) {
foreach (array_keys($this->wrappers[StreamWrapperInterface::ALL]) as $scheme) {
stream_wrapper_unregister($scheme);
}
}
}
/**
* {@inheritdoc}
*/
public function registerWrapper($scheme, $class, $type) {
if (in_array($scheme, stream_get_wrappers(), TRUE)) {
stream_wrapper_unregister($scheme);
}
if (($type & StreamWrapperInterface::LOCAL) == StreamWrapperInterface::LOCAL) {
stream_wrapper_register($scheme, $class);
}
else {
stream_wrapper_register($scheme, $class, STREAM_IS_URL);
}
// Pre-populate the static cache with the filters most typically used.
$info = array('type' => $type, 'class' => $class);
$this->wrappers[StreamWrapperInterface::ALL][$scheme] = $info;
if (($type & StreamWrapperInterface::WRITE_VISIBLE) == StreamWrapperInterface::WRITE_VISIBLE) {
$this->wrappers[StreamWrapperInterface::WRITE_VISIBLE][$scheme] = $info;
}
}
}

View file

@ -0,0 +1,159 @@
<?php
/**
* @file
* Contains \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface.
*/
namespace Drupal\Core\StreamWrapper;
/**
* Provides a StreamWrapper manager.
*
* @see file_get_stream_wrappers()
* @see \Drupal\Core\StreamWrapper\StreamWrapperInterface
*/
interface StreamWrapperManagerInterface {
/**
* Provides Drupal stream wrapper registry.
*
* A stream wrapper is an abstraction of a file system that allows Drupal to
* use the same set of methods to access both local files and remote
* resources.
*
* Provide a facility for managing and querying user-defined stream wrappers
* in PHP. PHP's internal stream_get_wrappers() doesn't return the class
* registered to handle a stream, which we need to be able to find the
* handler
* for class instantiation.
*
* If a module registers a scheme that is already registered with PHP, the
* existing scheme will be unregistered and replaced with the specified
* class.
*
* A stream is referenced as "scheme://target".
*
* The optional $filter parameter can be used to retrieve only the stream
* wrappers that are appropriate for particular usage. For example, this
* returns only stream wrappers that use local file storage:
*
* @code
* $local_stream_wrappers =
* file_get_stream_wrappers(StreamWrapperInterface::LOCAL);
* @endcode
*
* The $filter parameter can only filter to types containing a particular
* flag. In some cases, you may want to filter to types that do not contain a
* particular flag. For example, you may want to retrieve all stream wrappers
* that are not writable, or all stream wrappers that are not local. PHP's
* array_diff_key() function can be used to help with this. For example, this
* returns only stream wrappers that do not use local file storage:
* @code
* $remote_stream_wrappers =
* array_diff_key(file_get_stream_wrappers(StreamWrapperInterface::ALL),
* file_get_stream_wrappers(StreamWrapperInterface::LOCAL));
* @endcode
*
* @param int $filter
* (Optional) Filters out all types except those with an on bit for each on
* bit in $filter. For example, if $filter is
* StreamWrapperInterface::WRITE_VISIBLE, which is equal to
* (StreamWrapperInterface::READ | StreamWrapperInterface::WRITE |
* StreamWrapperInterface::VISIBLE), then only stream wrappers with all
* three of these bits set are returned. Defaults to
* StreamWrapperInterface::ALL, which returns all registered stream
* wrappers.
*
* @return array
* An array keyed by scheme, with values containing an array of information
* about the stream wrapper, as returned by hook_stream_wrappers(). If
* $filter is omitted or set to StreamWrapperInterface::ALL, the entire
* Drupal stream wrapper registry is returned. Otherwise only the stream
* wrappers whose 'type' bitmask has an on bit for each bit specified in
* $filter are returned.
*/
public function getWrappers($filter = StreamWrapperInterface::ALL);
/**
* Returns registered stream wrapper names.
*
* @param int $filter
* (Optional) Filters out all types except those with an on bit for each on
* bit in $filter. For example, if $filter is
* StreamWrapperInterface::WRITE_VISIBLE, which is equal to
* (StreamWrapperInterface::READ | StreamWrapperInterface::WRITE |
* StreamWrapperInterface::VISIBLE), then only stream wrappers with all
* three of these bits set are returned. Defaults to
* StreamWrapperInterface::ALL, which returns all registered stream
* wrappers.
*
* @return array
* Stream wrapper names, keyed by scheme.
*/
public function getNames($filter = StreamWrapperInterface::ALL);
/**
* Returns registered stream wrapper descriptions.
*
* @param int $filter
* (Optional) Filters out all types except those with an on bit for each on
* bit in $filter. For example, if $filter is
* StreamWrapperInterface::WRITE_VISIBLE, which is equal to
* (StreamWrapperInterface::READ | StreamWrapperInterface::WRITE |
* StreamWrapperInterface::VISIBLE), then only stream wrappers with all
* three of these bits set are returned. Defaults to
* StreamWrapperInterface::ALL, which returns all registered stream
* wrappers.
*
* @return array
* Stream wrapper descriptions, keyed by scheme.
*/
public function getDescriptions($filter = StreamWrapperInterface::ALL);
/**
* Returns a stream wrapper via scheme.
*
* @param string $scheme
* The scheme of the stream wrapper.
*
* @return \Drupal\Core\StreamWrapper\StreamWrapperInterface|bool
* A stream wrapper object, or false if the scheme is not available.
*/
public function getViaScheme($scheme);
/**
* Returns a stream wrapper via URI.
*
* @param string $uri
* The URI of the stream wrapper.
*
* @return \Drupal\Core\StreamWrapper\StreamWrapperInterface|bool
* A stream wrapper object, or false if the scheme is not available.
*/
public function getViaUri($uri);
/**
* Returns the stream wrapper class.
*
* @param string $scheme
* The stream wrapper scheme.
*
* @return string|bool
* The stream wrapper class, or false if the scheme does not exist.
*/
public function getClass($scheme);
/**
* Registers stream wrapper with PHP.
*
* @param string $scheme
* The scheme of the stream wrapper.
* @param string $class
* The class of the stream wrapper.
* @param int $type
* The type of the stream wrapper.
*/
public function registerWrapper($scheme, $class, $type);
}

View file

@ -0,0 +1,55 @@
<?php
/**
* @file
* Contains \Drupal\Core\StreamWrapper\TemporaryStream.
*/
namespace Drupal\Core\StreamWrapper;
use \Drupal\Core\Url;
/**
* Defines a Drupal temporary (temporary://) stream wrapper class.
*
* Provides support for storing temporarily accessible files with the Drupal
* file interface.
*/
class TemporaryStream extends LocalStream {
/**
* {@inheritdoc}
*/
public static function getType() {
return StreamWrapperInterface::LOCAL_HIDDEN;
}
/**
* {@inheritdoc}
*/
public function getName() {
return t('Temporary files');
}
/**
* {@inheritdoc}
*/
public function getDescription() {
return t('Temporary local files for upload and previews.');
}
/**
* Implements Drupal\Core\StreamWrapper\LocalStream::getDirectoryPath()
*/
public function getDirectoryPath() {
return file_directory_temp();
}
/**
* Implements Drupal\Core\StreamWrapper\StreamWrapperInterface::getExternalUrl().
*/
public function getExternalUrl() {
$path = str_replace('\\', '/', $this->getTarget());
return Url::fromRoute('system.temporary', [], ['absolute' => TRUE, 'query' => ['file' => $path]])->toString();
}
}