1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Client\Twitter; |
4
|
|
|
|
5
|
|
|
use ApiClients\Foundation\Client; |
|
|
|
|
6
|
|
|
use ApiClients\Foundation\Factory; |
7
|
|
|
use ApiClients\Foundation\Hydrator\CommandBus\Command\HydrateCommand; |
8
|
|
|
use ApiClients\Foundation\Oauth1\Middleware\Oauth1Middleware; |
9
|
|
|
use ApiClients\Foundation\Oauth1\Options as Oauth1Options; |
10
|
|
|
use ApiClients\Foundation\Options; |
11
|
|
|
use ApiClients\Foundation\Transport\CommandBus\Command\RequestCommand; |
12
|
|
|
use ApiClients\Foundation\Transport\CommandBus\Command\StreamingRequestCommand; |
13
|
|
|
use ApiClients\Foundation\Transport\Options as TransportOptions; |
14
|
|
|
use ApiClients\Tools\Psr7\Oauth1\Definition; |
15
|
|
|
use GuzzleHttp\Psr7\Request; |
16
|
|
|
use Psr\Http\Message\RequestInterface; |
17
|
|
|
use Psr\Http\Message\ResponseInterface; |
18
|
|
|
use React\EventLoop\LoopInterface; |
19
|
|
|
use React\Promise\PromiseInterface; |
20
|
|
|
use Rx\Extra\Operator\CutOperator; |
21
|
|
|
use Rx\Observable; |
22
|
|
|
use Rx\React\Promise; |
23
|
|
|
use function React\Promise\resolve; |
24
|
|
|
|
25
|
|
|
final class AsyncStreamingClient |
26
|
|
|
{ |
27
|
|
|
const STREAM_DELIMITER = "\r\n"; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var LoopInterface |
31
|
|
|
*/ |
32
|
|
|
private $loop; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Client |
36
|
|
|
*/ |
37
|
|
|
protected $client; |
38
|
|
|
|
39
|
|
|
public function __construct(Client $client) |
40
|
|
|
{ |
41
|
|
|
$this->client = $client; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function sample(): Observable |
45
|
|
|
{ |
46
|
|
|
return $this->stream( |
47
|
|
|
new Request('GET', 'https://stream.twitter.com/1.1/statuses/sample.json') |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function filtered(array $filter = []): Observable |
52
|
|
|
{ |
53
|
|
|
$postData = http_build_query($filter); |
54
|
|
|
return $this->stream( |
55
|
|
|
new Request( |
56
|
|
|
'POST', |
57
|
|
|
'https://stream.twitter.com/1.1/statuses/filter.json', |
58
|
|
|
[ |
59
|
|
|
'Content-Type' => 'application/x-www-form-urlencoded', |
60
|
|
|
'Content-Length' => strlen($postData), |
61
|
|
|
], |
62
|
|
|
$postData |
63
|
|
|
) |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected function stream(RequestInterface $request): Observable |
68
|
|
|
{ |
69
|
|
|
return Promise::toObservable($this->client->handle(new StreamingRequestCommand( |
70
|
|
|
$request |
71
|
|
|
)))->switchLatest()->lift(function () { |
72
|
|
|
return new CutOperator(self::STREAM_DELIMITER); |
73
|
|
|
})->filter(function (string $json) { |
74
|
|
|
return trim($json) !== ''; // To keep the stream alive Twitter sends an empty line at times |
75
|
|
|
})->jsonDecode()->flatMap(function (array $document) { |
76
|
|
|
if (isset($document['delete'])) { |
77
|
|
|
return Promise::toObservable($this->client->handle( |
78
|
|
|
new HydrateCommand('DeletedTweet', $document['delete']) |
79
|
|
|
)); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return Promise::toObservable($this->client->handle(new HydrateCommand('Tweet', $document))); |
83
|
|
|
}); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: