forked from Ubiquiti-App/UCRM-Plugin-SDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUcrmOptionsManager.php
111 lines (98 loc) · 3.19 KB
/
UcrmOptionsManager.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?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\Data\UcrmOptions;
use Ubnt\UcrmPluginSdk\Exception\InvalidPluginRootPathException;
use Ubnt\UcrmPluginSdk\Exception\JsonException;
/**
* This class can be used to retrieve automatically generated UCRM options from `ucrm.json` file.
*
* @see https://github.com/Ubiquiti-App/UCRM-plugins/blob/master/docs/file-structure.md#ucrmjson
*/
class UcrmOptionsManager extends AbstractOptionsManager
{
private const UCRM_JSON = 'ucrm.json';
/**
* @var UcrmOptions|null
*/
private $options;
/**
* 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) instance of UcrmOptions data class,
* which holds automatically generated UCRM options from `ucrm.json` file.
*
* @see https://github.com/Ubiquiti-App/UCRM-plugins/blob/master/docs/file-structure.md#ucrmjson
*
* Example usage:
*
* $ucrmOptionsManager = new UcrmOptionsManager();
* $options = $ucrmOptionsManager->loadOptions();
* echo $options->pluginPublicUrl;
*
* @throws InvalidPluginRootPathException
* @throws JsonException
*/
public function loadOptions(): UcrmOptions
{
if (! $this->options) {
$this->updateOptions();
}
return $this->options;
}
/**
* Refreshes the cached instance of UcrmOptions held in this class.
*
* Example usage:
*
* $ucrmOptionsManager = new UcrmOptionsManager();
* $options = $ucrmOptionsManager->loadOptions();
*
* // ... long operation ...
* // ... long operation ...
* // ... long operation ...
*
* $ucrmOptionsManager->updateOptions();
* // options are now up to date
* $options = $ucrmOptionsManager->loadOptions();
*
* @throws InvalidPluginRootPathException
* @throws JsonException
*/
public function updateOptions(): void
{
$this->options = $this->getOptions();
}
/**
* @throws InvalidPluginRootPathException
* @throws JsonException
*/
private function getOptions(): UcrmOptions
{
$options = new UcrmOptions();
$reflectionClass = new \ReflectionClass($options);
$data = $this->getDataFromJson(self::UCRM_JSON);
foreach ($reflectionClass->getProperties() as $reflectionProperty) {
if (array_key_exists($reflectionProperty->getName(), $data)) {
$reflectionProperty->setValue($options, $data[$reflectionProperty->getName()]);
}
}
return $options;
}
}