1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CMS Pico - Create websites using Pico CMS for Nextcloud. |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2019, Daniel Rudolf (<[email protected]>) |
6
|
|
|
* |
7
|
|
|
* @license GNU AGPL version 3 or any later version |
8
|
|
|
* |
9
|
|
|
* This program is free software: you can redistribute it and/or modify |
10
|
|
|
* it under the terms of the GNU Affero General Public License as |
11
|
|
|
* published by the Free Software Foundation, either version 3 of the |
12
|
|
|
* License, or (at your option) any later version. |
13
|
|
|
* |
14
|
|
|
* This program is distributed in the hope that it will be useful, |
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17
|
|
|
* GNU Affero General Public License for more details. |
18
|
|
|
* |
19
|
|
|
* You should have received a copy of the GNU Affero General Public License |
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
declare(strict_types=1); |
24
|
|
|
|
25
|
|
|
namespace OCA\CMSPico\Files; |
26
|
|
|
|
27
|
|
|
use OCP\Files\AlreadyExistsException; |
28
|
|
|
use OCP\Files\GenericFileException; |
29
|
|
|
use OCP\Files\InvalidPathException; |
30
|
|
|
use OCP\Files\NotFoundException; |
31
|
|
|
use OCP\Files\NotPermittedException; |
32
|
|
|
|
33
|
|
|
trait FolderTrait |
34
|
|
|
{ |
35
|
|
|
/** @var \Generator */ |
36
|
|
|
private $generator; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $path |
40
|
|
|
* |
41
|
|
|
* @return FolderInterface |
42
|
|
|
* @throws InvalidPathException |
43
|
|
|
* @throws NotFoundException |
44
|
|
|
* @throws GenericFileException |
45
|
|
|
*/ |
46
|
29 |
|
public function getFolder(string $path): FolderInterface |
47
|
|
|
{ |
48
|
|
|
/** @var FolderInterface $folder */ |
49
|
29 |
|
$folder = $this->get($path); |
50
|
29 |
|
if (!$folder->isFolder()) { |
51
|
|
|
throw new InvalidPathException(); |
52
|
|
|
} |
53
|
|
|
|
54
|
29 |
|
return $folder; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $path |
59
|
|
|
* |
60
|
|
|
* @return FileInterface |
61
|
|
|
* @throws InvalidPathException |
62
|
|
|
* @throws NotFoundException |
63
|
|
|
* @throws GenericFileException |
64
|
|
|
*/ |
65
|
14 |
|
public function getFile(string $path): FileInterface |
66
|
|
|
{ |
67
|
|
|
/** @var FileInterface $file */ |
68
|
14 |
|
$file = $this->get($path); |
69
|
14 |
|
if (!$file->isFile()) { |
70
|
|
|
throw new InvalidPathException(); |
71
|
|
|
} |
72
|
|
|
|
73
|
14 |
|
return $file; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $fullPath |
78
|
|
|
* |
79
|
|
|
* @return FolderInterface |
80
|
|
|
* @throws AlreadyExistsException |
81
|
|
|
* @throws InvalidPathException |
82
|
|
|
* @throws NotPermittedException |
83
|
|
|
*/ |
84
|
11 |
|
protected function newFolderRecursive(string $fullPath): FolderInterface |
85
|
|
|
{ |
86
|
11 |
|
if ($fullPath !== '/') { |
87
|
11 |
|
if (!$this->getRootFolder()->exists($fullPath)) { |
88
|
|
|
return $this->getRootFolder()->newFolder($fullPath); |
89
|
|
|
} else { |
90
|
|
|
/** @var FolderInterface $parentFolder */ |
91
|
11 |
|
$parentFolder = $this->getRootFolder()->get($fullPath); |
92
|
11 |
|
if (!$parentFolder->isFolder()) { |
93
|
|
|
throw new AlreadyExistsException(); |
94
|
|
|
} |
95
|
11 |
|
return $parentFolder; |
96
|
|
|
} |
97
|
|
|
} else { |
98
|
10 |
|
return $this->getRootFolder(); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return FolderInterface |
104
|
|
|
* @throws InvalidPathException |
105
|
|
|
*/ |
106
|
|
|
abstract protected function getRootFolder(): FolderInterface; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return \Generator |
110
|
|
|
* @throws NotPermittedException |
111
|
|
|
* @throws GenericFileException |
112
|
|
|
*/ |
113
|
|
|
abstract protected function getGenerator(): \Generator; |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @throws NotPermittedException |
117
|
|
|
* @throws GenericFileException |
118
|
|
|
*/ |
119
|
21 |
|
public function rewind(): void |
120
|
|
|
{ |
121
|
21 |
|
$this->generator = $this->getGenerator(); |
122
|
21 |
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return void |
126
|
|
|
*/ |
127
|
18 |
|
public function next(): void |
128
|
|
|
{ |
129
|
18 |
|
$this->generator->next(); |
130
|
18 |
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return bool |
134
|
|
|
*/ |
135
|
21 |
|
public function valid(): bool |
136
|
|
|
{ |
137
|
21 |
|
return $this->generator->valid(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return NodeInterface |
142
|
|
|
*/ |
143
|
18 |
|
public function current(): NodeInterface |
144
|
|
|
{ |
145
|
18 |
|
return $this->generator->current(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return int |
150
|
|
|
*/ |
151
|
|
|
public function key(): int |
152
|
|
|
{ |
153
|
|
|
return $this->generator->key(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return bool |
158
|
|
|
*/ |
159
|
3 |
|
public function hasChildren(): bool |
160
|
|
|
{ |
161
|
3 |
|
return ($this->current() instanceof FolderInterface); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @return \RecursiveIterator |
166
|
|
|
*/ |
167
|
3 |
|
public function getChildren(): \RecursiveIterator |
168
|
|
|
{ |
169
|
3 |
|
$node = $this->current(); |
170
|
3 |
|
if ($node instanceof FolderInterface) { |
171
|
|
|
/** @var FolderInterface $node */ |
172
|
3 |
|
return $node; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
throw new \InvalidArgumentException(); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|