PHP Classes

File: src/ClientProxy.php

Recommend this page to a friend!
  Classes of Till Wehowski   PHP Proxy Server Script   src/ClientProxy.php   Download  
File: src/ClientProxy.php
Role: Class source
Content type: text/plain
Description: Class source
Class: PHP Proxy Server Script
Forward HTTP requests to other servers as a proxy
Author: By
Last change:
Date: 3 years ago
Size: 2,052 bytes
 

Contents

Class file image Download
<?php
namespace frdl\Proxy;

use
Proxy\Proxy as BaseProxy;
use
Proxy\Adapter\AdapterInterface;
use
Proxy\Exception\UnexpectedValueException;
use
Psr\Http\Message\RequestInterface;
use
Psr\Http\Message\ResponseInterface;
use
Relay\RelayBuilder;
use
Zend\Diactoros\Response;
use
Zend\Diactoros\Uri;

class
ClientProxy extends BaseProxy
{
   
    protected
$toUri = null;
    public function
__construct(AdapterInterface $adapter, $toUri = null)
    {
    
// call_user_func_array(parent::__construct, func_get_args());
     
parent::__construct($adapter, $toUri);
      
$this->toUri = $toUri;
    }
           
    public function
toUri(){
        if(
null === $this->toUri){
            return
$this->to( new Uri($_SERVER['REQUEST_URI']) . '');
        }
        return
$this->to($this->toUri . '');
    }
   
    public function
to($target)
    {
        if (
$this->request === null) {
            throw new
UnexpectedValueException('Missing request instance.');
        }
       
$target = new Uri($target);
       
// Overwrite target scheme and host.
       
$uri = $this->request->getUri()
            ->
withScheme($target->getScheme())
            ->
withHost($target->getHost());
       
// Check for custom port.
       
if ($port = $target->getPort()) {
           
$uri = $uri->withPort($port);
        }
       
// Check for subdirectory.
      // if ($path = $target->getPath()) {
      // $uri = $uri->withPath(rtrim($path, '/') . '/' . ltrim($uri->getPath(), '/'));
      // }
        
if ($path = $target->getPath()) {
          
$uri = $uri->withPath(
                   
//$uri->withPath(rtrim($path, '/') .
                  
'/' . trim($path, '/')
               );
         }
       
       
$request = $this->request->withUri($uri);
       
$stack = $this->filters;
       
$stack[] = function (RequestInterface $request, ResponseInterface $response, callable $next) {
           
$response = $this->adapter->send($request);
   
            return
$next($request, $response);
        };
       
$relay = (new RelayBuilder)->newInstance($stack);
        return
$relay($request, new Response);
    }
   
   
}