3.4 Psr\Http\Message\StreamInterface
<?php
namespace Psr\Http\Message;
/**
* Describes a data stream.
*
* Typically, an instance will wrap a PHP stream; this interface provides
* a wrapper around the most common operations, including serialization of
* the entire stream to a string.
*/
interface StreamInterface
{
/**
* Reads all data from the stream into a string, from the beginning to end.
*
* This method MUST attempt to seek to the beginning of the stream before
* reading data and read the stream until the end is reached.
*
* Warning: This could attempt to load a large amount of data into memory.
*
* This method MUST NOT raise an exception in order to conform with PHP's
* string casting operations.
*
* @see http://php.net/manual/en/language.oop5.magic.php#object.tostring
* @return string
*/
public function __toString();
/**
* Closes the stream and any underlying resources.
*
* @return void
*/
public function close();
/**
* Separates any underlying resources from the stream.
*
* After the stream has been detached, the stream is in an unusable state.
*
* @return resource|null Underlying PHP stream, if any
*/
public function detach();
/**
* Get the size of the stream if known.
*
* @return int|null Returns the size in bytes if known, or null if unknown.
*/
public function getSize();
/**
* Returns the current position of the file read/write pointer
*
* @return int Position of the file pointer
* @throws \RuntimeException on error.
*/
public function tell();
/**
* Returns true if the stream is at the end of the stream.
*
* @return bool
*/
public function eof();
/**
* Returns whether or not the stream is seekable.
*
* @return bool
*/
public function isSeekable();
/**
* Seek to a position in the stream.
*
* @see http://www.php.net/manual/en/function.fseek.php
* @param int $offset Stream offset
* @param int $whence Specifies how the cursor position will be calculated
* based on the seek offset. Valid values are identical to the built-in
* PHP $whence values for `fseek()`. SEEK_SET: Set position equal to
* offset bytes SEEK_CUR: Set position to current location plus offset
* SEEK_END: Set position to end-of-stream plus offset.
* @throws \RuntimeException on failure.
*/
public function seek($offset, $whence = SEEK_SET);
/**
* Seek to the beginning of the stream.
*
* If the stream is not seekable, this method will raise an exception;
* otherwise, it will perform a seek(0).
*
* @see seek()
* @see http://www.php.net/manual/en/function.fseek.php
* @throws \RuntimeException on failure.
*/
public function rewind();
/**
* Returns whether or not the stream is writable.
*
* @return bool
*/
public function isWritable();
/**
* Write data to the stream.
*
* @param string $string The string that is to be written.
* @return int Returns the number of bytes written to the stream.
* @throws \RuntimeException on failure.
*/
public function write($string);
/**
* Returns whether or not the stream is readable.
*
* @return bool
*/
public function isReadable();
/**
* Read data from the stream.
*
* @param int $length Read up to $length bytes from the object and return
* them. Fewer than $length bytes may be returned if underlying stream
* call returns fewer bytes.
* @return string Returns the data read from the stream, or an empty string
* if no bytes are available.
* @throws \RuntimeException if an error occurs.
*/
public function read($length);
/**
* Returns the remaining contents in a string
*
* @return string
* @throws \RuntimeException if unable to read.
* @throws \RuntimeException if error occurs while reading.
*/
public function getContents();
/**
* Get stream metadata as an associative array or retrieve a specific key.
*
* The keys returned are identical to the keys returned from PHP's
* stream_get_meta_data() function.
*
* @see http://php.net/manual/en/function.stream-get-meta-data.php
* @param string $key Specific metadata to retrieve.
* @return array|mixed|null Returns an associative array if no key is
* provided. Returns a specific key value if a key is provided and the
* value is found, or null if the key is not found.
*/
public function getMetadata($key = null);
}
3.5 Psr\Http\Message\UriInterface
<?php
namespace Psr\Http\Message;
/**
* URI 数据对象。
*
* 此接口按照 RFC 3986 来构建 HTTP URI,提供了一些通用的操作,你可以自由的对此接口
* 进行扩展。你可以使用此 URI 接口来做 HTTP 相关的操作,也可以使用此接口做任何 URI
* 相关的操作。
*
* 此接口的实例化对象被视为无法修改的,所有能修改状态的方法,都 **必须** 有一套机制,在内部保
* 持好原有的内容,然后把修改状态后的,新的实例返回。
*
* @see http://tools.ietf.org/html/rfc3986 (URI 通用标准规范)
*/
interface UriInterface
{
/**
* 从 URI 中取出 scheme。
*
* 如果不存在 Scheme,此方法 **必须** 返回空字符串。
*
* 返回的数据 **必须** 是小写字母,遵照 RFC 3986 规范 3.1 章节。
*
* 最后部分的 ":" 字串不属于 Scheme,**一定不可** 作为返回数据的一部分。
*
* @see https://tools.ietf.org/html/rfc3986#section-3.1
* @return string URI scheme 的值
*/
public function getScheme();
/**
* 返回 URI 授权信息。
*
* 如果没有 URI 信息的话,**必须** 返回一个空数组。
*
* URI 的授权信息语法是:
*
* <pre>
* [user-info@]host[:port]
* </pre>
*
* 如果端口部分没有设置,或者端口不是标准端口,**一定不可** 包含在返回值内。
*
* @see https://tools.ietf.org/html/rfc3986#section-3.2
* @return string URI 授权信息,格式为:"[user-info@]host[:port]"
*/
public function getAuthority();
/**
* 从 URI 中获取用户信息。
*
* 如果不存在用户信息,此方法 **必须** 返回一个空字符串。
*
* 用户信息后面跟着的 "@" 字符,不是用户信息里面的一部分,**一定不可** 在返回值里
* 出现。
*
* @return string URI 的用户信息,格式:"username[:password]"
*/
public function getUserInfo();
/**
* 从 URI 信息中获取 HOST 值。
*
* 如果 URI 中没有此值,**必须** 返回空字符串。
*
* 返回的数据 **必须** 是小写字母,遵照 RFC 3986 规范 3.2.2 章节。
*
* @see http://tools.ietf.org/html/rfc3986#section-3.2.2
* @return string URI 信息中的 HOST 值。
*/
public function getHost();
/**
* 从 URI 信息中获取端口信息。
*
* 如果端口信息是与当前 Scheme 的标准端口不匹配的话,就使用整数值的格式返回,如果是一
* 样的话,**必须** 返回 `null` 值。
*
* 如果存在端口信息,都是不存在 scheme 信息的话,**必须** 返回 `null` 值。
*
* 如果不存在端口数据,但是 scheme 数据存在的话,**可以** 返回 scheme 对应的
* 标准端口,但是 **应该** 返回 `null`。
*
* @return null|int 从 URI 信息中的端口信息。
*/
public function getPort();
/**
* 从 URI 信息中获取路径。
*
* The path can either be empty or absolute (starting with a slash) or
* rootless (not starting with a slash). Implementations MUST support all
* three syntaxes.
*
* Normally, the empty path "" and absolute path "/" are considered equal as
* defined in RFC 7230 Section 2.7.3. But this method MUST NOT automatically
* do this normalization because in contexts with a trimmed base path, e.g.
* the front controller, this difference becomes significant. It's the task
* of the user to handle both "" and "/".
*
* The value returned MUST be percent-encoded, but MUST NOT double-encode
* any characters. To determine what characters to encode, please refer to
* RFC 3986, Sections 2 and 3.3.
*
* As an example, if the value should include a slash ("/") not intended as
* delimiter between path segments, that value MUST be passed in encoded
* form (e.g., "%2F") to the instance.
*
* @see https://tools.ietf.org/html/rfc3986#section-2
* @see https://tools.ietf.org/html/rfc3986#section-3.3
* @return string The URI path.
*/
public function getPath();
/**
* Retrieve the query string of the URI.
*
* If no query string is present, this method MUST return an empty string.
*
* The leading "?" character is not part of the query and MUST NOT be
* added.
*
* The value returned MUST be percent-encoded, but MUST NOT double-encode
* any characters. To determine what characters to encode, please refer to
* RFC 3986, Sections 2 and 3.4.
*
* As an example, if a value in a key/value pair of the query string should
* include an ampersand ("&") not intended as a delimiter between values,
* that value MUST be passed in encoded form (e.g., "%26") to the instance.
*
* @see https://tools.ietf.org/html/rfc3986#section-2
* @see https://tools.ietf.org/html/rfc3986#section-3.4
* @return string The URI query string.
*/
public function getQuery();
/**
* Retrieve the fragment component of the URI.
*
* If no fragment is present, this method MUST return an empty string.
*
* The leading "#" character is not part of the fragment and MUST NOT be
* added.
*
* The value returned MUST be percent-encoded, but MUST NOT double-encode
* any characters. To determine what characters to encode, please refer to
* RFC 3986, Sections 2 and 3.5.
*
* @see https://tools.ietf.org/html/rfc3986#section-2
* @see https://tools.ietf.org/html/rfc3986#section-3.5
* @return string The URI fragment.
*/
public function getFragment();
/**
* Return an instance with the specified scheme.
*
* This method MUST retain the state of the current instance, and return
* an instance that contains the specified scheme.
*
* Implementations MUST support the schemes "http" and "https" case
* insensitively, and MAY accommodate other schemes if required.
*
* An empty scheme is equivalent to removing the scheme.
*
* @param string $scheme The scheme to use with the new instance.
* @return self A new instance with the specified scheme.
* @throws \InvalidArgumentException for invalid schemes.
* @throws \InvalidArgumentException for unsupported schemes.
*/
public function withScheme($scheme);
/**
* Return an instance with the specified user information.
*
* This method MUST retain the state of the current instance, and return
* an instance that contains the specified user information.
*
* Password is optional, but the user information MUST include the
* user; an empty string for the user is equivalent to removing user
* information.
*
* @param string $user The user name to use for authority.
* @param null|string $password The password associated with $user.
* @return self A new instance with the specified user information.
*/
public function withUserInfo($user, $password = null);
/**
* Return an instance with the specified host.
*
* This method MUST retain the state of the current instance, and return
* an instance that contains the specified host.
*
* An empty host value is equivalent to removing the host.
*
* @param string $host The hostname to use with the new instance.
* @return self A new instance with the specified host.
* @throws \InvalidArgumentException for invalid hostnames.
*/
public function withHost($host);
/**
* Return an instance with the specified port.
*
* This method MUST retain the state of the current instance, and return
* an instance that contains the specified port.
*
* Implementations MUST raise an exception for ports outside the
* established TCP and UDP port ranges.
*
* A null value provided for the port is equivalent to removing the port
* information.
*
* @param null|int $port The port to use with the new instance; a null value
* removes the port information.
* @return self A new instance with the specified port.
* @throws \InvalidArgumentException for invalid ports.
*/
public function withPort($port);
/**
* Return an instance with the specified path.
*
* This method MUST retain the state of the current instance, and return
* an instance that contains the specified path.
*
* The path can either be empty or absolute (starting with a slash) or
* rootless (not starting with a slash). Implementations MUST support all
* three syntaxes.
*
* If an HTTP path is intended to be host-relative rather than path-relative
* then it must begin with a slash ("/"). HTTP paths not starting with a slash
* are assumed to be relative to some base path known to the application or
* consumer.
*
* Users can provide both encoded and decoded path characters.
* Implementations ensure the correct encoding as outlined in getPath().
*
* @param string $path The path to use with the new instance.
* @return self A new instance with the specified path.
* @throws \InvalidArgumentException for invalid paths.
*/
public function withPath($path);
/**
* Return an instance with the specified query string.
*
* This method MUST retain the state of the current instance, and return
* an instance that contains the specified query string.
*
* Users can provide both encoded and decoded query characters.
* Implementations ensure the correct encoding as outlined in getQuery().
*
* An empty query string value is equivalent to removing the query string.
*
* @param string $query The query string to use with the new instance.
* @return self A new instance with the specified query string.
* @throws \InvalidArgumentException for invalid query strings.
*/
public function withQuery($query);
/**
* Return an instance with the specified URI fragment.
*
* This method MUST retain the state of the current instance, and return
* an instance that contains the specified URI fragment.
*
* Users can provide both encoded and decoded fragment characters.
* Implementations ensure the correct encoding as outlined in getFragment().
*
* An empty fragment value is equivalent to removing the fragment.
*
* @param string $fragment The fragment to use with the new instance.
* @return self A new instance with the specified fragment.
*/
public function withFragment($fragment);
/**
* Return the string representation as a URI reference.
*
* Depending on which components of the URI are present, the resulting
* string is either a full URI or relative reference according to RFC 3986,
* Section 4.1. The method concatenates the various components of the URI,
* using the appropriate delimiters:
*
* - If a scheme is present, it MUST be suffixed by ":".
* - If an authority is present, it MUST be prefixed by "//".
* - The path can be concatenated without delimiters. But there are two
* cases where the path has to be adjusted to make the URI reference
* valid as PHP does not allow to throw an exception in __toString():
* - If the path is rootless and an authority is present, the path MUST
* be prefixed by "/".
* - If the path is starting with more than one "/" and no authority is
* present, the starting slashes MUST be reduced to one.
* - If a query is present, it MUST be prefixed by "?".
* - If a fragment is present, it MUST be prefixed by "#".
*
* @see http://tools.ietf.org/html/rfc3986#section-4.1
* @return string
*/
public function __toString();
}
3.6 Psr\Http\Message\UploadedFileInterface
<?php
namespace Psr\Http\Message;
/**
* 通过 HTTP 请求上传的一个文件内容。
*
* 此接口的实例是被视为无法修改的,所有能修改状态的方法,都 **必须** 有一套机制,在内部保
* 持好原有的内容,然后把修改状态后的,新的实例返回。
*/
interface UploadedFileInterface
{
/**
* 获取上传文件的数据流。
*
* 此方法必须返回一个 `StreamInterface` 实例,此方法的目的在于允许 PHP 对获取到的数
* 据流直接操作,如 stream_copy_to_stream() 。
*
* 如果在调用此方法之前调用了 `moveTo()` 方法,此方法 **必须** 抛出异常。
*
* @return StreamInterface 上传文件的数据流
* @throws \RuntimeException 没有数据流的情形下。
* @throws \RuntimeException 无法创建数据流。
*/
public function getStream();
/**
* 把上传的文件移动到新目录。
*
* 此方法保证能同时在 `SAPI` 和 `non-SAPI` 环境下使用。实现类库 **必须** 判断
* 当前处在什么环境下,并且使用合适的方法来处理,如 move_uploaded_file(), rename()
* 或者数据流操作。
*
* $targetPath 可以是相对路径,也可以是绝对路径,使用 rename() 解析起来应该是一样的。
*
* 当这一次完成后,原来的文件 **必须** 会被移除。
*
* 如果此方法被调用多次,一次以后的其他调用,都要抛出异常。
*
* 如果在 SAPI 环境下的话,$_FILES 内有值,当使用 moveTo(), is_uploaded_file()
* 和 move_uploaded_file() 方法来移动文件时 **应该** 确保权限和上传状态的准确性。
*
* 如果你希望操作数据流的话,请使用 `getStream()` 方法,因为在 SAPI 场景下,无法
* 保证书写入数据流目标。
*
* @see http://php.net/is_uploaded_file
* @see http://php.net/move_uploaded_file
* @param string $targetPath 目标文件路径。
* @throws \InvalidArgumentException 参数有问题时抛出异常。
* @throws \RuntimeException 发生任何错误,都抛出此异常。
* @throws \RuntimeException 多次运行,也抛出此异常。
*/
public function moveTo($targetPath);
/**
* 获取文件大小。
*
* 实现类库 **应该** 优先使用 $_FILES 里的 `size` 数值。
*
* @return int|null 以 bytes 为单位,或者 null 未知的情况下。
*/
public function getSize();
/**
* 获取上传文件时出现的错误。
*
* 返回值 **必须** 是 PHP 的 UPLOAD_ERR_XXX 常量。
*
* 如果文件上传成功,此方法 **必须** 返回 UPLOAD_ERR_OK。
*
* 实现类库 **必须** 返回 $_FILES 数组中的 `error` 值。
*
* @see http://php.net/manual/en/features.file-upload.errors.php
* @return int PHP 的 UPLOAD_ERR_XXX 常量。
*/
public function getError();
/**
* 获取客户端上传的文件的名称。
*
* 永远不要信任此方法返回的数据,客户端有可能发送了一个恶意的文件名来攻击你的程序。
*
* 实现类库 **应该** 返回存储在 $_FILES 数组中 `name` 的值。
*
* @return string|null 用户上传的名字,或者 null 如果没有此值。
*/
public function getClientFilename();
/**
* 客户端提交的文件类型。
*
* 永远不要信任此方法返回的数据,客户端有可能发送了一个恶意的文件类型名称来攻击你的程序。
*
* 实现类库 **应该** 返回存储在 $_FILES 数组中 `type` 的值。
*
* @return string|null 用户上传的类型,或者 null 如果没有此值。
*/
public function getClientMediaType();
}