-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathPluginConfigManager.php
91 lines (82 loc) · 2.59 KB
/
PluginConfigManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
/*
* This file is part of UCRM Plugin SDK.
*
* Copyright (c) 2019 Ubiquiti Inc.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Ubnt\UcrmPluginSdk\Service;
use Ubnt\UcrmPluginSdk\Exception\InvalidPluginRootPathException;
use Ubnt\UcrmPluginSdk\Exception\JsonException;
/**
* This class can be used to retrieve plugin's configuration from `data/config.json` file.
*
* @see https://github.com/Ubiquiti-App/UCRM-plugins/blob/master/docs/file-structure.md#dataconfigjson
*/
class PluginConfigManager extends AbstractOptionsManager
{
private const CONFIG_JSON = 'data/config.json';
/**
* @var mixed[]
*/
private array $config = [];
/**
* Plugin root path is configured automatically if standard directory structure is used.
* That is, UCRM Plugin SDK resides in `vendor/ubnt` directory inside of plugin's root.
*
* If this is not the case, you can use the `$pluginRootPath` parameter to specify the path.
*/
public static function create(?string $pluginRootPath = null): self
{
return new self($pluginRootPath);
}
/**
* Returns (cached) associative array, which holds plugin's configuration from `data/config.json` file.
*
* @see https://github.com/Ubiquiti-App/UCRM-plugins/blob/master/docs/file-structure.md#dataconfigjson
*
* Example usage:
*
* $pluginConfigManager = new PluginConfigManager();
* $config = $pluginConfigManager->loadConfig();
* echo $config['yourConfigurationKey'];
*
* @return mixed[]
*
* @throws InvalidPluginRootPathException
* @throws JsonException
*/
public function loadConfig(): array
{
if (! $this->config) {
$this->updateConfig();
}
return $this->config;
}
/**
* Refreshes the cached plugin configuration held in this class.
*
* Example usage:
*
* $pluginConfigManager = new PluginConfigManager();
* $config = $pluginConfigManager->loadConfig();
*
* // ... long operation ...
* // ... long operation ...
* // ... long operation ...
*
* $pluginConfigManager->updateConfig();
* // config is now up to date
* $config = $pluginConfigManager->loadConfig();
*
* @throws InvalidPluginRootPathException
* @throws JsonException
*/
public function updateConfig(): void
{
$this->config = $this->getDataFromJson(self::CONFIG_JSON);
}
}