|
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->getBaseFolder()->exists($fullPath)) { |
|
88
|
|
|
return $this->getBaseFolder()->newFolder($fullPath); |
|
89
|
|
|
} else { |
|
90
|
|
|
/** @var FolderInterface $parentFolder */ |
|
91
|
11 |
|
$parentFolder = $this->getBaseFolder()->get($fullPath); |
|
92
|
11 |
|
if (!$parentFolder->isFolder()) { |
|
93
|
|
|
throw new AlreadyExistsException(); |
|
94
|
|
|
} |
|
95
|
11 |
|
return $parentFolder; |
|
96
|
|
|
} |
|
97
|
|
|
} else { |
|
98
|
10 |
|
return $this->getBaseFolder(); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return FolderInterface |
|
104
|
|
|
*/ |
|
105
|
|
|
abstract protected function getBaseFolder(): FolderInterface; |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return \Generator |
|
109
|
|
|
* @throws NotPermittedException |
|
110
|
|
|
* @throws GenericFileException |
|
111
|
|
|
*/ |
|
112
|
|
|
abstract protected function getGenerator(): \Generator; |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @throws NotPermittedException |
|
116
|
|
|
* @throws GenericFileException |
|
117
|
|
|
*/ |
|
118
|
21 |
|
public function rewind(): void |
|
119
|
|
|
{ |
|
120
|
21 |
|
$this->generator = $this->getGenerator(); |
|
121
|
21 |
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @return void |
|
125
|
|
|
*/ |
|
126
|
18 |
|
public function next(): void |
|
127
|
|
|
{ |
|
128
|
18 |
|
$this->generator->next(); |
|
129
|
18 |
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @return bool |
|
133
|
|
|
*/ |
|
134
|
21 |
|
public function valid(): bool |
|
135
|
|
|
{ |
|
136
|
21 |
|
return $this->generator->valid(); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @return NodeInterface |
|
141
|
|
|
*/ |
|
142
|
18 |
|
public function current(): NodeInterface |
|
143
|
|
|
{ |
|
144
|
18 |
|
return $this->generator->current(); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return int |
|
149
|
|
|
*/ |
|
150
|
|
|
public function key(): int |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->generator->key(); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @return bool |
|
157
|
|
|
*/ |
|
158
|
3 |
|
public function hasChildren(): bool |
|
159
|
|
|
{ |
|
160
|
3 |
|
return ($this->current() instanceof FolderInterface); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @return \RecursiveIterator |
|
165
|
|
|
*/ |
|
166
|
3 |
|
public function getChildren(): \RecursiveIterator |
|
167
|
|
|
{ |
|
168
|
3 |
|
$node = $this->current(); |
|
169
|
3 |
|
if ($node instanceof FolderInterface) { |
|
170
|
|
|
/** @var FolderInterface $node */ |
|
171
|
3 |
|
return $node; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
throw new \InvalidArgumentException(); |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|