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
|
108 |
|
public function __construct($path) |
38
|
|
|
{ |
39
|
108 |
|
parent::__construct(new FileSystem(new Local('/')), $path); |
40
|
|
|
|
41
|
108 |
|
$this->compression = CompressionFactory::TYPE_NONE; |
42
|
108 |
|
$this->encoding = null; |
43
|
108 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
20 |
|
public function getContents() |
49
|
|
|
{ |
50
|
20 |
|
if ($this->exists() && |
51
|
20 |
|
$this->getCompression() != CompressionFactory::TYPE_NONE |
52
|
|
|
) { |
53
|
2 |
|
$factory = new CompressionFactory(); |
54
|
2 |
|
$compressor = $factory->getDeCompressor($this->getCompression()); |
55
|
2 |
|
$uncompressed = $compressor->decompress($this); |
56
|
2 |
|
$content = $uncompressed->getContents(); |
57
|
2 |
|
$uncompressed->delete(); |
58
|
2 |
|
return $content; |
59
|
|
|
} else { |
60
|
20 |
|
return parent::getContents(); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get a stream for the given node |
66
|
|
|
* |
67
|
|
|
* @param string $mode |
68
|
|
|
* |
69
|
|
|
* @return StreamInterface |
70
|
|
|
*/ |
71
|
2 |
|
public function getStream($mode = 'c+') |
72
|
|
|
{ |
73
|
2 |
|
return new LazyOpenStream($this->getPath(), $mode); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|