Skip to content

Commit 54ea748

Browse files
php cs fixer
scrutinize php version refactor multi curl
1 parent 3db9856 commit 54ea748

19 files changed

+364
-120
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
/.idea/
33
/.php_cs.cache
44
/.php_cs
5+
/composer.lock

.scrutinizer.yml

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,32 @@ checks:
22
php: true
33

44
build:
5-
environment:
6-
php: 7.2.0
75
tests:
86
override:
97
-
108
command: 'vendor/bin/phpunit --coverage-clover=clover'
119
coverage:
1210
file: 'clover'
1311
format: 'clover'
12+
13+
analysis:
14+
project_setup:
15+
override: true
16+
tests:
17+
override: [php-scrutinizer-run]
18+
1419
nodes:
15-
analysis:
16-
project_setup:
17-
override: true
18-
tests:
19-
override: [php-scrutinizer-run]
20+
php56:
21+
environment:
22+
php: 5.6
23+
24+
php71:
25+
environment:
26+
php: 7.1
27+
28+
php72:
29+
environment:
30+
php: 7.2
2031

2132
filter:
2233
paths: ["src/*"]

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@
55
[![Code Coverage](https://scrutinizer-ci.com/g/php-guard/curl/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/php-guard/curl/?branch=master)
66
[![GPL Licence](https://badges.frapsoft.com/os/gpl/gpl.png?v=103)](https://opensource.org/licenses/GPL-3.0/)
77

8-
Installation
8+
Installation
9+
10+
This library is an alternative to "https://github.com/php-curl-class/php-curl-class". It implements classes representing Curl queries and their response that can more easily be manipulated and extended.

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
}
1818
},
1919
"require": {
20+
"php": "^7.1",
2021
"ext-curl": "*",
2122
"ext-json": "*",
2223
"ext-mbstring": "*",

src/Collection/Headers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* php-guard/curl <https://github.com/php-guard/curl>
4-
* Copyright (C) 2018 by Alexandre Le Borgne <alexandre.leborgne.83@gmail.com>.
4+
* Copyright (C) ${YEAR} by Alexandre Le Borgne <alexandre.leborgne.83@gmail.com>.
55
*
66
* This program is free software: you can redistribute it and/or modify
77
* it under the terms of the GNU General Public License as published by

src/Curl.php

Lines changed: 57 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -40,84 +40,87 @@ public function __construct(?string $host = null)
4040
}
4141

4242
/**
43-
* @param string $url
44-
* @param null|array|string $query
45-
* @param array $headers
43+
* @param string $url
44+
* @param null $query
45+
* @param array $headers
4646
*
47-
* @return CurlRequest
47+
* @return CurlResponse
48+
*
49+
* @throws CurlError
4850
*/
49-
public function get(string $url, $query = null, array $headers = [])
51+
public function get(string $url, $query = null, array $headers = []): CurlResponse
5052
{
51-
return $this->curlRequestFactory->create('GET', $url, null, $query, $headers);
53+
return $this->curlRequestFactory->create('GET', $url, null, $query, $headers)->execute();
5254
}
5355

56+
/**
57+
* @param string $url
58+
* @param null $data
59+
* @param null $query
60+
* @param array $headers
61+
*
62+
* @return CurlResponse
63+
*
64+
* @throws CurlError
65+
*/
5466
public function post(string $url, $data = null, $query = null, array $headers = [])
5567
{
56-
return $this->curlRequestFactory->create('POST', $url, $data, $query, $headers);
68+
return $this->curlRequestFactory->create('POST', $url, $data, $query, $headers)->execute();
5769
}
5870

71+
/**
72+
* @param string $url
73+
* @param null $data
74+
* @param null $query
75+
* @param array $headers
76+
*
77+
* @return CurlResponse
78+
*
79+
* @throws CurlError
80+
*/
5981
public function put(string $url, $data = null, $query = null, array $headers = [])
6082
{
61-
return $this->curlRequestFactory->create('PUT', $url, $data, $query, $headers);
83+
return $this->curlRequestFactory->create('PUT', $url, $data, $query, $headers)->execute();
6284
}
6385

86+
/**
87+
* @param string $url
88+
* @param null $data
89+
* @param null $query
90+
* @param array $headers
91+
*
92+
* @return CurlResponse
93+
*
94+
* @throws CurlError
95+
*/
6496
public function patch(string $url, $data = null, $query = null, array $headers = [])
6597
{
66-
return $this->curlRequestFactory->create('PATCH', $url, $data, $query, $headers);
98+
return $this->curlRequestFactory->create('PATCH', $url, $data, $query, $headers)->execute();
6799
}
68100

101+
/**
102+
* @param string $url
103+
* @param null $data
104+
* @param null $query
105+
* @param array $headers
106+
*
107+
* @return CurlResponse
108+
*
109+
* @throws CurlError
110+
*/
69111
public function delete(string $url, $data = null, $query = null, array $headers = [])
70112
{
71-
return $this->curlRequestFactory->create('DELETE', $url, $data, $query, $headers);
113+
return $this->curlRequestFactory->create('DELETE', $url, $data, $query, $headers)->execute();
72114
}
73115

74116
/**
75-
* @param CurlRequest[] $requests
76-
* @param array $options Définit les options pour le gestionnaire multiple cURL
117+
* @param array $options Définit les options pour le gestionnaire multiple cURL
77118
*
78-
* @return CurlResponse[]
79-
*
80-
* @throws CurlError
119+
* @return MultiCurl
81120
*/
82-
public function multi(array $requests, array $options = [])
121+
public function multi(array $options = [])
83122
{
84-
$mh = curl_multi_init();
85-
86-
foreach ($options as $key => $value) {
87-
curl_multi_setopt($mh, $key, $value);
88-
}
89-
90-
$chs = [];
91-
foreach ($requests as $request) {
92-
$ch = $this->prepare($request);
93-
$chs[] = $ch;
94-
curl_multi_add_handle($mh, $ch);
95-
}
96-
97-
$active = null;
98-
99-
do {
100-
$code = curl_multi_exec($mh, $active);
101-
curl_multi_select($mh);
102-
} while ($active > 0);
103-
104-
try {
105-
if ($code > CURLM_OK) {
106-
throw new CurlError(curl_multi_strerror($code), $code);
107-
}
108-
109-
$responses = [];
110-
foreach ($chs as $ch) {
111-
$responses[] = $this->curlResponseFactory->create($ch, curl_multi_getcontent($ch));
112-
}
113-
114-
return $responses;
115-
} finally {
116-
foreach ($chs as $ch) {
117-
curl_multi_remove_handle($mh, $ch);
118-
}
119-
curl_multi_close($mh);
120-
}
123+
return new MultiCurl($this, $options);
121124
}
122125

123126
/**
@@ -129,7 +132,7 @@ public function multi(array $requests, array $options = [])
129132
*/
130133
public function execute(CurlRequest $request): CurlResponse
131134
{
132-
$ch = $this->prepare($request);
135+
$ch = $this->requestModifierPipeline->process($request)->resource();
133136

134137
try {
135138
return $this->curlResponseFactory->create($ch, curl_exec($ch));
@@ -138,30 +141,6 @@ public function execute(CurlRequest $request): CurlResponse
138141
}
139142
}
140143

141-
/**
142-
* @param CurlRequest $request
143-
*
144-
* @return resource
145-
*/
146-
protected function prepare(CurlRequest $request)
147-
{
148-
$request = $this->requestModifierPipeline->process($request);
149-
150-
// create curl resource
151-
$ch = curl_init();
152-
153-
curl_setopt($ch, CURLOPT_URL, $request->getUrl());
154-
curl_setopt($ch, CURLOPT_POSTFIELDS, $request->getData());
155-
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $request->getMethod());
156-
curl_setopt($ch, CURLOPT_HTTPHEADER, $request->getHeaders()->toHttp());
157-
158-
foreach ($request->getCurlOptions()->all() as $key => $value) {
159-
curl_setopt($ch, $key, $value);
160-
}
161-
162-
return $ch;
163-
}
164-
165144
/**
166145
* @return RequestModifierPipeline
167146
*/

src/CurlRequest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,21 @@ public function execute(bool $throwExceptionOnHttpError = false): CurlResponse
156156

157157
return $response;
158158
}
159+
160+
public function resource()
161+
{
162+
// create curl resource
163+
$ch = curl_init();
164+
165+
curl_setopt($ch, CURLOPT_URL, $this->getUrl());
166+
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->getData());
167+
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->getMethod());
168+
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->getHeaders()->toHttp());
169+
170+
foreach ($this->getCurlOptions()->all() as $key => $value) {
171+
curl_setopt($ch, $key, $value);
172+
}
173+
174+
return $ch;
175+
}
159176
}

src/CurlRequestFactory.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class CurlRequestFactory
3939
/**
4040
* @var null|string
4141
*/
42-
private $host;
42+
private $baseUrl;
4343
/**
4444
* @var Curl
4545
*/
@@ -53,14 +53,18 @@ public function __construct(Curl $curl, ?string $host = null)
5353

5454
$this->defaultHeaders = new Headers();
5555
$this->defaultCurlOptions = new CurlOptions(self::DEFAULT_CURL_OPTIONS);
56-
$this->host = $host;
56+
$this->baseUrl = $host;
5757
$this->curl = $curl;
5858
}
5959

6060
public function create(string $method, string $url, $data = null, $query = null, array $headers = [])
6161
{
62-
if ($this->host && is_null(parse_url($url, PHP_URL_HOST))) {
63-
$url = $this->host.$url;
62+
if ($this->baseUrl && is_null(parse_url($url, PHP_URL_HOST))) {
63+
if (isset($url[0]) && '/' != $url[0]) {
64+
$url = '/'.$url;
65+
}
66+
67+
$url = $this->baseUrl.$url;
6468
}
6569

6670
if (!empty($query)) {
@@ -103,23 +107,23 @@ public function getDefaultHeaders(): Headers
103107
/**
104108
* @return null|string
105109
*/
106-
public function getHost(): ?string
110+
public function getBaseUrl(): ?string
107111
{
108-
return $this->host;
112+
return $this->baseUrl;
109113
}
110114

111115
/**
112-
* @param null|string $host
116+
* @param null|string $baseUrl
113117
*
114118
* @return CurlRequestFactory
115119
*/
116-
public function setHost(?string $host): self
120+
public function setBaseUrl(?string $baseUrl): self
117121
{
118-
if ($host) {
119-
$host = rtrim($host, '/');
122+
if ($baseUrl) {
123+
$baseUrl = rtrim($baseUrl, '/');
120124
}
121125

122-
$this->host = $host;
126+
$this->baseUrl = $baseUrl;
123127

124128
return $this;
125129
}

src/CurlResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class CurlResponse
3232
private $rawResponse;
3333

3434
/**
35-
* @var array
35+
* @var Headers
3636
*/
3737
private $headers;
3838

0 commit comments

Comments
 (0)