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\Format\FormatAwareInterface; |
17
|
|
|
use Graze\DataFile\Format\FormatAwareTrait; |
18
|
|
|
use Graze\DataFile\Modify\Compress\CompressionAwareInterface; |
19
|
|
|
use Graze\DataFile\Modify\Compress\CompressionAwareTrait; |
20
|
|
|
use Graze\DataFile\Modify\Encoding\EncodingAwareInterface; |
21
|
|
|
use Graze\DataFile\Modify\Encoding\EncodingAwareTrait; |
22
|
|
|
use Graze\DataFile\Modify\Exception\CopyFailedException; |
23
|
|
|
use Graze\DataFile\Node\FileSystem\FilesystemWrapper; |
24
|
|
|
use Graze\DataFile\Node\FileSystem\FilesystemWrapperInterface; |
25
|
|
|
use League\Flysystem\File; |
26
|
|
|
use League\Flysystem\FilesystemInterface; |
27
|
|
|
|
28
|
|
|
class FileNode extends File implements FileNodeInterface, FormatAwareInterface, CompressionAwareInterface, EncodingAwareInterface |
29
|
|
|
{ |
30
|
|
|
use FormatAwareTrait; |
31
|
|
|
use CompressionAwareTrait; |
32
|
|
|
use EncodingAwareTrait; |
33
|
|
|
|
34
|
|
|
/** @var FilesystemWrapperInterface */ |
35
|
|
|
private $wrapper; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* FileNode constructor. |
39
|
|
|
* |
40
|
|
|
* @param FilesystemInterface $filesystem |
41
|
|
|
* @param null|string $path |
42
|
|
|
*/ |
43
|
113 |
|
public function __construct(FilesystemInterface $filesystem, $path) |
44
|
|
|
{ |
45
|
113 |
|
$this->wrapper = new FilesystemWrapper($filesystem); |
46
|
113 |
|
parent::__construct($this->wrapper, $path); |
47
|
113 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
14 |
|
public function __toString() |
53
|
|
|
{ |
54
|
14 |
|
return $this->getPath(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return mixed |
59
|
|
|
*/ |
60
|
10 |
|
public function getDirectory() |
61
|
|
|
{ |
62
|
10 |
|
return pathinfo($this->path, PATHINFO_DIRNAME) . '/'; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
18 |
|
public function getFilename() |
69
|
|
|
{ |
70
|
18 |
|
return pathinfo($this->path, PATHINFO_BASENAME); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Returns the contents of the file as an array. |
75
|
|
|
* |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
21 |
|
public function getContents() |
79
|
|
|
{ |
80
|
21 |
|
if ($this->exists()) { |
81
|
19 |
|
return explode("\n", trim($this->read())); |
82
|
|
|
} else { |
83
|
2 |
|
return []; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string|null $newPath |
89
|
|
|
* |
90
|
|
|
* @return static |
91
|
|
|
* @throws CopyFailedException When it is unable to copy the file |
92
|
|
|
*/ |
93
|
4 |
|
public function copy($newPath = null) |
94
|
|
|
{ |
95
|
4 |
|
if (is_null($newPath)) { |
96
|
1 |
|
$newPath = $this->path . '-copy'; |
97
|
1 |
|
} |
98
|
|
|
|
99
|
4 |
|
if (@$this->filesystem->copy($this->path, $newPath)) { |
100
|
3 |
|
return $this->getClone()->setPath($newPath); |
101
|
|
|
} else { |
102
|
1 |
|
$lastError = error_get_last(); |
103
|
1 |
|
throw new CopyFailedException($this, $newPath, $lastError['message']); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return FilesystemWrapperInterface |
109
|
|
|
*/ |
110
|
17 |
|
public function getFilesystem() |
111
|
|
|
{ |
112
|
17 |
|
return $this->wrapper; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param FilesystemInterface $filesystem |
117
|
|
|
* |
118
|
|
|
* @return $this |
119
|
|
|
*/ |
120
|
1 |
|
public function setFilesystem(FilesystemInterface $filesystem) |
121
|
|
|
{ |
122
|
1 |
|
$this->wrapper = new FilesystemWrapper($filesystem); |
123
|
1 |
|
return parent::setFilesystem($this->wrapper); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Return a clone of this object |
128
|
|
|
* |
129
|
|
|
* @return static |
130
|
|
|
*/ |
131
|
57 |
|
public function getClone() |
132
|
|
|
{ |
133
|
57 |
|
return clone $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Clone sub objects |
138
|
|
|
*/ |
139
|
57 |
|
public function __clone() |
140
|
|
|
{ |
141
|
57 |
|
if ($this->format) { |
142
|
1 |
|
$this->format = clone $this->format; |
143
|
1 |
|
} |
144
|
57 |
|
} |
145
|
|
|
} |
146
|
|
|
|