类文档

Port

💡 云策文档标注

概述

本文档介绍了 WordPress 中 Port 类的功能,用于根据请求类型获取对应的端口号。该类提供了静态方法 get() 来检索端口,并定义了常见请求类型的端口常量。

关键要点

  • Port 类是一个 final 类,包含多个端口常量,如 ACAP、DICT、HTTP 和 HTTPS。
  • 静态方法 get($type) 接受字符串类型的请求类型参数,返回对应的端口号。
  • 方法会验证输入类型,若为非字符串或不受支持的端口类型,会抛出异常。

代码示例

final class Port {
    const ACAP = 674;
    const DICT = 2628;
    const HTTP = 80;
    const HTTPS = 443;

    public static function get($type) {
        if (!is_string($type)) {
            throw InvalidArgument::create(1, '$type', 'string', gettype($type));
        }

        $type = strtoupper($type);
        if (!defined("self::{$type}")) {
            $message = sprintf('Invalid port type (%s) passed', $type);
            throw new Exception($message, 'portnotsupported');
        }

        return constant("self::{$type}");
    }
}

注意事项

  • 输入参数 $type 必须是字符串,否则会抛出 InvalidArgument 异常。
  • 仅支持 'acap'、'dict'、'http' 和 'https' 类型的请求,其他类型会抛出 Exception 异常。
  • 该类在版本 2.0.0 中引入。

📄 原文内容

Find the correct port depending on the Request type.

Methods

Name Description
Port::get Retrieve the port number to use.

Source

final class Port {

	/**
	 * Port to use with Acap requests.
	 *
	 * @var int
	 */
	const ACAP = 674;

	/**
	 * Port to use with Dictionary requests.
	 *
	 * @var int
	 */
	const DICT = 2628;

	/**
	 * Port to use with HTTP requests.
	 *
	 * @var int
	 */
	const HTTP = 80;

	/**
	 * Port to use with HTTP over SSL requests.
	 *
	 * @var int
	 */
	const HTTPS = 443;

	/**
	 * Retrieve the port number to use.
	 *
	 * @param string $type Request type.
	 *                     The following requests types are supported:
	 *                     'acap', 'dict', 'http' and 'https'.
	 *
	 * @return int
	 *
	 * @throws WpOrgRequestsExceptionInvalidArgument When a non-string input has been passed.
	 * @throws WpOrgRequestsException                 When a non-supported port is requested ('portnotsupported').
	 */
	public static function get($type) {
		if (!is_string($type)) {
			throw InvalidArgument::create(1, '$type', 'string', gettype($type));
		}

		$type = strtoupper($type);
		if (!defined("self::{$type}")) {
			$message = sprintf('Invalid port type (%s) passed', $type);
			throw new Exception($message, 'portnotsupported');
		}

		return constant("self::{$type}");
	}
}

Changelog

Version Description
2.0.0 Introduced.