Commit 99a84021 authored by TheBigB's avatar TheBigB
Browse files

Reformat code

parent 16d32e3a
Pipeline #127204820 passed with stage
in 2 minutes and 20 seconds
The MIT License (MIT)
Copyright (c) 2015 Bernardo van der Wal
Copyright (c) 2020 Bernardo van der Wal
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
......
[![build status](https://gitlab.com/1of0/php-curly/badges/develop/build.svg)](https://gitlab.com/1of0/php-curly/commits/develop)
[![coverage report](https://gitlab.com/1of0/php-curly/badges/develop/coverage.svg)](https://gitlab.com/1of0/php-curly/commits/develop)
[![pipeline status](https://gitlab.com/1of0/php-curly/badges/develop/pipeline.svg)](https://gitlab.com/1of0/php-curly/-/commits/develop)
[![coverage report](https://gitlab.com/1of0/php-curly/badges/develop/coverage.svg)](https://gitlab.com/1of0/php-curly/-/commits/develop)
# Curly
......@@ -16,10 +16,10 @@ $curly = new Curly();
// Using PSR-7 RequestInterface implementation
$request = (new Request)
->withMethod(Curly::HTTP_POST)
->withUri(new Uri('http://example.com'))
->withHeader('Accepts', 'application/json');
->withMethod(Curly::HTTP_POST)
->withUri(new Uri('http://example.com'))
->withHeader('Accepts', 'application/json');
$response = $curly->request($request);
// Using plain URL and method
......
<phpunit bootstrap="../vendor/autoload.php">
<testsuites>
<testsuite name="Curly">
<directory>../tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">../src</directory>
<exclude>
<directory>../src/Exceptions</directory>
</exclude>
</whitelist>
</filter>
<testsuites>
<testsuite name="Curly">
<directory>../tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">../src</directory>
<exclude>
<directory>../src/Exceptions</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
......@@ -5,10 +5,10 @@
set -xe
apt-get update -yqq
apt-get install git libzip-dev zip curl libffi-dev python-pip python-dev gunicorn -yqq
apt-get install git libzip-dev zip curl libffi-dev python3-pip python3-dev gunicorn3 -yqq
pip install --upgrade cffi
pip install httpbin
pip3 install --upgrade cffi
pip3 install httpbin
pecl install xdebug-2.9.3
......
{
"name": "1of0/curly",
"description": "cURL wrapper",
"keywords": [ "cURL", "wrapper", "http" ],
"license": "MIT",
"require":
{
"php": ">=5.6.0",
"name": "1of0/curly",
"description": "cURL wrapper",
"keywords": [ "cURL", "wrapper", "http" ],
"license": "MIT",
"require": {
"php": "^7.2",
"ext-json": "*",
"ext-curl": "*",
"psr/http-message": "^1.0",
"1of0/streams": "^0.1.3",
"zendframework/zend-diactoros": "^1.1.0"
},
"require-dev":
{
"psr/http-message": "^1.0",
"1of0/streams": "^0.1.3",
"laminas/laminas-diactoros": "^1.0|^2.0"
},
"require-dev": {
"roave/security-advisories": "dev-master",
"phpunit/phpunit": "^5.0.0"
},
"autoload":
{
"psr-4":
{
"OneOfZero\\Curly\\": "src/"
}
},
"autoload-dev":
{
"psr-4":
{
"OneOfZero\\Curly\\Tests\\": "tests/"
}
}
"phpunit/phpunit": "^8.0"
},
"suggest": {
"ext-mbstring": "*"
},
"autoload": {
"psr-4": {
"OneOfZero\\Curly\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"OneOfZero\\Curly\\Tests\\": "tests/"
}
}
}
This diff is collapsed.
<?php
/**
* Copyright (c) 2015 Bernardo van der Wal
* MIT License
*
* Refer to the LICENSE file for the full copyright notice.
*/
namespace OneOfZero\Curly;
use Psr\Http\Message\StreamInterface;
......@@ -18,113 +11,110 @@ BinarySafe::initialize();
* Class BinarySafe
*
* Collection of helper functions that ensure proper function when dealing with unicode data.
*
* @package OneOfZero\Curly
*/
class BinarySafe
{
/**
* Default maximum for write failures
*/
const MAX_WRITE_FAILURES = 5;
/**
* @var bool $mbFunctionsAvailable
*/
private static $mbFunctionsAvailable = false;
/**
* Detects whether mb_* functions are available.
*/
public static function initialize()
{
self::$mbFunctionsAvailable = function_exists('mb_strlen');
}
/**
* Binary-safe strlen implementation; uses mb_strlen if available.
*
* @param $input
* @return int
* @throws RuntimeException
*/
public static function strlen($input)
{
if (self::$mbFunctionsAvailable)
{
$length = mb_strlen($input, '8bit');
if ($length === false)
{
throw new RuntimeException('mb_strlen() failed');
}
return $length;
}
return strlen($input);
}
/**
* Binary-safe substr implementation; uses mb_substr if available.
*
* @param string $input
* @param int $start
* @param int|null $length
* @return string
*/
public static function substr($input, $start, $length = null)
{
if ($start === 0 && $length === null)
{
return $input;
}
if (self::$mbFunctionsAvailable)
{
if (!isset($length))
{
$length = ($start >= 0) ? self::strlen($input) - $start : -$start;
}
return mb_substr($input, $start, $length, '8bit');
}
return isset($length)
/**
* Default maximum for write failures
*/
private const MAX_WRITE_FAILURES = 5;
/**
* @var bool
*/
private static $mbFunctionsAvailable = false;
/**
* Detects whether mb_* functions are available.
*/
public static function initialize(): void
{
self::$mbFunctionsAvailable = function_exists('mb_strlen');
}
/**
* Binary-safe strlen implementation; uses mb_strlen if available.
*
* @param string $input
* @return int
* @throws RuntimeException
*/
public static function strlen(string $input): int
{
if (self::$mbFunctionsAvailable) {
$length = mb_strlen($input, '8bit');
if ($length === false) {
throw new RuntimeException('mb_strlen() failed');
}
return $length;
}
return strlen($input);
}
/**
* Binary-safe substr implementation; uses mb_substr if available.
*
* @param string $input
* @param int $start
* @param int|null $length
* @return string
*/
public static function substr(string $input, int $start, ?int $length = null): string
{
if ($start === 0 && $length === null) {
return $input;
}
if (self::$mbFunctionsAvailable) {
if (!isset($length)) {
$length = ($start >= 0)
? self::strlen($input) - $start
: -$start;
}
return mb_substr($input, $start, $length, '8bit');
}
return isset($length)
? substr($input, $start, $length)
: substr($input, $start);
}
/**
* Robust stream writing method that has tolerance for write failures.
*
* @param StreamInterface $stream
* @param string $data
* @param int $maxWriteFailures
* @return int
*/
public static function write(StreamInterface $stream, $data, $maxWriteFailures = self::MAX_WRITE_FAILURES)
{
$bytesToWrite = self::strlen($data);
$bytesWrittenTotal = 0;
$failCounter = 0;
while ($bytesWrittenTotal < $bytesToWrite)
{
$bytesWritten = $stream->write(self::substr($data, $bytesWrittenTotal));
if ($bytesWritten === false)
{
return $bytesWrittenTotal;
}
$failCounter = ($bytesWritten === 0) ? $failCounter + 1 : 0;
if ($failCounter === $maxWriteFailures)
{
return $bytesWrittenTotal;
}
$bytesWrittenTotal += $bytesWritten;
}
return $bytesWrittenTotal;
}
}
/**
* Robust stream writing method that has tolerance for write failures.
*
* @param StreamInterface $stream
* @param string $data
* @param int $maxWriteFailures
* @return int
*/
public static function write(
StreamInterface $stream,
string $data,
int $maxWriteFailures = self::MAX_WRITE_FAILURES
): int {
$bytesToWrite = self::strlen($data);
$bytesWrittenTotal = 0;
$failCounter = 0;
while ($bytesWrittenTotal < $bytesToWrite) {
$bytesWritten = $stream->write(self::substr($data, $bytesWrittenTotal));
if ($bytesWritten === false) {
return $bytesWrittenTotal;
}
$failCounter = ($bytesWritten === 0)
? $failCounter + 1
: 0;
if ($failCounter === $maxWriteFailures) {
return $bytesWrittenTotal;
}
$bytesWrittenTotal += $bytesWritten;
}
return $bytesWrittenTotal;
}
}
<?php
/**
* Copyright (c) 2015 Bernardo van der Wal
* MIT License
*
* Refer to the LICENSE file for the full copyright notice.
*/
namespace OneOfZero\Curly;
/**
* Interface CancellationCallbackInterface
*
* Defines an interface for a cancellation callback.
*
* @package OneOfZero\Curly
*/
interface CancellationCallbackInterface
{
/**
* Should return true to signal cancellation.
*
* @return bool
*/
public function isCanceled();
/**
* Should return true to signal cancellation.
*
* @return bool
*/
public function isCanceled(): bool;
}
<?php
/**
* Copyright (c) 2015 Bernardo van der Wal
* MIT License
*
* Refer to the LICENSE file for the full copyright notice.
*/
namespace OneOfZero\Curly;
use OneOfZero\Curly\Exceptions\CurlException;
......@@ -15,299 +8,278 @@ use OneOfZero\Streams\SharedStreamInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use Zend\Diactoros\Request;
use Zend\Diactoros\Response;
use Zend\Diactoros\Stream;
use Laminas\Diactoros\Request;
use Laminas\Diactoros\Response;
use Laminas\Diactoros\Stream;
/**
* Class Curly
*
* Object oriented wrapper around cURL with PSR-7 support.
*
* @package OneOfZero\Curly
*/
class Curly implements HttpClientInterface
{
/**
* Holds the cURL options relevant to this instance.
*
* @var CurlyOptions $options
*/
private $options;
/**
* Holds the configured handler (if any).
*
* @var AbstractHandler $customHandler;
*/
private $customHandler;
/**
* Creates an instance of Curly, optionally pre-configuring it with a CurlyOptions instance.
*
* @param CurlyOptions $options
*/
public function __construct(CurlyOptions $options = null)
{
$this->setOptions($options);
}
/**
* Executes a request with the provided URL and method.
*
* Returns a PSR-7 response object.
*
* @param string $url
* @param string $method
*
* @return ResponseInterface
*/
public function requestByUrl($url, $method = 'GET')
{
return $this->request(new Request($url, $method));
}
/**
* Executes a request with the provided PSR-7 request object.
*
* Returns a PSR-7 response object.
*
* @param RequestInterface $request
*
* @return ResponseInterface
*/
public function request(RequestInterface $request)
{
$options = clone $this->options;
$this->applyRequest($request, $options);
if ($this->customHandler !== null)
{
$this->customHandler->registerCallbacks($options);
}
$headerStream = $this->prepareHeaderStream($options);
$responseStream = $this->prepareResponseStream($options);
$channel = curl_init();
$options->apply($channel);
$result = curl_exec($channel);
if ($result === false) {
throw new CurlException(curl_error($channel), curl_errno($channel));
/**
* Holds the cURL options relevant to this instance.
*
* @var CurlyOptions
*/
private $options;
/**
* Holds the configured handler (if any).
*
* @var AbstractHandler;
*/
private $customHandler;
/**
* Creates an instance of Curly, optionally pre-configuring it with a CurlyOptions instance.
*
* @param CurlyOptions $options
*/
public function __construct(?CurlyOptions $options = null)
{
$this->setOptions($options);
}
/**
* Executes a request with the provided URL and method.
*
* Returns a PSR-7 response object.
*
* @param string $url
* @param string $method
*
* @return ResponseInterface
*/
public function requestByUrl(string $url, string $method = 'GET'): ResponseInterface
{
return $this->request(new Request($url, $method));
}
/**
* Executes a request with the provided PSR-7 request object.
*
* Returns a PSR-7 response object.
*
* @param RequestInterface $request
*
* @return ResponseInterface
*/
public function request(RequestInterface $request): ResponseInterface
{
$options = clone $this->options;
$this->applyRequest($request, $options);
if ($this->customHandler !== null) {
$this->customHandler->registerCallbacks($options);
}
$headerStream = $this->prepareHeaderStream($options);
$responseStream = $this->prepareResponseStream($options);
$channel = curl_init();
$options->apply($channel);
$result = curl_exec($channel);
if ($result === false) {
throw new CurlException(curl_error($channel), curl_errno($channel));
}
if ($responseStream !== null && $responseStream->isSeekable()) {
$responseStream->rewind();
}
$status = curl_getinfo($channel, CURLINFO_HTTP_CODE);
return new Response($responseStream ?: 'php://temp', $status, $this->parseHeaderStream($headerStream));
}
/**
* Prepares a stream to store headers in.
*
* @param CurlyOptions $options
*
* @return Stream|null
*/
private function prepareHeaderStream(CurlyOptions $options): ?Stream
{
if ($options->onHeader !== null) {
return null;
}
if ($options->outputHeaderStream === null) {
$stream = fopen('php://temp', 'r+b');
$options->outputHeaderStream = $stream;
return new Stream($stream);
}
return new Stream($options->outputHeaderStream);
}
/**
* Prepares a stream to store the response in.
*
* @param CurlyOptions $options
*
* @return Stream|null
*/
private function prepareResponseStream(CurlyOptions $options): ?Stream
{
if ($options->onWrite !== null) {
return null;
}