1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the Composer Proxy Plugin package. |
||
5 | * |
||
6 | * (c) hugh.li <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | namespace HughCube\Composer\ProxyPlugin\Config; |
||
13 | |||
14 | use HughCube\PUrl\Url; |
||
15 | |||
16 | /** |
||
17 | * Helper of package config. |
||
18 | * |
||
19 | * @author hugh.li <[email protected]> |
||
20 | */ |
||
21 | final class Config |
||
22 | { |
||
23 | /** |
||
24 | * @var array proxies |
||
25 | */ |
||
26 | private $proxies; |
||
27 | |||
28 | /** |
||
29 | * Constructor. |
||
30 | * |
||
31 | * @param array $proxies The config |
||
32 | */ |
||
33 | public function __construct(array $proxies) |
||
34 | { |
||
35 | $this->proxies = $proxies; |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * Get the http proxy of url. |
||
40 | * |
||
41 | * @param string $url |
||
42 | * @param bool $isSsl |
||
43 | * |
||
44 | * @return null|string |
||
45 | */ |
||
46 | public function getHttpProxy($url, $isSsl = false) |
||
47 | { |
||
48 | $protocol = $isSsl ? 'https' : 'http'; |
||
49 | |||
50 | $proxy = $this->getProxy($url, $protocol); |
||
51 | if (!is_array($proxy)) { |
||
52 | return null; |
||
53 | } |
||
54 | |||
55 | return Url::instance() |
||
56 | ->withScheme((isset($proxy['protocol']) ? $proxy['protocol'] : null)) |
||
57 | ->withHost((isset($proxy['host']) ? $proxy['host'] : null)) |
||
58 | ->withPort((isset($proxy['port']) ? $proxy['port'] : null)) |
||
59 | ->withUserInfo( |
||
60 | (isset($proxy['username']) ? $proxy['username'] : null), |
||
61 | (isset($proxy['password']) ? $proxy['password'] : null) |
||
62 | ) |
||
63 | ->toString(); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Get the https proxy of url. |
||
68 | * |
||
69 | * @param string $url |
||
70 | * |
||
71 | * @return null|string |
||
72 | */ |
||
73 | public function getHttpsProxy($url) |
||
74 | { |
||
75 | return $this->getHttpProxy($url, true); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Get the proxy of url and protocol. |
||
80 | * |
||
81 | * @param string $url |
||
82 | * @param string $protocol |
||
83 | * |
||
84 | * @return null|array |
||
85 | */ |
||
86 | private function getProxy($url, $protocol) |
||
87 | { |
||
88 | $url = Url::instance($url); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
89 | |||
90 | foreach ($this->proxies as $proxy) { |
||
91 | if (isset($proxy['active']) && !$proxy['active']) { |
||
92 | continue; |
||
93 | } |
||
94 | |||
95 | if ($protocol !== $proxy['protocol']) { |
||
96 | continue; |
||
97 | } |
||
98 | |||
99 | $nonProxyHosts = isset($proxy['nonProxyHosts']) ? $proxy['nonProxyHosts'] : null; |
||
100 | if (null != $nonProxyHosts && $url->matchHost($proxy['nonProxyHosts'])) { |
||
101 | continue; |
||
102 | } |
||
103 | |||
104 | $proxyHosts = isset($proxy['proxyHosts']) ? $proxy['proxyHosts'] : null; |
||
105 | if (null != $proxyHosts && !$url->matchHost($proxy['proxyHosts'])) { |
||
106 | continue; |
||
107 | } |
||
108 | |||
109 | return $proxy; |
||
110 | } |
||
111 | |||
112 | return null; |
||
113 | } |
||
114 | } |
||
115 |