|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* hirak/prestissimo |
|
4
|
|
|
* @author Hiraku NAKANO |
|
5
|
|
|
* @license MIT https://github.com/hirak/prestissimo |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace Hirak\Prestissimo; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* cache manager for curl handler |
|
11
|
|
|
* |
|
12
|
|
|
* Singleton |
|
13
|
|
|
*/ |
|
14
|
|
|
final class Factory |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
2 |
|
* not need Authorization |
|
18
|
|
|
* @var array { |
|
19
|
2 |
|
* 'origin.example.com' => x |
|
20
|
|
|
* } |
|
21
|
|
|
*/ |
|
22
|
1 |
|
private static $connections = array(); |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
1 |
|
* need Authorization header |
|
26
|
|
|
* @var array { |
|
27
|
|
|
* 'origin.example.com' => x |
|
28
|
|
|
* } |
|
29
|
|
|
*/ |
|
30
|
|
|
private static $authConnections = array(); |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* get cached curl handler |
|
34
|
|
|
* @param string $origin |
|
35
|
|
|
* @param bool $auth |
|
36
|
|
|
* @return resource<curl> |
|
|
|
|
|
|
37
|
|
|
*/ |
|
38
|
|
|
public static function getConnection($origin, $auth=false) |
|
39
|
|
|
{ |
|
40
|
|
|
if ($auth) { |
|
41
|
|
|
if (isset(self::$authConnections[$origin])) { |
|
42
|
|
|
return self::$authConnections[$origin]; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return self::$authConnections[$origin] = curl_init(); |
|
46
|
|
|
} else { |
|
47
|
|
|
if (isset(self::$connections[$origin])) { |
|
48
|
|
|
return self::$connections[$origin]; |
|
49
|
1 |
|
} |
|
50
|
|
|
|
|
51
|
1 |
|
return self::$connections[$origin] = curl_init(); |
|
52
|
1 |
|
} |
|
53
|
1 |
|
} |
|
54
|
1 |
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return Aspects\JoinPoint |
|
57
|
1 |
|
*/ |
|
58
|
|
|
public static function getPreEvent(Aspects\HttpGetRequest $req) |
|
59
|
1 |
|
{ |
|
60
|
1 |
|
$pre = new Aspects\JoinPoint('pre-download', $req); |
|
61
|
|
|
$pre->attach(static::getAspectAuth()); |
|
62
|
|
|
$pre->attach(new Aspects\AspectRedirect); |
|
63
|
1 |
|
$pre->attach(new Aspects\AspectProxy); |
|
64
|
|
|
return $pre; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return Aspects\JoinPoint |
|
69
|
|
|
*/ |
|
70
|
1 |
|
public static function getPostEvent(Aspects\HttpGetRequest $req) |
|
71
|
|
|
{ |
|
72
|
1 |
|
$post = new Aspects\JoinPoint('post-download', $req); |
|
73
|
1 |
|
$post->attach(static::getAspectAuth()); |
|
74
|
1 |
|
return $post; |
|
75
|
1 |
|
} |
|
76
|
1 |
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return Aspects\AspectAuth (same instance) |
|
79
|
|
|
*/ |
|
80
|
|
|
public static function getAspectAuth() |
|
81
|
|
|
{ |
|
82
|
1 |
|
static $auth; |
|
83
|
|
|
return $auth ?: $auth = new Aspects\AspectAuth; |
|
84
|
1 |
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.