创建协议

@param  string $protocol

@return \One\Protocol\Contracts\Protocol
@throws \One\Protocol\Exceptions\ProtocolException
16107
public static function newProtocol(string $protocol): Protocol
    {
        if (! isset(static::$protocols[$protocol])) {
            throw ProtocolException::notSupport($protocol);
        }

        return Reflection::newInstance(
            '\\One\\Protocol\\Protocols\\' . static::$protocols[$protocol]
        );
    }
644561
public function request($protocolVersion, $headers, $body, $method, $uri)
    {
        return new Messages\Message\Request\Implementation(
            $protocolVersion,
            $headers,
            $body,
            $method,
            $uri
        );
    }
1257716
public function setProtocol($protocol) {

        $proto = strtoupper($protocol);

        if ( !in_array($proto, static::$supported_protocols) ){
            throw new Exception("Invalid RPC protocol");
        }

        $this->protocol = $proto;

        return $this;

    }
1355288
public function withProtocolVersion($version)
    {
        Validator::checkProtocolVersion($version);
        $instance = clone $this;
        $instance->protocol = $version;

        return $instance;
    }
1095223
public function make($contract)
    {
        if (empty($this->bindings[$contract])) {
            throw new \LogicException('Not found contract:' . $contract);
        }

        $class = $this->bindings[$contract];
        if ($class['type'] == 'closure') {
            return call_user_func($class['closure'], $class['class'], $class['args']);
        } else if ($class['type'] == 'singleton') {
            return $class['instance'];
        }

        throw new \LogicException('Not found contract:' . $contract);
    }
1012840
static function register($protocol = 'sftp')
    {
        if (in_array($protocol, stream_get_wrappers(), true)) {
            return false;
        }
        $class = function_exists('get_called_class') ? get_called_class() : __CLASS__;
        return stream_wrapper_register($protocol, $class);
    }
93162
public function execute($request)
    {
        RequestNotSupportedException::assertSupports($this, $request);

        $details = ArrayObject::ensureArrayObject($request->getModel());

        if (isset($details['ErrorMessage']) === true) {
            $request->markFailed();

            return;
        }

        if (isset($details['ResCode']) === true) {
            if ($details['ResCode'] === '0') {
                $request->markFailed();

                return;
            }

            $request->markCaptured();

            return;
        }

        if (isset($details['RtnCode']) === true) {
            if ($details['RtnCode'] === '0') {
                $request->markFailed();

                return;
            }

            if ($details['RtnCode'] === '1') {
                if (isset($details['RtnMerchantTradeNo']) === true) {
                    $request->markRefunded();

                    return;
                }
                // 取消訂單(統一超商C2C).
                if (isset($details['CVSPaymentNo']) === true && isset($details['MerchantTradeDate']) === false) {
                    $request->markCanceled();

                    return;
                }

                // 宅配逆物流訂單產生
                if (isset($details['AllPayLogisticsID']) === true && isset($details['MerchantTradeDate']) === false) {
                    $request->markRefunded();

                    return;
                }

                $request->markCaptured();

                return;
            }

            $request->markUnknown();

            return;
        }

        // 超商取貨逆物流訂單(全家超商B2C).
        if (isset($details['RtnMerchantTradeNo']) === true && isset($details['RtnOrderNo']) === true) {
            $request->markRefunded();

            return;
        }

        if (isset($details['CVSStoreID']) === true) {
            $request->markCaptured();

            return;
        }

        $request->markNew();
    }
583213
public function getEndpointAsUrl($port, $protocol = 'tcp')
    {
        if (!$this->hasEndpoint($port, $protocol)) {
            throw new Exception('Endpoint is not available');
        }

        $key = $port . '/' . $protocol;

        return Url::fromString($this->endpoints[$key]);
    }
12248
public static function getProtocol($url)
    {
        $urlInfo = static::info($url);

        return ArrayHelper::get($urlInfo, 'protocol', null, true);
    }
717237
public static function getClient($protocol = null)
    {
        $protocol = $protocol ?: self::DEFAULT_PROTOCOL;
        return self::$clients[$protocol];
    }
37823
public function make($abstract, array $parameters = [])
    {
        if ($this->offsetExists($abstract)) {
            return $this->offsetGet($abstract);
        }

        if (!class_exists($abstract)) {
            throw new RuntimeException("Can not make an undefined class [$abstract]");
        }

        $reflector = new ReflectionClass($abstract);

        // 检测是否可实例化
        if (!$reflector->isInstantiable()) {
            throw new RuntimeException("The class [$abstract] is not instantiable.");
        }

        $instance = $reflector->newInstanceArgs($parameters);

        $this->offsetSet($abstract, $instance);

        return $instance;
    }
1179249
private function protocol()
    {
        $cacheKey = 'cet_apply-' . $this->getUserName();

        if (!$this->getCache()->fetch($cacheKey)) {
            $url = Config::get('url.cet_protocol');

            $param['__VIEWSTATE'] = true;
            $param['__VIEWSTATEGENERATOR'] = "C510EDC3";
            $param['TextBox1'] = "C510EDC3";
            $param['Button1'] = iconv('utf-8', 'gb2312', '我已认真阅读，并同意以上相关规定');

            $response = $this->request('post', [$url, $param]);

            $body = $this->checkAndThrow($response);

            if (!preg_match('/bmxmb/', $body)) {
                throw new HttpException('四六级协议同意失败', 10008);
            }

            $this->getCache()->save($cacheKey, true, Config::get('cache.expire', 100));
        }
    }
1136217
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        // 获取Response对象
        $response = $handler->handle($request);
        // 获取路由结果
        $result = RequestContext::get('routeResult');
        if(null === $result)
        {
            throw new \RuntimeException('RequestContent not found routeResult');
        }
        // 路由匹配结果是否是[控制器对象, 方法名]
        $isObject = is_array($result['callable']) && isset($result['callable'][0]) && $result['callable'][0] instanceof HttpController;
        if($isObject)
        {
            // 复制一份控制器对象
            $result['callable'][0] = clone $result['callable'][0];
            // 传入Request和Response对象
            $result['callable'][0]->request = $request;
            $result['callable'][0]->response = $response;
        }
        // 执行动作
        $actionResult = call_user_func_array($result['callable'], $this->prepareActionParams($request, $result));
        if($isObject)
        {
            // 获得控制器中的Response
            $response = $result['callable'][0]->response;
        }
        // 视图
        if($actionResult instanceof \Imi\Server\View\Annotation\View)
        {
            // 动作返回的值是@View注解
            $viewAnnotation = $actionResult;
        }
        else if($actionResult instanceof Response)
        {
            $response = $actionResult;
        }
        else
        {
            // 获取对应动作的视图注解
            $viewAnnotation = clone ViewParser::getInstance()->getByCallable($result['callable']);
            if(null !== $viewAnnotation)
            {
                if(is_array($actionResult))
                {
                    // 动作返回值是数组，合并到视图注解
                    $viewAnnotation->data = array_merge($viewAnnotation->data, $actionResult);
                }
                else
                {
                    // 非数组直接赋值
                    $viewAnnotation->data = $actionResult;
                }
            }
        }

        if(isset($viewAnnotation))
        {
            // 视图渲染
            $view = RequestContext::getServerBean('View');
            $response = $view->render($viewAnnotation, $response);
        }

        return $response;
    }
1116569
public function send($method) {

        if(!filter_var($this->url, FILTER_VALIDATE_URL)) {

            throw new Exception('Not a valid request address');

            exit;

        }

        $method = strtoupper($method);

        if($method !== 'GET') {

            if ($method === 'POST')

                curl_setopt(self::$handle, CURLOPT_POST, true);

            else

                curl_setopt(self::$handle, CURLOPT_CUSTOMREQUEST, $method);

            //判断传输类型 如果是x-www-form-urlencoded 参数http_build_query
            foreach($this->header as $val) {

                if(stripos($val, 'x-www-form-urlencoded')) {

                    $this->params = http_build_query($this->params);

                    break;
                }
            }

            curl_setopt(self::$handle, CURLOPT_POSTFIELDS, $this->params);

        } else if(is_array($this->params)) {

            $this->url .= (strpos($this->url, '?') !== false ? '&' : '?') . urlencode(http_build_query($this->params));

        }

        curl_setopt_array(self::$handle, $this->get_curl_base_options());

        $response    = curl_exec(self::$handle);

        $info        = curl_getinfo(self::$handle);

        if($error = curl_error(self::$handle)) {

            throw new Exception($error);

            exit;

        }

        curl_close(self::$handle);

        return (new Response($info['http_code'], substr($response, $info['header_size']), substr($response, 0, $info['header_size'])))->result();

    }
781858
public static function site($uri = '', $protocol = null, $index = false)
    {
        // 关闭可能的计划，主机，端口，用户和其他匹配到的部分
        $path = preg_replace('~^[-a-z0-9+.]++://[^/]++/?~', '', trim($uri, '/'));

        if (preg_match('/[^\x00-\x7F]/S', $path))
        {
            // 编码所有非ASCII字符，RFC 1738标准
            $path = preg_replace_callback('~([^/]+)~', 'rawurlencode', $path);
        }

        return self::base($protocol, $index) . $path;
    }
546612
public function setProtocol($protocol)
    {
        $lowerProtocol = strtolower($protocol);

        // handle special cases where protocol is not
        // formatted as intended yet.

        if ("websocket" == $lowerProtocol)
            $lowerProtocol = "ws";

        if (in_array($lowerProtocol, ["https", "wss"])) {
            // provided secure type of protocol
            $this->setUseSsl(true);
            $lowerProtocol = substr($lowerProtocol, 0, -1);
        }

        $this->protocol = $lowerProtocol;
        return $this;
    }
1305049
protected function createSwooleServer(string $protocolName, array $config = []): self
    {
        switch ($protocolName) {
            // HTTP Server
            case Protocol::HTTP:
                $server = SwHttpServer::class;
                break;

            // WebSocket Server
            case Protocol::WEBSOCKET:
                $server = SwWebSocketServer::class;
                break;

            // TCP/UDP Server
            default:
                $server = SwServer::class;
                break;
        }

        // {{ 创建 Server
        $swoole = new $server(
            $config['host'],
            $config['port'],
            SWOOLE_PROCESS,
            SWOOLE_SOCK_TCP
        );

        $swoole->set($config['swoole']);
        unset($server);

        $this->swoole = $this->bindSwooleEvents($swoole, $protocolName);
        unset($swoole);
        // }}

        return $this;
    }