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\InvalidArgumentException; |
15
|
|
|
use Alchemy\Zippy\Exception\IOException; |
16
|
|
|
use Alchemy\Zippy\Resource\Resource as ZippyResource; |
17
|
|
|
use Alchemy\Zippy\Resource\ResourceLocator; |
18
|
|
|
use Symfony\Component\Filesystem\Exception\IOException as SfIOException; |
19
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* This class transports an object using the local filesystem |
23
|
|
|
*/ |
24
|
|
|
class LocalTeleporter extends AbstractTeleporter |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var Filesystem |
28
|
|
|
*/ |
29
|
|
|
private $filesystem; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var ResourceLocator |
33
|
|
|
*/ |
34
|
|
|
private $resourceLocator; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Constructor |
38
|
|
|
* |
39
|
|
|
* @param Filesystem $filesystem |
40
|
|
|
*/ |
41
|
|
|
public function __construct(Filesystem $filesystem) |
42
|
|
|
{ |
43
|
|
|
$this->filesystem = $filesystem; |
44
|
|
|
$this->resourceLocator = new ResourceLocator(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
public function teleport(ZippyResource $resource, $context) |
51
|
|
|
{ |
52
|
|
|
$target = $this->resourceLocator->mapResourcePath($resource, $context); |
53
|
|
|
$path = $resource->getOriginal(); |
54
|
|
|
|
55
|
|
|
if (!file_exists($path)) { |
56
|
|
|
throw new InvalidArgumentException(sprintf('Invalid path %s', $path)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
try { |
60
|
|
|
if (is_file($path)) { |
61
|
|
|
$this->filesystem->copy($path, $target); |
62
|
|
|
} elseif (is_dir($path)) { |
63
|
|
|
$this->filesystem->mirror($path, $target); |
64
|
|
|
} else { |
65
|
|
|
throw new InvalidArgumentException(sprintf('Invalid file or directory %s', $path)); |
66
|
|
|
} |
67
|
|
|
} catch (SfIOException $e) { |
68
|
|
|
throw new IOException(sprintf('Could not write %s', $target), $e->getCode(), $e); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Creates the LocalTeleporter |
74
|
|
|
* |
75
|
|
|
* @return LocalTeleporter |
76
|
|
|
* @deprecated This method will be removed in a future release (0.5.x) |
77
|
|
|
*/ |
78
|
|
|
public static function create() |
79
|
|
|
{ |
80
|
|
|
return new static(new Filesystem()); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.