wishthis/vendor/knplabs/github-api/lib/Github/HttpClient/Plugin/PathPrepend.php

47 lines
1,020 B
PHP
Raw Normal View History

2022-01-21 08:23:52 +00:00
<?php
namespace Github\HttpClient\Plugin;
use Http\Client\Common\Plugin;
use Http\Promise\Promise;
use Psr\Http\Message\RequestInterface;
/**
* Prepend the URI with a string.
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
final class PathPrepend implements Plugin
{
/**
* @var string
*/
private $path;
/**
* @param string $path
*/
public function __construct(string $path)
{
$this->path = $path;
}
/**
* @param RequestInterface $request
* @param callable $next
* @param callable $first
*
* @return Promise
*/
public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise
{
$currentPath = $request->getUri()->getPath();
if (strpos($currentPath, $this->path) !== 0) {
$uri = $request->getUri()->withPath($this->path.$currentPath);
$request = $request->withUri($uri);
}
return $next($request);
}
}