|
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\Teleporter; |
|
13
|
|
|
|
|
14
|
|
|
use Alchemy\Zippy\Exception\IOException; |
|
15
|
|
|
use Alchemy\Zippy\Resource\Resource as ZippyResource; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class AbstractTeleporter |
|
19
|
|
|
* @package Alchemy\Zippy\Resource\Teleporter |
|
20
|
|
|
* |
|
21
|
|
|
* @deprecated Typehint against TeleporterInterface instead and use GenericTeleporter |
|
22
|
|
|
* with custom reader/writers instead. This class will be removed in v0.5.x |
|
23
|
|
|
*/ |
|
24
|
|
|
abstract class AbstractTeleporter implements TeleporterInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Writes the target |
|
28
|
|
|
* |
|
29
|
|
|
* @param string $data |
|
30
|
|
|
* @param ZippyResource $resource |
|
31
|
|
|
* @param string $context |
|
32
|
|
|
* |
|
33
|
|
|
* @return TeleporterInterface |
|
34
|
|
|
* |
|
35
|
|
|
* @throws IOException |
|
36
|
|
|
*/ |
|
37
|
|
|
protected function writeTarget($data, ZippyResource $resource, $context) |
|
38
|
|
|
{ |
|
39
|
|
|
$target = $this->getTarget($context, $resource); |
|
40
|
|
|
|
|
41
|
|
|
if (!file_exists(dirname($target)) && false === mkdir(dirname($target))) { |
|
42
|
|
|
throw new IOException(sprintf('Could not create parent directory %s', dirname($target))); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
if (false === file_put_contents($target, $data)) { |
|
46
|
|
|
throw new IOException(sprintf('Could not write to %s', $target)); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $this; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Returns the relative target of a Resource |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $context |
|
56
|
|
|
* @param ZippyResource $resource |
|
57
|
|
|
* |
|
58
|
|
|
* @return string |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function getTarget($context, ZippyResource $resource) |
|
61
|
|
|
{ |
|
62
|
|
|
return sprintf('%s/%s', rtrim($context, '/'), $resource->getTarget()); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|