|
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 GuzzleHttp\Psr7\Stream; |
|
19
|
|
|
use League\Flysystem\Adapter\Local; |
|
20
|
|
|
use League\Flysystem\Filesystem; |
|
21
|
|
|
use Psr\Http\Message\StreamInterface; |
|
22
|
|
|
|
|
23
|
|
|
class LocalFile extends FileNode implements LocalFileNodeInterface, NodeStreamInterface |
|
24
|
104 |
|
{ |
|
25
|
|
|
/** |
|
26
|
104 |
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
104 |
|
protected $compression; |
|
29
|
104 |
|
|
|
30
|
104 |
|
/** |
|
31
|
|
|
* @var string|null |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $encoding; |
|
34
|
|
|
|
|
35
|
13 |
|
/** |
|
36
|
|
|
* @param string $path |
|
37
|
13 |
|
*/ |
|
38
|
|
|
public function __construct($path) |
|
39
|
|
|
{ |
|
40
|
|
|
parent::__construct(new FileSystem(new Local('/')), $path); |
|
41
|
|
|
|
|
42
|
|
|
$this->compression = CompressionFactory::TYPE_NONE; |
|
43
|
|
|
$this->encoding = null; |
|
44
|
|
|
} |
|
45
|
14 |
|
|
|
46
|
|
|
/** |
|
47
|
14 |
|
* @return string |
|
|
|
|
|
|
48
|
|
|
*/ |
|
49
|
14 |
|
public function getEncoding() |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->encoding; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
20 |
|
* @param string $encoding |
|
56
|
|
|
* |
|
57
|
20 |
|
* @return $this |
|
58
|
19 |
|
*/ |
|
59
|
20 |
|
public function setEncoding($encoding) |
|
60
|
2 |
|
{ |
|
61
|
2 |
|
$this->encoding = $encoding; |
|
62
|
2 |
|
|
|
63
|
2 |
|
return $this; |
|
64
|
2 |
|
} |
|
65
|
2 |
|
|
|
66
|
|
|
/** |
|
67
|
20 |
|
* {@inheritdoc} |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getContents() |
|
70
|
|
|
{ |
|
71
|
|
|
if ($this->exists() && |
|
72
|
|
|
$this->getCompression() != CompressionFactory::TYPE_NONE |
|
73
|
|
|
) { |
|
74
|
30 |
|
$factory = new CompressionFactory(); |
|
75
|
|
|
$compressor = $factory->getDeCompressor($this->getCompression()); |
|
76
|
30 |
|
$uncompressed = $compressor->decompress($this); |
|
77
|
|
|
$content = $uncompressed->getContents(); |
|
78
|
|
|
$uncompressed->delete(); |
|
79
|
|
|
return $content; |
|
80
|
|
|
} else { |
|
81
|
|
|
return parent::getContents(); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
21 |
|
|
|
85
|
|
|
/** |
|
86
|
21 |
|
* @return string - see CompressionFactory |
|
87
|
|
|
*/ |
|
88
|
21 |
|
public function getCompression() |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->compression; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param string $compression - @see CompressionFactory |
|
95
|
|
|
* |
|
96
|
|
|
* @return $this |
|
97
|
|
|
*/ |
|
98
|
|
|
public function setCompression($compression) |
|
99
|
|
|
{ |
|
100
|
|
|
$this->compression = $compression; |
|
101
|
|
|
|
|
102
|
|
|
return $this; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Get a stream for the given node |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $mode |
|
109
|
|
|
* |
|
110
|
|
|
* @return StreamInterface |
|
111
|
|
|
*/ |
|
112
|
|
|
public function getStream($mode = 'c+') |
|
113
|
|
|
{ |
|
114
|
|
|
return new LazyOpenStream($this->getPath(), $mode); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.