|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* BEdita, API-first content management framework |
|
4
|
|
|
* Copyright 2021 ChannelWeb Srl, Chialab Srl |
|
5
|
|
|
* |
|
6
|
|
|
* This file is part of BEdita: you can redistribute it and/or modify |
|
7
|
|
|
* it under the terms of the GNU Lesser General Public License as published |
|
8
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
|
9
|
|
|
* (at your option) any later version. |
|
10
|
|
|
* |
|
11
|
|
|
* See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace App\Utility; |
|
15
|
|
|
|
|
16
|
|
|
use BEdita\SDK\BEditaClientException; |
|
17
|
|
|
use BEdita\WebTools\ApiClientProvider; |
|
18
|
|
|
use Cake\Cache\Cache; |
|
19
|
|
|
use Cake\Utility\Hash; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Applications class, to get applications data |
|
23
|
|
|
*/ |
|
24
|
|
|
class Applications |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Applications |
|
28
|
|
|
* |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
public static $applications = null; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Get application name by application ID |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $applicationId The application ID |
|
37
|
|
|
* @return string |
|
38
|
|
|
*/ |
|
39
|
|
|
public static function getName(string $applicationId): string |
|
40
|
|
|
{ |
|
41
|
|
|
return (string)Hash::get(self::list(), $applicationId); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Get applications, in the form [<id>: <name>]. |
|
46
|
|
|
* This uses cache 'applications'. |
|
47
|
|
|
* |
|
48
|
|
|
* @return array |
|
49
|
|
|
*/ |
|
50
|
|
|
public static function list(): array |
|
51
|
|
|
{ |
|
52
|
|
|
if (!empty(static::$applications)) { |
|
53
|
|
|
return static::$applications; |
|
54
|
|
|
} |
|
55
|
|
|
try { |
|
56
|
|
|
static::$applications = Cache::remember( |
|
57
|
|
|
CacheTools::cacheKey('applications'), |
|
58
|
|
|
function () { |
|
59
|
|
|
$response = (array)ApiClientProvider::getApiClient()->get('applications'); |
|
60
|
|
|
|
|
61
|
|
|
return (array)Hash::combine($response, 'data.{n}.id', 'data.{n}.attributes.name'); |
|
62
|
|
|
} |
|
63
|
|
|
); |
|
64
|
|
|
} catch (BEditaClientException $e) { |
|
65
|
|
|
return []; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return static::$applications; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|