1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventEspresso\core\services\graphql; |
4
|
|
|
|
5
|
|
|
use EventEspresso\core\services\collections\CollectionDetailsException; |
6
|
|
|
use EventEspresso\core\services\collections\CollectionLoaderException; |
7
|
|
|
use EventEspresso\core\services\graphql\enums\EnumCollection; |
8
|
|
|
use EventEspresso\core\services\graphql\enums\EnumInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class EnumsManager |
12
|
|
|
* Loads and registers custom GraphQL Enums and Fields |
13
|
|
|
* |
14
|
|
|
* @package EventEspresso\core\services\graphql |
15
|
|
|
* @author Manzoor Wani |
16
|
|
|
* @since $VID:$ |
17
|
|
|
*/ |
18
|
|
|
class EnumsManager |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var EnumCollection|EnumInterface[] $enums |
23
|
|
|
*/ |
24
|
|
|
private $enums; |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* EnumsManager constructor. |
29
|
|
|
* |
30
|
|
|
* @param EnumCollection|EnumInterface[] $enums |
31
|
|
|
*/ |
32
|
|
|
public function __construct(EnumCollection $enums) |
33
|
|
|
{ |
34
|
|
|
$this->enums = $enums; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @throws CollectionDetailsException |
40
|
|
|
* @throws CollectionLoaderException |
41
|
|
|
* @since $VID:$ |
42
|
|
|
*/ |
43
|
|
|
public function init() |
44
|
|
|
{ |
45
|
|
|
$this->enums->loadEnums(); |
46
|
|
|
add_action('graphql_register_types', [$this, 'configureEnums'], 8); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @since $VID:$ |
52
|
|
|
*/ |
53
|
|
|
public function configureEnums() |
54
|
|
|
{ |
55
|
|
|
// loop through the collection of enums and register their fields |
56
|
|
|
foreach ($this->enums as $enum) { |
57
|
|
|
$this->registerEnum($enum); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param EnumInterface $enum |
64
|
|
|
* @since $VID:$ |
65
|
|
|
*/ |
66
|
|
|
public function registerEnum(EnumInterface $enum) |
67
|
|
|
{ |
68
|
|
|
// Register the enum type. |
69
|
|
|
register_graphql_enum_type( |
70
|
|
|
$enum->name(), |
71
|
|
|
[ |
72
|
|
|
'description' => $enum->description(), |
73
|
|
|
'values' => $enum->values(), |
74
|
|
|
] |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|