Skip to content

Commit 05c4d6d

Browse files
author
Vladimir Mikhav
committed
feat: switch to config file from constants, update dependencies
1 parent d837fc4 commit 05c4d6d

11 files changed

+121
-118
lines changed

.editorconfig

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Editor configuration, see https://editorconfig.org
2+
root = true
3+
4+
[*]
5+
charset = utf-8
6+
indent_style = space
7+
indent_size = 2
8+
end_of_line = lf
9+
insert_final_newline = true
10+
trim_trailing_whitespace = true
11+
12+
[{*.php,*.json}]
13+
indent_size = 4
14+
15+
[*.md]
16+
max_line_length = off
17+
trim_trailing_whitespace = false
18+
19+
[*.bat]
20+
end_of_line = crlf

.gitattributes

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
/tests export-ignore
22
/.github export-ignore
3+
*.php eol=lf
4+
*.sh eol=lf

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
Includes the following classes:
44

5-
- Constants:
6-
- AppSettings;
75
- DTO:
86
- UpdateAppSettingDTO.
97
- Model:
@@ -19,7 +17,9 @@ Install:
1917
composer require onix-systems-php/hyperf-app-settings
2018
```
2119

22-
Publish database migrations:
20+
Publish config and database migrations:
2321
```shell script
2422
php bin/hyperf.php vendor:publish onix-systems-php/hyperf-app-settings
2523
```
24+
25+
Fill `app_settings` config with fields you want to store in database and validation rules for them, following existing examples.

composer.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,16 @@
2020
"require": {
2121
"php": ">=8.0",
2222
"ext-json": "*",
23-
"onix-systems-php/hyperf-core": "^1.0|dev-main",
24-
"onix-systems-php/hyperf-actions-log": "^1.0|dev-main",
23+
"onix-systems-php/hyperf-core": ">=1.0.5",
24+
"onix-systems-php/hyperf-actions-log": "^1.0",
2525
"hyperf/database": "^2.2|^3.0",
2626
"hyperf/cache": "~2.2.0|^3.0",
27-
"nesbot/carbon": "^2.0|^3.0",
2827
"hyperf/constants": "~2.2.0",
28+
"hyperf/contract": "^2.2|^3.0",
2929
"hyperf/db-connection": "^2.2|^3.0",
30+
"hyperf/translation": "^2.2|^3.0",
3031
"hyperf/validation": "^2.2|^3.0",
31-
"yzen.dev/plain-to-class": "^1.0"
32+
"hyperf/utils": "^2.2|^3.0"
3233
},
3334
"require-dev": {
3435
"friendsofphp/php-cs-fixer": "^2.2|^3.0",

publish/config/app_settings.php

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
return [
6+
'fields' => [
7+
'example_integer' => [
8+
'type' => 'integer',
9+
'category' => 'user',
10+
'name' => 'example_integer',
11+
'value' => [
12+
'data' => 1024,
13+
],
14+
],
15+
'example_string' => [
16+
'type' => 'string',
17+
'category' => 'user',
18+
'name' => 'example_string',
19+
'value' => [
20+
'data' => 'Enabled',
21+
],
22+
],
23+
'emails' => [
24+
'type' => 'array_of_emails',
25+
'category' => 'manager',
26+
'name' => 'emails',
27+
"value" => [],
28+
],
29+
],
30+
'types' => [
31+
'integer' => [
32+
'value.data' => 'required|numeric|max:1024',
33+
],
34+
'string' => [
35+
'value.data' => 'required|string|max:100',
36+
],
37+
'array_of_emails' => [
38+
'value.*' => 'email',
39+
],
40+
],
41+
'categories' => [
42+
'manager',
43+
'user',
44+
],
45+
];

src/ConfigProvider.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,23 @@ public function __invoke(): array
2929
],
3030
'publish' => [
3131
[
32-
'id' => 'migration',
32+
'id' => 'migration_base',
3333
'description' => 'The addition for migration from onix-systems-php/hyperf-app-settings.',
3434
'source' => __DIR__ . '/../publish/migrations/2022_04_18_122005_create_app_settings_table.php',
3535
'destination' => BASE_PATH . '/migrations/2022_04_18_122005_create_app_settings_table.php',
3636
],
3737
[
38-
'id' => 'migration',
38+
'id' => 'migration_category',
3939
'description' => 'The addition for migration from onix-systems-php/hyperf-app-settings.',
4040
'source' => __DIR__ . '/../publish/migrations/2022_05_10_111104_add_category_field_to_app_settings_table.php',
4141
'destination' => BASE_PATH . '/migrations/2022_05_10_111104_add_category_field_to_app_settings_table.php',
4242
],
43+
[
44+
'id' => 'config',
45+
'description' => 'The config for onix-systems-php/hyperf-app-settings.',
46+
'source' => __DIR__ . '/../publish/config/app_settings.php',
47+
'destination' => BASE_PATH . '/config/autoload/app_settings.php',
48+
],
4349
],
4450
];
4551
}

src/Constants/AppSettings.php

-79
This file was deleted.

src/DTO/UpdateAppSettingDTO.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ class UpdateAppSettingDTO extends AbstractDTO
1313

1414
public string $name;
1515

16-
public $value;
16+
public array $value;
1717
}

src/Repository/AppSettingsRepository.php

+11-7
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33

44
namespace OnixSystemsPHP\HyperfAppSettings\Repository;
55

6-
use OnixSystemsPHP\HyperfAppSettings\Constants\AppSettings;
7-
use OnixSystemsPHP\HyperfAppSettings\Constants\Time;
8-
use OnixSystemsPHP\HyperfAppSettings\Model\AppSetting;
96
use Hyperf\Cache\Annotation\Cacheable;
10-
use OnixSystemsPHP\HyperfCore\Repository\AbstractRepository;
7+
use Hyperf\Contract\ConfigInterface;
118
use Hyperf\Database\Model\Builder;
9+
use Hyperf\Utils\ApplicationContext;
10+
use OnixSystemsPHP\HyperfAppSettings\Model\AppSetting;
11+
use OnixSystemsPHP\HyperfCore\Constants\Time;
12+
use OnixSystemsPHP\HyperfCore\Repository\AbstractRepository;
1213

1314
/**
1415
* @method AppSetting create(array $data)
@@ -22,13 +23,16 @@ class AppSettingsRepository extends AbstractRepository
2223
#[Cacheable(prefix: 'app:setting_list', ttl: Time::YEAR)]
2324
public function getSettingsList(): array
2425
{
26+
/** @var ConfigInterface $config */
27+
$config = ApplicationContext::getContainer()->get(ConfigInterface::class);
28+
$settingsList = $config->get('app_settings.fields');
2529
$settings = [];
2630
foreach (AppSetting::all()->all() as $setting) {
2731
$settings[$setting->name] = $setting;
2832
}
29-
foreach (AppSettings::SETTINGS_LIST as $setting) {
30-
if (!isset($settings[$setting])) {
31-
$settings[$setting] = new AppSetting(AppSettings::SETTINGS_DATA[$setting]);
33+
foreach ($settingsList as $name => $data) {
34+
if (!isset($settings[$name])) {
35+
$settings[$name] = (new AppSetting())->fill($data);
3236
}
3337
}
3438
return $settings;

src/Service/AppSettingsService.php

+12-11
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
namespace OnixSystemsPHP\HyperfAppSettings\Service;
55

6-
use OnixSystemsPHP\HyperfAppSettings\Constants\AppSettings;
7-
use OnixSystemsPHP\HyperfAppSettings\Constants\Time;
8-
use OnixSystemsPHP\HyperfAppSettings\Exception\BusinessException;
9-
use OnixSystemsPHP\HyperfAppSettings\Repository\AppSettingsRepository;
106
use Hyperf\Cache\Annotation\CacheEvict;
7+
use Hyperf\Contract\ConfigInterface;
8+
use OnixSystemsPHP\HyperfAppSettings\Repository\AppSettingsRepository;
9+
use OnixSystemsPHP\HyperfCore\Constants\Time;
10+
use OnixSystemsPHP\HyperfCore\Exception\BusinessException;
1111
use OnixSystemsPHP\HyperfCore\Service\Service;
1212

1313
#[Service]
@@ -16,6 +16,7 @@ class AppSettingsService
1616
private array $appSettings;
1717

1818
public function __construct(
19+
private ConfigInterface $config,
1920
private AppSettingsRepository $rAppSettings,
2021
) {
2122
$this->reload();
@@ -29,14 +30,14 @@ public function reload(): void
2930

3031
public function get(string $setting)
3132
{
32-
if (in_array($setting, AppSettings::SETTINGS_LIST)) {
33-
if (!isset($this->appSettings[$setting])) {
34-
$this->reload();
35-
}
36-
return $this->appSettings[$setting]->value;
37-
} else {
38-
throw new BusinessException(404, __('get_app_settings.wrong_type'));
33+
$settingsList = $this->config->get('app_settings.fields');
34+
if (!array_key_exists($setting, $settingsList)) {
35+
throw new BusinessException(404, __('exceptions.app_settings.get_wrong_type'));
36+
}
37+
if (!isset($this->appSettings[$setting])) {
38+
$this->reload();
3939
}
40+
return $this->appSettings[$setting]->value;
4041
}
4142

4243
public function list(?array $categoryFilter = null): array

src/Service/UpdateAppSettingsService.php

+14-11
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33

44
namespace OnixSystemsPHP\HyperfAppSettings\Service;
55

6-
use OnixSystemsPHP\HyperfAppSettings\Constants\AppSettings;
7-
use OnixSystemsPHP\HyperfAppSettings\DTO\UpdateAppSettingDTO;
6+
use Hyperf\Contract\ConfigInterface;
7+
use Hyperf\DbConnection\Annotation\Transactional;
8+
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
89
use OnixSystemsPHP\HyperfActionsLog\Event\Action;
9-
use OnixSystemsPHP\HyperfCore\Exception\BusinessException;
10+
use OnixSystemsPHP\HyperfAppSettings\DTO\UpdateAppSettingDTO;
1011
use OnixSystemsPHP\HyperfAppSettings\Model\AppSetting;
1112
use OnixSystemsPHP\HyperfAppSettings\Repository\AppSettingsRepository;
13+
use OnixSystemsPHP\HyperfCore\Exception\BusinessException;
1214
use OnixSystemsPHP\HyperfCore\Service\Service;
13-
use Hyperf\DbConnection\Annotation\Transactional;
14-
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
1515
use Psr\EventDispatcher\EventDispatcherInterface;
1616

1717
#[Service]
@@ -21,6 +21,7 @@ class UpdateAppSettingsService
2121

2222
public function __construct(
2323
private ValidatorFactoryInterface $vf,
24+
private ConfigInterface $config,
2425
private AppSettingsRepository $rAppSettings,
2526
private AppSettingsService $appSettingsService,
2627
private EventDispatcherInterface $eventDispatcher,
@@ -48,15 +49,17 @@ public function run(UpdateAppSettingDTO $updateAppSettingDTO): ?AppSetting
4849

4950
private function validate(UpdateAppSettingDTO $userData): array
5051
{
51-
if (!isset(AppSettings::SETTINGS_DATA[$userData->name])) {
52-
throw new BusinessException(422, __('exceptions.update_app_settings.wrong_type'));
52+
$settingsList = $this->config->get('app_settings.fields');
53+
$settingsTypes = $this->config->get('app_settings.types');
54+
if (!array_key_exists($userData->name, $settingsList)) {
55+
throw new BusinessException(422, __('exceptions.app_settings.update_wrong_type'));
5356
}
54-
$type = AppSettings::SETTINGS_DATA[$userData->name]['type'];
55-
$category = AppSettings::SETTINGS_DATA[$userData->name]['category'];
57+
$type = $settingsList[$userData->name]['type'];
58+
$category = $settingsList[$userData->name]['category'];
5659
$result = $this->vf
5760
->make($userData->toArray(), array_merge([
58-
'name' => 'in:' . implode(',', AppSettings::SETTINGS_LIST),
59-
], AppSettings::TYPES_VALIDATION[$type]))
61+
'name' => 'in:' . implode(',', array_keys($settingsList)),
62+
], $settingsTypes[$type]))
6063
->validate();
6164
$result['type'] = $type;
6265
$result['category'] = $category;

0 commit comments

Comments
 (0)