Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace core;
18
 
19
use ReflectionAttribute;
20
 
21
/**
22
 * Helper for loading attributes.
23
 *
24
 * @package    core
25
 * @copyright  2024 Andrew Lyons <andrew@nicols.co.uk>
26
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 */
28
class attribute_helper {
29
 
30
    /**
31
     * Get an instance of an attribute from a reference.
32
     *
33
     * The reference can be:
34
     * - a string, in which case it will be checked for a function, class, method, property, constant, or enum.
35
     * - an array
36
     * - an instantiated object, in which case the object will be checked for a class, method, property, or constant.
37
     *
38
     * @param array|string|object $reference A reference of where to find the attribute
39
     * @param null|string $attributename The name of the attribute to find
40
     * @param int $attributeflags The flags to use when finding the attribute
41
     * @return ?object
42
     */
43
    public static function instance(
44
        array|string|object $reference,
45
        ?string $attributename = null,
46
        int $attributeflags = ReflectionAttribute::IS_INSTANCEOF,
47
    ): ?object {
48
        return self::one_from($reference, $attributename, $attributeflags)?->newInstance();
49
    }
50
 
51
    /**
52
     * Get all instance of an attribute from a reference.
53
     *
54
     * The reference can be:
55
     * - a string, in which case it will be checked for a function, class, method, property, constant, or enum.
56
     * - an array
57
     * - an instantiated object, in which case the object will be checked for a class, method, property, or constant.
58
     *
59
     * @param array|string|object $reference A reference of where to find the attribute
60
     * @param null|string $attributename The name of the attribute to find
61
     * @param int $attributeflags The flags to use when finding the attribute
62
     * @return ?object[]
63
     */
64
    public static function instances(
65
        array|string|object $reference,
66
        ?string $attributename = null,
67
        int $attributeflags = ReflectionAttribute::IS_INSTANCEOF,
68
    ): ?array {
69
        if ($attributes = self::from($reference, $attributename, $attributeflags)) {
70
            return array_map(fn ($attribute) => $attribute->newInstance(), $attributes);
71
        }
72
 
73
        return null;
74
    }
75
 
76
    /**
77
     * Get one attribute from a reference.
78
     *
79
     * The reference can be:
80
     * - a string, in which case it will be checked for a function, class, method, property, constant, or enum.
81
     * - an array
82
     * - an instantiated object, in which case the object will be checked for a class, method, property, or constant.
83
     *
84
     * @param array|string|object $reference A reference of where to find the attribute
85
     * @param null|string $attributename The name of the attribute to find
86
     * @param int $attributeflags The flags to use when finding the attribute
87
     * @return \ReflectionAttribute|null
88
     */
89
    public static function one_from(
90
        array|string|object $reference,
91
        ?string $attributename = null,
92
        int $attributeflags = ReflectionAttribute::IS_INSTANCEOF,
93
    ): ?\ReflectionAttribute {
94
        $attributes = self::from($reference, $attributename, $attributeflags);
95
 
96
        if ($attributes && count($attributes) > 1) {
97
            throw new \coding_exception('More than one attribute found');
98
        }
99
 
100
        return $attributes ? $attributes[0] : null;
101
    }
102
 
103
    /**
104
     * Get the attribute from a reference.
105
     *
106
     * The reference can be:
107
     * - a string, in which case it will be checked for a function, class, method, property, constant, or enum.
108
     * - an array
109
     * - an instantiated object, in which case the object will be checked for a class, method, property, or constant.
110
     *
111
     * @param array|string|object $reference A reference of where to find the attribute
112
     * @param null|string $attributename The name of the attribute to find
113
     * @param int $attributeflags The flags to use when finding the attribute
114
     * @return \ReflectionAttribute[]|null
115
     */
116
    public static function from(
117
        array|string|object $reference,
118
        ?string $attributename = null,
119
        int $attributeflags = ReflectionAttribute::IS_INSTANCEOF,
120
    ): ?array {
121
        if (is_string($reference)) {
122
            if (str_contains($reference, '::')) {
123
                // The reference is a string but it looks to be in the format `object::item`.
124
                return self::from(explode('::', $reference), $attributename, $attributeflags);;
125
            }
126
 
127
            if (class_exists($reference)) {
128
                // The reference looks to be a class name.
129
                return self::from([$reference], $attributename, $attributeflags);
130
            }
131
 
132
            if (function_exists($reference)) {
133
                // The reference looks to be a global function.
134
                $ref = new \ReflectionFunction($reference);
135
                return $ref->getAttributes(
136
                    name: $attributename,
137
                    flags: $attributeflags,
138
                );
139
            }
140
 
141
            return null;
142
        }
143
 
144
        if (is_object($reference)) {
145
            // The reference is an object. Normalise and check again.
146
            return self::from([$reference], $attributename, $attributeflags);
147
        }
148
 
149
        if (is_array($reference) && count($reference)) {
150
            if (is_object($reference[0])) {
151
                // The first array key is an instance of a class, enum, etc.
152
                $rc = new \ReflectionObject($reference[0]);
153
 
154
                if ($rc->isEnum() && $reference[0]->name) {
155
                    // Enums can be passed via ::from([enum::NAME]).
156
                    // In this case they will have a 'name', which must exist.
157
                    return self::from_reflected_object(
158
                        rc: $rc,
159
                        referenceproperty: $reference[0]->name,
160
                        attributename: $attributename,
161
                        flags: $attributeflags,
162
                    );
163
                }
164
 
165
                // The object is an instance of a class, or similar.
166
                // That means that, if provided, the second array key is the name of the property, constant, method, etc.
167
                return self::from_reflected_object(
168
                    rc: $rc,
169
                    referenceproperty: $reference[1] ?? null,
170
                    attributename: $attributename,
171
                    flags: $attributeflags,
172
                );
173
            }
174
 
175
            if (is_string($reference[0]) && class_exists($reference[0])) {
176
                // The first array key is a class name.
177
                // That means that, if provided, the second array key is the name of the property, constant, method, etc.
178
                $rc = new \ReflectionClass($reference[0]);
179
                return self::from_reflected_object(
180
                    rc: $rc,
181
                    referenceproperty: $reference[1] ?? null,
182
                    attributename: $attributename,
183
                    flags: $attributeflags,
184
                );
185
            }
186
 
187
            // The reference is an array, but it's not an object or a class that currently exists.
188
            return null;
189
        }
190
    }
191
 
192
    /**
193
     * Fetch an attribute from a reflected object.
194
     *
195
     * @param \ReflectionClass $rc The reflected object
196
     * @param null|string $referenceproperty The name of the thing to find attributes on
197
     * @param null|string $attributename The name of the attribute to find
198
     * @param int $attributeflags The flags to use when finding the attribute
199
     * @return \ReflectionAttribute[]|null
200
     */
201
    protected static function from_reflected_object(
202
        \ReflectionClass $rc,
203
        ?string $referenceproperty,
204
        ?string $attributename = null,
205
        int $flags = 0,
206
    ): ?array {
207
        if ($referenceproperty === null) {
208
            // No name specified - may be the whole class..
209
            return $rc->getAttributes(
210
                name: $attributename,
211
                flags: $flags,
212
            );
213
        }
214
 
215
        if ($rc->hasConstant($referenceproperty)) {
216
            // This class has a constant with the specified name.
217
            // Note: This also applies to enums.
218
            $ref = $rc->getReflectionConstant($referenceproperty);
219
            return $ref->getAttributes(
220
                name: $attributename,
221
                flags: $flags,
222
            );
223
        }
224
 
225
        if ($rc->hasMethod($referenceproperty)) {
226
            // This class has a method with the specified name.
227
            $ref = $rc->getMethod($referenceproperty);
228
            return $ref->getAttributes(
229
                name: $attributename,
230
                flags: $flags,
231
            );
232
        }
233
 
234
        if ($rc->hasProperty($referenceproperty)) {
235
            // This class has a property with the specified name.
236
            $ref = $rc->getProperty($referenceproperty);
237
            return $ref->getAttributes(
238
                name: $attributename,
239
                flags: $flags,
240
            );
241
        }
242
 
243
        return null;
244
    }
245
}