|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Zippy. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Alchemy <[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 Alchemy\Zippy\Resource; |
|
13
|
|
|
|
|
14
|
|
|
use Alchemy\Zippy\Exception\InvalidArgumentException; |
|
15
|
|
|
use Alchemy\Zippy\Resource\Reader\Guzzle\GuzzleReaderFactory; |
|
16
|
|
|
use Alchemy\Zippy\Resource\Reader\Guzzle\LegacyGuzzleReaderFactory; |
|
17
|
|
|
use Alchemy\Zippy\Resource\Resource as ZippyResource; |
|
18
|
|
|
use Alchemy\Zippy\Resource\Teleporter\GenericTeleporter; |
|
19
|
|
|
use Alchemy\Zippy\Resource\Teleporter\LocalTeleporter; |
|
20
|
|
|
use Alchemy\Zippy\Resource\Teleporter\StreamTeleporter; |
|
21
|
|
|
use Alchemy\Zippy\Resource\Teleporter\TeleporterInterface; |
|
22
|
|
|
use Alchemy\Zippy\Resource\Writer\FilesystemWriter; |
|
23
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* A container of TeleporterInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
class TeleporterContainer implements \ArrayAccess, \Countable |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var TeleporterInterface[] |
|
32
|
|
|
*/ |
|
33
|
|
|
private $teleporters = array(); |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var callable[] |
|
37
|
|
|
*/ |
|
38
|
|
|
private $factories = array(); |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Returns the appropriate TeleporterInterface for a given Resource |
|
42
|
|
|
* |
|
43
|
|
|
* @param ZippyResource $resource |
|
44
|
|
|
* |
|
45
|
|
|
* @return TeleporterInterface |
|
46
|
|
|
*/ |
|
47
|
|
|
public function fromResource(ZippyResource $resource) |
|
48
|
|
|
{ |
|
49
|
|
|
switch (true) { |
|
50
|
|
|
case is_resource($resource->getOriginal()): |
|
51
|
|
|
$teleporter = 'stream-teleporter'; |
|
52
|
|
|
break; |
|
53
|
|
|
case is_string($resource->getOriginal()): |
|
54
|
|
|
$data = parse_url($resource->getOriginal()); |
|
55
|
|
|
|
|
56
|
|
|
if (!isset($data['scheme']) || 'file' === $data['scheme']) { |
|
57
|
|
|
$teleporter = 'local-teleporter'; |
|
58
|
|
|
} elseif (in_array($data['scheme'], array('http', 'https')) && isset($this->factories['guzzle-teleporter'])) { |
|
59
|
|
|
$teleporter = 'guzzle-teleporter'; |
|
60
|
|
|
} else { |
|
61
|
|
|
$teleporter = 'stream-teleporter'; |
|
62
|
|
|
} |
|
63
|
|
|
break; |
|
64
|
|
|
default: |
|
65
|
|
|
throw new InvalidArgumentException('No teleporter found'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $this->getTeleporter($teleporter); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
private function getTeleporter($typeName) |
|
72
|
|
|
{ |
|
73
|
|
|
if (!isset($this->teleporters[$typeName])) { |
|
74
|
|
|
$factory = $this->factories[$typeName]; |
|
75
|
|
|
$this->teleporters[$typeName] = $factory(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $this->teleporters[$typeName]; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Instantiates TeleporterContainer and register default teleporters |
|
83
|
|
|
* |
|
84
|
|
|
* @return TeleporterContainer |
|
85
|
|
|
*/ |
|
86
|
|
|
public static function load() |
|
87
|
|
|
{ |
|
88
|
|
|
$container = new static(); |
|
89
|
|
|
|
|
90
|
|
|
$container->factories['stream-teleporter'] = function () { |
|
91
|
|
|
return new StreamTeleporter(); |
|
92
|
|
|
}; |
|
93
|
|
|
|
|
94
|
|
|
$container->factories['local-teleporter'] = function () { |
|
95
|
|
|
return new LocalTeleporter(new Filesystem()); |
|
96
|
|
|
}; |
|
97
|
|
|
|
|
98
|
|
|
if (class_exists('GuzzleHttp\Client')) { |
|
99
|
|
|
$container->factories['guzzle-teleporter'] = function () { |
|
100
|
|
|
return new GenericTeleporter( |
|
101
|
|
|
new GuzzleReaderFactory(), |
|
102
|
|
|
new FilesystemWriter(), |
|
103
|
|
|
new ResourceLocator() |
|
104
|
|
|
); |
|
105
|
|
|
}; |
|
106
|
|
|
} |
|
107
|
|
|
elseif (class_exists('Guzzle\Http\Client')) { |
|
108
|
|
|
$container->factories['guzzle-teleporter'] = function () { |
|
109
|
|
|
return new GenericTeleporter( |
|
110
|
|
|
new LegacyGuzzleReaderFactory(), |
|
111
|
|
|
new FilesystemWriter(), |
|
112
|
|
|
new ResourceLocator() |
|
113
|
|
|
); |
|
114
|
|
|
}; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return $container; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* (PHP 5 >= 5.0.0)<br/> |
|
122
|
|
|
* Whether a offset exists |
|
123
|
|
|
* |
|
124
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetexists.php |
|
125
|
|
|
* |
|
126
|
|
|
* @param mixed $offset <p> |
|
127
|
|
|
* An offset to check for. |
|
128
|
|
|
* </p> |
|
129
|
|
|
* |
|
130
|
|
|
* @return bool true on success or false on failure. |
|
131
|
|
|
* </p> |
|
132
|
|
|
* <p> |
|
133
|
|
|
* The return value will be casted to boolean if non-boolean was returned. |
|
134
|
|
|
*/ |
|
135
|
|
|
public function offsetExists($offset) |
|
136
|
|
|
{ |
|
137
|
|
|
return isset($this->teleporters[$offset]); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* (PHP 5 >= 5.0.0)<br/> |
|
142
|
|
|
* Offset to retrieve |
|
143
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetget.php |
|
144
|
|
|
* @param mixed $offset <p> |
|
145
|
|
|
* The offset to retrieve. |
|
146
|
|
|
* </p> |
|
147
|
|
|
* @return mixed Can return all value types. |
|
148
|
|
|
*/ |
|
149
|
|
|
public function offsetGet($offset) |
|
150
|
|
|
{ |
|
151
|
|
|
return $this->getTeleporter($offset); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* (PHP 5 >= 5.0.0)<br/> |
|
156
|
|
|
* Offset to set |
|
157
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetset.php |
|
158
|
|
|
* @param mixed $offset <p> |
|
159
|
|
|
* The offset to assign the value to. |
|
160
|
|
|
* </p> |
|
161
|
|
|
* @param mixed $value <p> |
|
162
|
|
|
* The value to set. |
|
163
|
|
|
* </p> |
|
164
|
|
|
* @return void |
|
165
|
|
|
*/ |
|
166
|
|
|
public function offsetSet($offset, $value) |
|
167
|
|
|
{ |
|
168
|
|
|
throw new \BadMethodCallException(); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* (PHP 5 >= 5.0.0)<br/> |
|
173
|
|
|
* Offset to unset |
|
174
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetunset.php |
|
175
|
|
|
* @param mixed $offset <p> |
|
176
|
|
|
* The offset to unset. |
|
177
|
|
|
* </p> |
|
178
|
|
|
* @return void |
|
179
|
|
|
*/ |
|
180
|
|
|
public function offsetUnset($offset) |
|
181
|
|
|
{ |
|
182
|
|
|
throw new \BadMethodCallException(); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
public function count() |
|
186
|
|
|
{ |
|
187
|
|
|
return count($this->teleporters); |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|