Skip to content

Commit

Permalink
Merge pull request #6 from Art4/requests-v1-support
Browse files Browse the repository at this point in the history
Add support for Requests v1
  • Loading branch information
Art4 authored Oct 31, 2022
2 parents 28814ea + 2e05673 commit 89a9637
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 3 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,14 @@ jobs:

# Install dependencies and handle caching in one go.
# @link https://github.com/marketplace/actions/install-composer-dependencies
- name: "Install Composer dependencies (PHP < 7.2)"
if: ${{ matrix.php < '7.2' }}
uses: "ramsey/composer-install@v2"
with:
composer-options: --prefer-lowest

- name: "Install Composer dependencies (PHP < 8.2)"
if: ${{ matrix.php < '8.2' }}
if: ${{ matrix.php >= '7.2' && matrix.php < '8.2' }}
uses: "ramsey/composer-install@v2"

- name: "Install Composer dependencies (PHP 8.2)"
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Add compatability with Requests v1
- Add tests to improve test coverage

## [1.0.0-beta.1](https://github.com/Art4/WP-Requests-PSR18-Adapter/compare/1.0.0-beta...1.0.0-beta.1)
Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
"php": "^7.1 || ^8.0",
"psr/http-client": "^1.0",
"psr/http-factory": "^1.0",
"rmccue/requests": "^2.0"
"rmccue/requests": "^1.8 || ^2.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.0",
"yoast/phpunit-polyfills": "^1.0"
"phpunit/phpunit": "^6 || ^7 || ^8 || ^9",
"yoast/phpunit-polyfills": "^1.0.3"
},
"autoload": {
"files": ["v1-compat/autoload.php"],
"psr-4": {
"Art4\\Requests\\": "src"
}
Expand Down
44 changes: 44 additions & 0 deletions v1-compat/InvalidArgument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* @source https://github.com/WordPress/Requests/blob/v2.0.0/src/Exception/InvalidArgument.php
*/

namespace WpOrg\Requests\Exception;

use InvalidArgumentException;

/**
* Exception for an invalid argument passed.
*
* @package Requests\Exceptions
* @since 2.0.0
*/
final class InvalidArgument extends InvalidArgumentException {

/**
* Create a new invalid argument exception with a standardized text.
*
* @param int $position The argument position in the function signature. 1-based.
* @param string $name The argument name in the function signature.
* @param string $expected The argument type expected as a string.
* @param string $received The actual argument type received.
*
* @return \WpOrg\Requests\Exception\InvalidArgument
*/
public static function create($position, $name, $expected, $received) {
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace
$stack = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);

return new self(
sprintf(
'%s::%s(): Argument #%d (%s) must be of type %s, %s given',
$stack[1]['class'],
$stack[1]['function'],
$position,
$name,
$expected,
$received
)
);
}
}
76 changes: 76 additions & 0 deletions v1-compat/Port.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* Port utilities for Requests
*
* @package Requests\Utilities
* @since 2.0.0
* @source https://github.com/WordPress/Requests/blob/v2.0.0/src/Port.php
*/

namespace WpOrg\Requests;

use WpOrg\Requests\Exception;
use WpOrg\Requests\Exception\InvalidArgument;

/**
* Find the correct port depending on the Request type.
*
* @package Requests\Utilities
* @since 2.0.0
*/
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 \WpOrg\Requests\Exception\InvalidArgument When a non-string input has been passed.
* @throws \WpOrg\Requests\Exception 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}");
}
}
13 changes: 13 additions & 0 deletions v1-compat/autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

if (! class_exists('WpOrg\Requests\Requests') && class_exists('Requests')) {
class_alias('Requests', 'WpOrg\Requests\Requests');
class_alias('Requests_Exception', 'WpOrg\Requests\Exception');
class_alias('Requests_Exception_Transport', 'WpOrg\Requests\Exception\Transport');
class_alias('Requests_IRI', 'WpOrg\Requests\Iri');
class_alias('Requests_Response', 'WpOrg\Requests\Response');
class_alias('Requests_Transport', 'WpOrg\Requests\Transport');

require_once(dirname(__FILE__) . '/InvalidArgument.php');
require_once(dirname(__FILE__) . '/Port.php');
}

0 comments on commit 89a9637

Please sign in to comment.