Skip to content

Commit d8c3168

Browse files
committed
Merge branch 'release/0.5.0'
2 parents e09aa7f + 064f3ec commit d8c3168

File tree

7 files changed

+97
-25
lines changed

7 files changed

+97
-25
lines changed

lib/Bitbucket/API/Api.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Bitbucket\API;
1313

14-
use Bitbucket\API\Http\Listener\RequestListener;
14+
use Bitbucket\API\Http\Listener\NormalizeArrayListener;
1515
use Buzz\Message\MessageInterface;
1616
use Buzz\Message\RequestInterface;
1717
use Buzz\Client\ClientInterface as BuzzClientInterface;
@@ -62,7 +62,7 @@ public function __construct(BuzzClientInterface $client = null)
6262
$this->client = (is_null($client)) ? new Curl : $client;
6363
$this->httpClient = new Client(array(), $client);
6464

65-
$this->httpClient->addListener(new RequestListener());
65+
$this->httpClient->addListener(new NormalizeArrayListener());
6666

6767
return $this;
6868
}

lib/Bitbucket/API/Http/Client.php

Lines changed: 74 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Client implements ClientInterface
3333
'api_versions' => array('1.0', '2.0'), // supported versions
3434
'format' => 'json',
3535
'formats' => array('json', 'xml'), // supported response formats
36-
'user_agent' => 'bitbucket-api-php/0.4.1 (https://bitbucket.org/gentlero/bitbucket-api)',
36+
'user_agent' => 'bitbucket-api-php/0.5.0 (https://bitbucket.org/gentlero/bitbucket-api)',
3737
'timeout' => 10,
3838
'verify_peer' => false
3939
);
@@ -44,12 +44,12 @@ class Client implements ClientInterface
4444
protected $client;
4545

4646
/**
47-
* @var MessageInterface
47+
* @var RequestInterface
4848
*/
4949
private $lastRequest;
5050

5151
/**
52-
* @var RequestInterface
52+
* @var MessageInterface
5353
*/
5454
private $lastResponse;
5555

@@ -58,6 +58,16 @@ class Client implements ClientInterface
5858
*/
5959
protected $listeners = array();
6060

61+
/**
62+
* @var MessageInterface
63+
*/
64+
protected $responseObj;
65+
66+
/**
67+
* @var RequestInterface
68+
*/
69+
protected $requestObj;
70+
6171
public function __construct(array $options = array(), BuzzClientInterface $client = null)
6272
{
6373
$this->client = (is_null($client)) ? new Curl : $client;
@@ -70,9 +80,14 @@ public function __construct(array $options = array(), BuzzClientInterface $clien
7080
/**
7181
* {@inheritDoc}
7282
*/
73-
public function addListener(ListenerInterface $listener)
83+
public function addListener(ListenerInterface $listener, $priority = 0)
7484
{
75-
$this->listeners[$listener->getName()] = $listener;
85+
// Don't allow same listener with different priorities.
86+
if ($this->isListener($listener->getName())) {
87+
$this->delListener($listener->getName());
88+
}
89+
90+
$this->listeners[$priority][$listener->getName()] = $listener;
7691

7792
return $this;
7893
}
@@ -87,7 +102,9 @@ public function delListener($name)
87102
}
88103

89104
if ($this->isListener($name) === true) {
90-
unset($this->listeners[$name]);
105+
foreach ($this->listeners as $collection) {
106+
unset($collection[$name]);
107+
}
91108
}
92109

93110
return $this;
@@ -98,19 +115,19 @@ public function delListener($name)
98115
*/
99116
public function getListener($name)
100117
{
101-
if (!$this->isListener($name)) {
118+
if (!$listener = $this->searchListener($name)) {
102119
throw new \InvalidArgumentException(sprintf('Unknown listener %s', $name));
103120
}
104121

105-
return $this->listeners[$name];
122+
return $listener;
106123
}
107124

108125
/**
109126
* {@inheritDoc}
110127
*/
111128
public function isListener($name)
112129
{
113-
return isset($this->listeners[$name]);
130+
return ($this->searchListener($name) instanceof ListenerInterface);
114131
}
115132

116133
/**
@@ -175,7 +192,7 @@ public function request($endpoint, $params = array(), $method = 'GET', array $he
175192
$request->setContent(is_array($params) ? http_build_query($params) : $params);
176193
}
177194

178-
$response = new Response;
195+
$response = is_object($this->responseObj) ? $this->responseObj : new Response;
179196

180197
$this->executeListeners($request, 'preSend');
181198

@@ -280,6 +297,26 @@ public function getLastResponse()
280297
return $this->lastResponse;
281298
}
282299

300+
/**
301+
* @access public
302+
* @param MessageInterface $response
303+
* @return void
304+
*/
305+
public function setResponse(MessageInterface $response)
306+
{
307+
$this->responseObj = $response;
308+
}
309+
310+
/**
311+
* @access public
312+
* @param RequestInterface $request
313+
* @return void
314+
*/
315+
public function setRequest(RequestInterface $request)
316+
{
317+
$this->requestObj = $request;
318+
}
319+
283320
/**
284321
* @access protected
285322
* @param string $method
@@ -288,7 +325,8 @@ public function getLastResponse()
288325
*/
289326
protected function createRequest($method, $url)
290327
{
291-
$request = new Request($method);
328+
$request = is_object($this->requestObj) ? $this->requestObj : new Request();
329+
$request->setMethod($method);
292330
$request->addHeaders(array(
293331
'User-Agent' => $this->options['user_agent']
294332
));
@@ -322,8 +360,31 @@ protected function executeListeners(RequestInterface $request, $when = 'preSend'
322360
$params[] = $response;
323361
}
324362

325-
foreach ($this->listeners as $listener) {
326-
call_user_func_array(array($listener, $when), $params);
363+
ksort($this->listeners, SORT_ASC);
364+
365+
array_walk_recursive(
366+
$this->listeners,
367+
function ($class) use ($when, $params) {
368+
if ($class instanceof ListenerInterface) {
369+
call_user_func_array(array($class, $when), $params);
370+
}
371+
}
372+
);
373+
}
374+
375+
/**
376+
* @access protected
377+
* @param string $name Listener name
378+
* @return ListenerInterface|bool false on error
379+
*/
380+
protected function searchListener($name)
381+
{
382+
foreach ($this->listeners as $collection) {
383+
if (isset($collection[$name])) {
384+
return $collection[$name];
385+
}
327386
}
387+
388+
return false;
328389
}
329390
}

lib/Bitbucket/API/Http/Listener/RequestListener.php renamed to lib/Bitbucket/API/Http/Listener/NormalizeArrayListener.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,23 @@
1515
use Buzz\Message\RequestInterface;
1616

1717
/**
18+
* Transform PHP array to API array
19+
*
20+
* PHP array square brackets does not play nice with remote API,
21+
* which expects just the key name, without brackets.
22+
*
23+
* Transforms: foo[0]=xxx&foo[1]=yyy" to "foo=xxx&foo=yyy"
24+
*
1825
* @author Alexandru G. <alex@gentle.ro>
1926
*/
20-
class RequestListener implements ListenerInterface
27+
class NormalizeArrayListener implements ListenerInterface
2128
{
2229
/**
2330
* {@inheritDoc}
2431
*/
2532
public function getName()
2633
{
27-
return 'request';
34+
return 'normalize_array';
2835
}
2936

3037
/**

lib/Bitbucket/API/Repositories.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Bitbucket\API;
1313

14+
use Buzz\Message\MessageInterface;
15+
1416
/**
1517
* @author Alexandru G. <alex@gentle.ro>
1618
*/
@@ -25,7 +27,7 @@ class Repositories extends Api
2527
*
2628
* @access public
2729
* @param string $owner The account of the repo owner.
28-
* @return mixed
30+
* @return MessageInterface
2931
*
3032
* @api 2.0
3133
* @since Method available since 0.2.0

lib/Bitbucket/API/Repositories/PullRequests.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,8 @@ public function accept($account, $repo, $id, $params = array())
290290
{
291291
return $this->getClient()->setApiVersion('2.0')->post(
292292
sprintf('repositories/%s/%s/pullrequests/%d/merge', $account, $repo, $id),
293-
$params
293+
json_encode($params),
294+
array('Content-Type' => 'application/json')
294295
);
295296
}
296297

@@ -308,7 +309,8 @@ public function decline($account, $repo, $id, $params = array())
308309
{
309310
return $this->getClient()->setApiVersion('2.0')->post(
310311
sprintf('repositories/%s/%s/pullrequests/%d/decline', $account, $repo, $id),
311-
$params
312+
json_encode($params),
313+
array('Content-Type' => 'application/json')
312314
);
313315
}
314316
}

test/Bitbucket/Tests/API/Http/Listener/RequestListenerTest.php renamed to test/Bitbucket/Tests/API/Http/Listener/NormalizeArrayListenerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22

33
namespace Bitbucket\Tests\API\Http\Listener;
44

5-
use Bitbucket\API\Http\Listener\RequestListener;
5+
use Bitbucket\API\Http\Listener\NormalizeArrayListener;
66
use Bitbucket\Tests\API as Tests;
77
use Buzz\Message\Request;
88

99
/**
1010
* @author Alexandru G. <alex@gentle.ro>
1111
*/
12-
class RequestListenerTest extends Tests\TestCase
12+
class NormalizeArrayListenerTest extends Tests\TestCase
1313
{
1414
public function testPHPArrayToApiArrayConversion()
1515
{
16-
$listener = new RequestListener();
16+
$listener = new NormalizeArrayListener();
1717
$request = new Request('POST', '/dummy');
1818

1919
$request->setContent(

test/Bitbucket/Tests/API/Repositories/PullRequestsTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ public function testAcceptAndMergeAPullRequest()
267267
$client = $this->getHttpClientMock();
268268
$client->expects($this->once())
269269
->method('post')
270-
->with($endpoint, $params);
270+
->with($endpoint, json_encode($params));
271271

272272
/** @var \Bitbucket\API\Repositories\PullRequests $pull */
273273
$pull = $this->getClassMock('Bitbucket\API\Repositories\PullRequests', $client);
@@ -285,7 +285,7 @@ public function testDeclineAPullRequest()
285285
$client = $this->getHttpClientMock();
286286
$client->expects($this->once())
287287
->method('post')
288-
->with($endpoint, $params);
288+
->with($endpoint, json_encode($params));
289289

290290
/** @var \Bitbucket\API\Repositories\PullRequests $pull */
291291
$pull = $this->getClassMock('Bitbucket\API\Repositories\PullRequests', $client);

0 commit comments

Comments
 (0)