|
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\Helper\Builder\BuilderAwareInterface; |
|
17
|
|
|
use Graze\DataFile\Helper\Builder\BuilderTrait; |
|
18
|
|
|
use Graze\DataFile\Modify\Compress\CompressionFactory; |
|
19
|
|
|
use League\Flysystem\Adapter\Local; |
|
20
|
|
|
use League\Flysystem\Filesystem; |
|
21
|
|
|
|
|
22
|
|
|
class LocalFile extends FileNode implements LocalFileNodeInterface, NodeStreamInterface, BuilderAwareInterface |
|
23
|
|
|
{ |
|
24
|
|
|
use BuilderTrait; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param string $path |
|
28
|
|
|
*/ |
|
29
|
108 |
|
public function __construct($path) |
|
30
|
|
|
{ |
|
31
|
108 |
|
parent::__construct(new FileSystem(new Local('/')), $path); |
|
32
|
108 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* {@inheritdoc} |
|
36
|
|
|
*/ |
|
37
|
20 |
|
public function getContents() |
|
38
|
|
|
{ |
|
39
|
20 |
|
if ($this->exists() && |
|
40
|
20 |
|
!in_array($this->getCompression(), [CompressionFactory::TYPE_NONE, CompressionFactory::TYPE_UNKNOWN]) |
|
41
|
|
|
) { |
|
42
|
|
|
/** @var CompressionFactory $factory */ |
|
43
|
2 |
|
$factory = $this->getBuilder()->build(CompressionFactory::class); |
|
44
|
2 |
|
$compressor = $factory->getDeCompressor($this->getCompression()); |
|
45
|
2 |
|
$uncompressed = $compressor->decompress($this); |
|
46
|
2 |
|
$content = $uncompressed->getContents(); |
|
47
|
2 |
|
$uncompressed->delete(); |
|
48
|
2 |
|
return $content; |
|
49
|
|
|
} else { |
|
50
|
20 |
|
return parent::getContents(); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Get a stream for the given node |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $mode |
|
58
|
|
|
* |
|
59
|
|
|
* @return resource |
|
60
|
|
|
*/ |
|
61
|
2 |
|
public function getStream($mode = 'c+') |
|
62
|
|
|
{ |
|
63
|
2 |
|
return fopen($this->getPath(), $mode); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|