|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of graze/data-file |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
* @license https://github.com/graze/data-file/blob/master/LICENSE.md |
|
11
|
|
|
* @link https://github.com/graze/data-file |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Graze\DataFile\Node; |
|
15
|
|
|
|
|
16
|
|
|
use Graze\DataFile\Modify\Compress\CompressionFactory; |
|
17
|
|
|
use GuzzleHttp\Psr7\LazyOpenStream; |
|
18
|
|
|
use League\Flysystem\Adapter\Local; |
|
19
|
|
|
use League\Flysystem\Filesystem; |
|
20
|
|
|
use Psr\Http\Message\StreamInterface; |
|
21
|
|
|
|
|
22
|
|
|
class LocalFile extends FileNode implements LocalFileNodeInterface, NodeStreamInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $compression; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string|null |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $encoding; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param string $path |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct($path) |
|
38
|
108 |
|
{ |
|
39
|
|
|
parent::__construct(new FileSystem(new Local('/')), $path); |
|
40
|
108 |
|
|
|
41
|
|
|
$this->compression = CompressionFactory::TYPE_NONE; |
|
42
|
108 |
|
$this->encoding = null; |
|
43
|
108 |
|
} |
|
44
|
108 |
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* {@inheritdoc} |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getContents() |
|
49
|
14 |
|
{ |
|
50
|
|
|
if ($this->exists() && |
|
51
|
14 |
|
$this->getCompression() != CompressionFactory::TYPE_NONE |
|
52
|
|
|
) { |
|
53
|
|
|
$factory = new CompressionFactory(); |
|
54
|
|
|
$compressor = $factory->getDeCompressor($this->getCompression()); |
|
55
|
|
|
$uncompressed = $compressor->decompress($this); |
|
56
|
|
|
$content = $uncompressed->getContents(); |
|
57
|
|
|
$uncompressed->delete(); |
|
58
|
|
|
return $content; |
|
59
|
15 |
|
} else { |
|
60
|
|
|
return parent::getContents(); |
|
61
|
15 |
|
} |
|
62
|
|
|
} |
|
63
|
15 |
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Get a stream for the given node |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $mode |
|
68
|
|
|
* |
|
69
|
20 |
|
* @return StreamInterface |
|
70
|
|
|
*/ |
|
71
|
20 |
|
public function getStream($mode = 'c+') |
|
72
|
19 |
|
{ |
|
73
|
20 |
|
return new LazyOpenStream($this->getPath(), $mode); |
|
74
|
2 |
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|