-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathAttributeDriver.php
More file actions
163 lines (137 loc) · 5.65 KB
/
Copy pathAttributeDriver.php
File metadata and controls
163 lines (137 loc) · 5.65 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
/*
* This file is part of jbtronics/settings-bundle (https://github.com/jbtronics/settings-bundle).
*
* Copyright (c) 2024 Jan Böhmer
* Copyright (c) 2026 Sviatoslav Vysitskyi
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
declare(strict_types=1);
namespace Jbtronics\SettingsBundle\Metadata\Driver;
use Ergebnis\Classy\Construct;
use Ergebnis\Classy\Constructs;
use Jbtronics\SettingsBundle\Helper\PropertyAccessHelper;
use Jbtronics\SettingsBundle\Settings\EmbeddedSettings;
use Jbtronics\SettingsBundle\Settings\Settings;
use Jbtronics\SettingsBundle\Settings\SettingsParameter;
/**
* Metadata driver that reads settings metadata from PHP 8.1+ attributes.
* This is the default driver and provides backwards compatibility with the existing attribute-based configuration.
*
* The compile time registration for dependency injection is done in the JbtronicsSettingsExtension class
*/
final class AttributeDriver implements MetadataDriverInterface
{
/**
* @param string[] $searchPathes Directories to search for settings classes (e.g. src/Settings/)
*/
public function __construct(
private readonly array $searchPathes,
) {
}
public function isSettingsClass(string $className): bool
{
if (!class_exists($className)) {
return false;
}
$reflClass = new \ReflectionClass($className);
$attributes = $reflClass->getAttributes(Settings::class);
return count($attributes) > 0;
}
public function loadClassMetadata(string $className): ?Settings
{
$reflClass = new \ReflectionClass($className);
$attributes = $reflClass->getAttributes(Settings::class);
if (count($attributes) < 1) {
return null;
}
if (count($attributes) > 1) {
throw new \LogicException(sprintf('The class "%s" has more than one Settings attributes! Only one is allowed', $className));
}
return $attributes[0]->newInstance();
}
public function loadParameterMetadata(string $className): array
{
$parameters = [];
$reflProperties = PropertyAccessHelper::getProperties($className);
foreach ($reflProperties as $reflProperty) {
$attributes = $reflProperty->getAttributes(SettingsParameter::class);
if (count($attributes) < 1) {
continue;
}
if (count($attributes) > 1) {
throw new \LogicException(sprintf(
'The property "%s" of the class "%s" has more than one SettingsParameter attributes! Only one is allowed',
$reflProperty->getName(),
$className
));
}
$parameters[$reflProperty->getName()] = $attributes[0]->newInstance();
}
return $parameters;
}
public function loadEmbeddedMetadata(string $className): array
{
$embeddeds = [];
$reflProperties = PropertyAccessHelper::getProperties($className);
foreach ($reflProperties as $reflProperty) {
$attributes = $reflProperty->getAttributes(EmbeddedSettings::class);
if (count($attributes) < 1) {
continue;
}
if (count($attributes) > 1) {
throw new \LogicException(sprintf(
'The property "%s" of the class "%s" has more than one EmbeddedSettings attributes! Only one is allowed',
$reflProperty->getName(),
$className
));
}
$embeddeds[$reflProperty->getName()] = $attributes[0]->newInstance();
}
return $embeddeds;
}
public function getAllManagedClassNames(): array
{
$pathes = $this->searchPathes;
$classes = [];
foreach ($pathes as $path) {
//Skip non-existing directories
if (!is_dir($path)) {
continue;
}
//Find all PHP classes in the given directories
$constructs = Constructs::fromDirectory($path);
$names = array_map(static function (Construct $construct): string {
return $construct->name();
}, $constructs);
$classes = array_merge($classes, $names);
}
//Now filter out all classes, which donot have the #[Settings] attribute
$settings_classes = [];
foreach ($classes as $class) {
$reflClass = new \ReflectionClass($class);
$attributes = $reflClass->getAttributes(Settings::class);
if (count($attributes) > 0) {
$settings_classes[] = $class;
}
}
return $settings_classes;
}
}