Skip to content

Commit a0d3baf

Browse files
authored
v2 (#1)
1 parent 7581af9 commit a0d3baf

11 files changed

+176
-255
lines changed

.travis.yml

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
language: php
22
php:
3-
- '5.3'
4-
- '5.4'
53
- '5.5'
64
- '5.6'
75
- '7.0'
6+
- '7.1'
87
- hhvm
98
- nightly
109

1110
before_script:
1211
- composer install
12+
13+
script:
14+
- vendor/bin/phpcs --standard=PSR2 src/ test/
15+
- phpunit --coverage-clover build/logs/clover.xml

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2014-2016 Alexey Karapetov
3+
Copyright (c) Alexey Karapetov
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy of
66
this software and associated documentation files (the "Software"), to deal in

README.md

+6-22
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class MyApiClient {
1616
function call($url)
1717
{
1818
$ch = curl_init($url);
19-
curl_set_opt($ch, ...)
19+
curl_set_opt($ch, /*...*/)
2020
return curl_exec($ch);
2121
}
2222
}
@@ -31,12 +31,12 @@ class MyApiClient {
3131
{
3232
$this->curl = $curl;
3333
}
34-
...
34+
//...
3535
function call($url)
3636
{
3737
$this->curl->init($url);
38-
$this->curl->setOpt(...)
39-
return $this->curl->exec($ch);
38+
$this->curl->setOpt(/*...*/)
39+
return $this->curl->exec();
4040
}
4141
}
4242
```
@@ -59,8 +59,8 @@ Via [composer](https://getcomposer.org):
5959
| `$i = curl_getinfo($h, $opt);` | `$i = $curl->getInfo($opt);` |
6060
| `curl_setopt($h, $opt, $val);` | `$curl->setOpt($opt, $val);` |
6161
| `curl_setopt_array($h, $array);` | `$curl->setOptArray($array);` |
62-
| `curl_version($age)` | `Curl::version($age);` |
63-
| `curl_strerror($errornum)` | `Curl::strerror($errornum);` |
62+
| `curl_version($age)` | `$curl->version($age);` |
63+
| `curl_strerror($errornum)` | `$curl->strerror($errornum);` |
6464
| `$h2 = curl_copy_handle($h);` | `$curl2 = clone($curl);` |
6565
| `$result = curl_exec($h);` | `$result = $curl->exec();` |
6666
| `$res = curl_pause($h, $mask);` | `$res = $curl->pause($mask);` |
@@ -88,19 +88,3 @@ Via [composer](https://getcomposer.org):
8888
| `curl_share_init();` | `$cs = new CurlShare();` |
8989
| `curl_share_close($h);` | `unset($cs);` |
9090
| `$r = curl_multi_setopt($h, $opt, $val);` | `$r = $cs->setOpt($opt, $val);` |
91-
92-
##Auto retry and error control through exceptions
93-
94-
Curl::exec() can automatically retry in case of an error (i.e. network is unstable). It also is able to throw a RuntimeException if an error occurs.
95-
96-
```php
97-
$curl = new Curl('http://example.com');
98-
$curl->setOpt(CURLOPT_RETURNTRANSFER, true);
99-
try {
100-
// try 3 times, if unable throw a RuntimeException
101-
$curl->exec(3, true);
102-
} catch (CurlException $e) {
103-
$message = $e->getMessage();
104-
$curlErrorNumber = $e->getCode();
105-
}
106-
```

composer.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@
2525
}
2626
],
2727
"require": {
28-
"php": ">=5.3.0",
28+
"php": ">=5.5.0",
2929
"ext-curl": "*"
3030
},
3131
"require-dev": {
32-
"phpunit/phpunit": "^4.8 || ^5.0"
32+
"phpunit/phpunit": "^4.8 || ^5.0",
33+
"squizlabs/php_codesniffer": "^2.0"
34+
3335
},
3436
"autoload": {
3537
"psr-4": {

phpcs.xml.dist

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0"?>
2+
<ruleset>
3+
<file>src</file>
4+
<file>test</file>
5+
<exclude-pattern>vendor/*</exclude-pattern>
6+
<rule ref="PSR2"/>
7+
</ruleset>

src/Curl.php

+25-84
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,14 @@
11
<?php
22
namespace PHPCurl\CurlWrapper;
33

4-
use InvalidArgumentException;
5-
6-
/**
7-
* OOP wrapper for curl_* functions
8-
*
9-
* Functional and OOP style mapping:
10-
*
11-
* $h = curl_init($url); $curl = new Curl($url); //or $curl->init($url)
12-
* curl_close($h); unset($curl);
13-
* $e = curl_errno($h); $e = $curl->errno();
14-
* $e = curl_error($h); $e = $curl->error();
15-
* $i = curl_getinfo($h, $opt); $i = $curl->getInfo($opt);
16-
* curl_setopt($h, $opt, $val); $curl->setOpt($opt, $val);
17-
* curl_setopt_array($h, $array); $curl->setOptArray($array);
18-
* curl_version($age) Curl::version($age);
19-
* curl_strerror($errornum) Curl::strerror($errornum);
20-
* $h2 = curl_copy_handle($h); $curl2 = clone($curl);
21-
* $result = curl_exec($h); $result = $curl->exec();
22-
* $res = curl_pause($h, $mask); $res = $curl->pause($mask);
23-
* $res = curl_escape($h, $str); $res = $curl->escape($str);
24-
* $res = curl_unescape($h, $str); $res = $curl->unescape($str);
25-
*
26-
* @copyright Alexey Karapetov
27-
* @author Alexey Karapetov <[email protected]>
28-
* @license http://opensource.org/licenses/mit-license.php The MIT License (MIT)
29-
*/
304
class Curl
315
{
326
/**
33-
* curl handle
34-
*
357
* @var resource
368
*/
379
private $handle;
3810

3911
/**
40-
* Ctor
41-
*
4212
* @param string $url URL
4313
*/
4414
public function __construct($url = null)
@@ -57,26 +27,6 @@ public function init($url = null)
5727
$this->handle = curl_init($url);
5828
}
5929

60-
/**
61-
* Get curl handle
62-
*
63-
* @return resource
64-
*/
65-
public function getHandle()
66-
{
67-
return $this->handle;
68-
}
69-
70-
/**
71-
* @see curl_close()
72-
*
73-
* @return void
74-
*/
75-
public function __destruct()
76-
{
77-
curl_close($this->handle);
78-
}
79-
8030
/**
8131
* @see curl_errno()
8232
*
@@ -100,29 +50,11 @@ public function error()
10050
/**
10151
* @see curl_exec()
10252
*
103-
* @param int $attempts Connection attempts (default is 1)
104-
* @param boolean $useException Throw \RuntimeException on failure
10553
* @return boolean|string
106-
* @throws InvalidArgumentException if the number of attempts is invalid
107-
* @throws CurlException if curl_exec() returned false
10854
*/
109-
public function exec($attempts = 1, $useException = false)
55+
public function exec()
11056
{
111-
$attempts = (int) $attempts;
112-
if ($attempts < 1) {
113-
throw new InvalidArgumentException(sprintf('Attempts count is not positive: %d', $attempts));
114-
}
115-
$i = 0;
116-
while ($i++ < $attempts) {
117-
$result = curl_exec($this->handle);
118-
if ($result !== false) {
119-
break;
120-
}
121-
}
122-
if ($useException && (false === $result)) {
123-
throw new CurlException(sprintf('Error "%s" after %d attempt(s)', $this->error(), $attempts), $this->errno());
124-
}
125-
return $result;
57+
return curl_exec($this->handle);
12658
}
12759

12860
/**
@@ -168,7 +100,7 @@ public function setOptArray(array $options)
168100
* @param int $age
169101
* @return array
170102
*/
171-
static public function version($age = CURLVERSION_NOW)
103+
public function version($age = CURLVERSION_NOW)
172104
{
173105
return curl_version($age);
174106
}
@@ -177,24 +109,13 @@ static public function version($age = CURLVERSION_NOW)
177109
* @see curl_strerror()
178110
*
179111
* @param int $errornum
180-
* @return array
112+
* @return string
181113
*/
182-
static public function strerror($errornum)
114+
public function strError($errornum)
183115
{
184116
return curl_strerror($errornum);
185117
}
186118

187-
/**
188-
* __clone
189-
* Copies handle using curl_copy_handle()
190-
*
191-
* @return void
192-
*/
193-
public function __clone()
194-
{
195-
$this->handle = curl_copy_handle($this->handle);
196-
}
197-
198119
/**
199120
* @see curl_escape()
200121
*
@@ -236,4 +157,24 @@ public function pause($bitmask)
236157
{
237158
return curl_pause($this->handle, $bitmask);
238159
}
160+
161+
/**
162+
* Get curl handle
163+
*
164+
* @return resource
165+
*/
166+
public function getHandle()
167+
{
168+
return $this->handle;
169+
}
170+
171+
public function __destruct()
172+
{
173+
curl_close($this->handle);
174+
}
175+
176+
public function __clone()
177+
{
178+
$this->handle = curl_copy_handle($this->handle);
179+
}
239180
}

src/CurlException.php

-12
This file was deleted.

src/CurlMulti.php

+6-8
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,21 @@
1616
* $i = curl_multi_select($mh, $timeout); $i = $cm->select($timeout);
1717
* $r = curl_multi_setopt($h, $opt, $val); $r = $cm->setOpt($opt, $val);
1818
*
19-
* @copyright Alexey Karapetov
20-
* @author Alexey Karapetov <[email protected]>
2119
* @license http://opensource.org/licenses/mit-license.php The MIT License (MIT)
2220
*/
2321
class CurlMulti
2422
{
2523
/**
26-
* curl handle
27-
*
2824
* @var resource
2925
*/
3026
private $handle;
3127

32-
/**
33-
* __construct
34-
*/
3528
public function __construct()
29+
{
30+
$this->init();
31+
}
32+
33+
public function init()
3634
{
3735
$this->handle = curl_multi_init();
3836
}
@@ -129,7 +127,7 @@ public function select($timeout = 1.0)
129127
* @param int $errornum
130128
* @return string
131129
*/
132-
static public function strerror($errornum)
130+
public function strError($errornum)
133131
{
134132
return curl_multi_strerror($errornum);
135133
}

src/CurlShare.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,26 @@
1010
* curl_share_close($h); unset($cs);
1111
* $r = curl_share_setopt($h, $opt, $val); $r = $cs->setOpt($opt, $val);
1212
*
13-
* @copyright Alexey Karapetov
14-
* @author Alexey Karapetov <[email protected]>
1513
* @license http://opensource.org/licenses/mit-license.php The MIT License (MIT)
1614
*/
1715
class CurlShare
1816
{
1917
/**
20-
* curl handle
21-
*
2218
* @var resource
2319
*/
2420
private $handle;
2521

22+
public function __construct()
23+
{
24+
$this->init();
25+
}
26+
2627
/**
27-
* __construct
28+
* @see curl_share_init()
29+
*
30+
* @return void
2831
*/
29-
public function __construct()
32+
public function init()
3033
{
3134
$this->handle = curl_share_init();
3235
}

test/CurlMultiTest.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ class CurlMultiTest extends \PHPUnit_Framework_TestCase
5959

6060
public function testAll()
6161
{
62-
$c = $this->getMock('PHPCurl\\CurlWrapper\\Curl', array('getHandle'));
62+
$c = $this->getMockBuilder('PHPCurl\\CurlWrapper\\Curl')
63+
->setMethods(['getHandle'])
64+
->getMock();
6365
$c->expects($this->any())
6466
->method('getHandle')
6567
->will($this->returnValue('bar'));
@@ -79,7 +81,7 @@ public function testAll()
7981
$this->assertEquals('exec_foo', $cm->exec($running));
8082
$this->assertEquals(24, $running);
8183

82-
$this->assertEquals('strerror_boo', CurlMulti::strerror('boo'));
84+
$this->assertEquals('strerror_boo', $cm->strError('boo'));
8385
unset($cm);
8486
$this->assertEquals('close_foo', array_pop(self::$log));
8587
}

0 commit comments

Comments
 (0)