These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Baguette\Mastodon; |
||
4 | |||
5 | use Baguette\Mastodon; |
||
0 ignored issues
–
show
|
|||
6 | use Baguette\Mastodon\Service\SessionStorage; |
||
7 | use Psr\Http\Message\ResponseInterface; |
||
8 | |||
9 | /** |
||
10 | * Mastodon API Client |
||
11 | * |
||
12 | * @author USAMI Kenta <[email protected]> |
||
13 | * @copyright 2017 Baguette HQ |
||
14 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPL-3.0 |
||
15 | */ |
||
16 | final class Client |
||
17 | { |
||
18 | const USER_AGENT = 'PhpMastodon/0.0.1(+https://github.com/zonuexe/php-mastodon-client)'; |
||
19 | |||
20 | /** @var string */ |
||
21 | private $instance; |
||
22 | /** @var string|null */ |
||
23 | private $client_name; |
||
24 | /** @var \GuzzleHttp\Client */ |
||
25 | private $api_http_client; |
||
26 | /** @var \GuzzleHttp\Client */ |
||
27 | private $oauth_http_client; |
||
0 ignored issues
–
show
|
|||
28 | |||
29 | /** |
||
30 | * @param string $instance |
||
31 | * @param array $options |
||
32 | */ |
||
33 | 10 | public function __construct($instance, array $options = []) |
|
34 | { |
||
35 | 10 | $this->instance = $instance; |
|
36 | |||
37 | 10 | if (isset($options['client_name'])) { |
|
38 | $this->client_name = $options['client_name']; |
||
39 | } |
||
40 | 10 | } |
|
41 | |||
42 | /** |
||
43 | * @param string $method "GET"|"POST" |
||
44 | * @param string $path |
||
45 | * @param array $options |
||
46 | * @param SessionStorage $session |
||
47 | * @return ResponseInterface |
||
48 | */ |
||
49 | 1 | public function requestAPI($method, $path, array $options, SessionStorage $session) |
|
50 | { |
||
51 | 1 | $request_options = $options + $this->getRequestAPIOptions($session); |
|
52 | |||
53 | 1 | return $this->getAPIHttpClient()->request($method, $path, $request_options); |
|
54 | } |
||
55 | |||
56 | /** |
||
57 | * @return void |
||
58 | */ |
||
59 | 1 | public function setAPIHttpClient(\GuzzleHttp\Client $client) |
|
60 | { |
||
61 | 1 | $this->api_http_client = $client; |
|
62 | 1 | } |
|
63 | |||
64 | /** |
||
65 | * @return \GuzzleHttp\Client |
||
66 | */ |
||
67 | 1 | private function getAPIHttpClient() |
|
68 | { |
||
69 | 1 | if (empty($this->api_http_client)) { |
|
70 | $this->api_http_client = new \GuzzleHttp\Client([ |
||
71 | 'base_uri' => sprintf('%s://%s', $this->getScheme(), $this->getHostname()), |
||
72 | ]); |
||
73 | } |
||
74 | |||
75 | 1 | return $this->api_http_client; |
|
76 | } |
||
77 | |||
78 | /** |
||
79 | * @param SessionStorage $session |
||
80 | * @return array |
||
81 | */ |
||
82 | 1 | private function getRequestAPIOptions(SessionStorage $session) |
|
83 | { |
||
84 | 1 | if (isset($this->client_name)) { |
|
85 | $user_agent = sprintf('%s; %s; %s', self::USER_AGENT, $this->client_name, \GuzzleHttp\default_user_agent()); |
||
86 | } else { |
||
87 | 1 | $user_agent = sprintf('%s; %s', self::USER_AGENT, \GuzzleHttp\default_user_agent()); |
|
88 | } |
||
89 | |||
90 | return [ |
||
91 | 'headers' => [ |
||
92 | 1 | 'Authorization' => "Bearer {$session->getAccessToken()}", |
|
93 | 1 | 'User-Agent' => $user_agent, |
|
94 | ], |
||
95 | ]; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @return string |
||
100 | */ |
||
101 | 4 | public function getScheme() |
|
102 | { |
||
103 | 4 | return parse_url($this->instance, PHP_URL_SCHEME) ?: 'https'; |
|
104 | } |
||
105 | |||
106 | /** |
||
107 | * @return string |
||
108 | */ |
||
109 | 4 | public function getHostname() |
|
110 | { |
||
111 | 4 | return parse_url($this->instance, PHP_URL_HOST) ?: $this->instance; |
|
112 | } |
||
113 | } |
||
114 |
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: