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\Model; |
26
|
|
|
|
27
|
|
|
use OCA\CMSPico\Exceptions\ThemeNotCompatibleException; |
28
|
|
|
use OCA\CMSPico\Files\FolderInterface; |
29
|
|
|
use OCA\CMSPico\Pico; |
30
|
|
|
use OCA\CMSPico\Service\MiscService; |
31
|
|
|
use OCP\Files\InvalidPathException; |
32
|
|
|
use OCP\Files\NotFoundException; |
33
|
|
|
use OCP\Files\NotPermittedException; |
34
|
|
|
use Symfony\Component\Yaml\Exception\ParseException as YamlParseException; |
35
|
|
|
use Symfony\Component\Yaml\Parser as YamlParser; |
36
|
|
|
use function OCA\CMSPico\t; |
37
|
|
|
|
38
|
|
|
class Theme implements \JsonSerializable |
39
|
|
|
{ |
40
|
|
|
/** @var int */ |
41
|
|
|
public const TYPE_SYSTEM = 1; |
42
|
|
|
|
43
|
|
|
/** @var int */ |
44
|
|
|
public const TYPE_CUSTOM = 2; |
45
|
|
|
|
46
|
|
|
/** @var int[] */ |
47
|
|
|
public const THEME_API_VERSIONS = [ |
48
|
|
|
Pico::API_VERSION_0, |
49
|
|
|
Pico::API_VERSION_1, |
50
|
|
|
Pico::API_VERSION_2, |
51
|
|
|
Pico::API_VERSION_3, |
52
|
|
|
]; |
53
|
|
|
|
54
|
|
|
/** @var MiscService */ |
55
|
|
|
private $miscService; |
56
|
|
|
|
57
|
|
|
/** @var FolderInterface */ |
58
|
|
|
private $folder; |
59
|
|
|
|
60
|
|
|
/** @var int */ |
61
|
|
|
private $type; |
62
|
|
|
|
63
|
|
|
/** @var bool|null */ |
64
|
|
|
private $compat; |
65
|
|
|
|
66
|
|
|
/** @var ThemeNotCompatibleException|null */ |
67
|
|
|
private $compatException; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Theme constructor. |
71
|
|
|
* |
72
|
|
|
* @param FolderInterface $folder |
73
|
|
|
* @param int $type |
74
|
|
|
*/ |
75
|
4 |
|
public function __construct(FolderInterface $folder, int $type = self::TYPE_SYSTEM) |
76
|
|
|
{ |
77
|
4 |
|
$this->miscService = \OC::$server->query(MiscService::class); |
|
|
|
|
78
|
|
|
|
79
|
4 |
|
$this->folder = $folder; |
80
|
4 |
|
$this->type = $type; |
81
|
4 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
4 |
|
public function getName(): string |
87
|
|
|
{ |
88
|
4 |
|
return $this->folder->getName(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return FolderInterface |
93
|
|
|
*/ |
94
|
4 |
|
public function getFolder(): FolderInterface |
95
|
|
|
{ |
96
|
4 |
|
return $this->folder; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return int |
101
|
|
|
*/ |
102
|
4 |
|
public function getType(): int |
103
|
|
|
{ |
104
|
4 |
|
return $this->type; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
4 |
|
public function isCompatible(): bool |
111
|
|
|
{ |
112
|
4 |
|
if ($this->compat !== null) { |
113
|
4 |
|
return $this->compat; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
try { |
117
|
4 |
|
$this->checkCompatibility(); |
118
|
4 |
|
return true; |
119
|
|
|
} catch (ThemeNotCompatibleException $e) { |
120
|
|
|
return false; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @throws ThemeNotCompatibleException |
126
|
|
|
*/ |
127
|
4 |
|
public function checkCompatibility(): void |
128
|
|
|
{ |
129
|
4 |
|
if ($this->compat === false) { |
130
|
|
|
throw $this->compatException; |
131
|
4 |
|
} elseif ($this->compat) { |
132
|
|
|
return; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
try { |
136
|
|
|
try { |
137
|
4 |
|
$this->getFolder()->getFile('index.twig'); |
138
|
|
|
} catch (InvalidPathException | NotFoundException $e) { |
139
|
|
|
throw new ThemeNotCompatibleException( |
140
|
|
|
$this->getName(), |
141
|
|
|
t('Incompatible theme: Twig template "{file}" not found.'), |
142
|
|
|
[ 'file' => $this->getName() . '/index.twig' ] |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
try { |
147
|
4 |
|
$themeConfigFile = $this->getFolder()->getFile('pico-theme.yml'); |
148
|
1 |
|
$themeConfigYaml = $themeConfigFile->getContent(); |
149
|
|
|
|
150
|
1 |
|
$themeConfig = (new YamlParser())->parse($themeConfigYaml); |
151
|
1 |
|
$themeConfig = is_array($themeConfig) ? $themeConfig : []; |
152
|
3 |
|
} catch (InvalidPathException | NotFoundException | NotPermittedException | YamlParseException $e) { |
153
|
3 |
|
$themeConfig = []; |
154
|
|
|
} |
155
|
|
|
|
156
|
4 |
|
$apiVersion = Pico::API_VERSION_0; |
157
|
4 |
|
if (isset($themeConfig['api_version'])) { |
158
|
1 |
|
if (is_int($themeConfig['api_version']) || preg_match('/^[0-9]+$/', $themeConfig['api_version'])) { |
159
|
1 |
|
$apiVersion = (int) $themeConfig['api_version']; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
4 |
|
if (!in_array($apiVersion, static::THEME_API_VERSIONS, true)) { |
164
|
|
|
throw new ThemeNotCompatibleException( |
165
|
|
|
$this->getName(), |
166
|
|
|
t('Incompatible theme: Themes for Pico CMS for Nextcloud must use one of the API versions ' |
167
|
|
|
. '{compatApiVersions}, but this theme uses API version {apiVersion}.'), |
168
|
|
|
[ 'compatApiVersions' => implode(', ', static::THEME_API_VERSIONS), 'apiVersion' => $apiVersion ] |
169
|
|
|
); |
170
|
|
|
} |
171
|
|
|
|
172
|
4 |
|
$this->compat = true; |
173
|
4 |
|
$this->compatException = null; |
174
|
|
|
} catch (ThemeNotCompatibleException $e) { |
175
|
|
|
$this->compat = false; |
176
|
|
|
$this->compatException = $e; |
177
|
|
|
|
178
|
|
|
throw $e; |
179
|
|
|
} |
180
|
4 |
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @return array |
184
|
|
|
*/ |
185
|
4 |
|
public function toArray(): array |
186
|
|
|
{ |
187
|
|
|
$data = [ |
188
|
4 |
|
'name' => $this->getName(), |
189
|
4 |
|
'type' => $this->getType(), |
190
|
4 |
|
'compat' => $this->isCompatible(), |
191
|
|
|
]; |
192
|
|
|
|
193
|
4 |
|
if (!$this->isCompatible() && ($this->compatException !== null)) { |
194
|
|
|
$data['compatReason'] = $this->compatException->getRawReason(); |
195
|
|
|
$data['compatReasonData'] = $this->compatException->getRawReasonData(); |
196
|
|
|
} |
197
|
|
|
|
198
|
4 |
|
return $data; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @return array |
203
|
|
|
*/ |
204
|
4 |
|
public function jsonSerialize(): array |
205
|
|
|
{ |
206
|
4 |
|
return $this->toArray(); |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.