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
|
|
|
private static $instance = null; |
17
|
2 |
|
public static function getInstance() |
18
|
|
|
{ |
19
|
2 |
|
return self::$instance ?: self::$instance = new self; |
20
|
|
|
} |
21
|
|
|
|
22
|
1 |
|
private function __construct() |
23
|
|
|
{ |
24
|
|
|
// do nothing |
25
|
1 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* don't need Authorization |
29
|
|
|
* @var array { |
30
|
|
|
* 'origin.example.com' => x |
31
|
|
|
* } |
32
|
|
|
*/ |
33
|
|
|
private $connections = array(); |
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* need Authorization header |
37
|
|
|
* @var array { |
38
|
|
|
* 'origin.example.com' => x |
39
|
|
|
* } |
40
|
|
|
*/ |
41
|
|
|
private $authConnections = array(); |
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* get cached curl handler |
45
|
|
|
* @param string $origin |
46
|
|
|
* @param bool $auth |
47
|
|
|
* @return resource<curl> |
|
|
|
|
48
|
|
|
*/ |
49
|
1 |
|
public static function getConnection($origin, $auth=false) |
50
|
|
|
{ |
51
|
1 |
|
$instance = self::getInstance(); |
52
|
1 |
|
if ($auth) { |
53
|
1 |
|
if (isset($instance->authConnections[$origin])) { |
54
|
1 |
|
return $instance->authConnections[$origin]; |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
return $instance->authConnections[$origin] = curl_init(); |
58
|
|
|
} else { |
59
|
1 |
|
if (isset($instance->connections[$origin])) { |
60
|
1 |
|
return $instance->connections[$origin]; |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
return $instance->connections[$origin] = curl_init(); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return Aspects\JoinPoint |
69
|
|
|
*/ |
70
|
1 |
|
public static function getPreEvent(Aspects\HttpGetRequest $req) |
71
|
|
|
{ |
72
|
1 |
|
$pre = new Aspects\JoinPoint('pre-download', $req); |
73
|
1 |
|
$pre->attach(static::getAspectAuth()); |
74
|
1 |
|
$pre->attach(new Aspects\AspectRedirect); |
75
|
1 |
|
$pre->attach(new Aspects\AspectProxy); |
76
|
1 |
|
return $pre; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return Aspects\JoinPoint |
81
|
|
|
*/ |
82
|
1 |
|
public static function getPostEvent(Aspects\HttpGetRequest $req) |
83
|
|
|
{ |
84
|
1 |
|
$post = new Aspects\JoinPoint('post-download', $req); |
85
|
1 |
|
$post->attach(static::getAspectAuth()); |
86
|
1 |
|
return $post; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return Aspects\AspectAuth (same instance) |
91
|
|
|
*/ |
92
|
2 |
|
public static function getAspectAuth() |
93
|
|
|
{ |
94
|
2 |
|
static $auth; |
95
|
2 |
|
return $auth ?: $auth = new Aspects\AspectAuth; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.