-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathExperiment_Loader.php
More file actions
230 lines (208 loc) · 6.19 KB
/
Experiment_Loader.php
File metadata and controls
230 lines (208 loc) · 6.19 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
/**
* Experiment Loader class.
*
* @package WordPress\AI
*/
declare( strict_types=1 );
namespace WordPress\AI;
use Throwable;
use WordPress\AI\Contracts\Experiment;
use WordPress\AI\Exception\Invalid_Experiment_Exception;
use WordPress\AI\Exception\Invalid_Experiment_Metadata_Exception;
/**
* Orchestrates experiment initialization and registration.
*
* This class is responsible for loading and initializing experiments from the registry.
* It decouples the initialization logic from the registry itself.
*
* @since 0.1.0
*/
final class Experiment_Loader {
/**
* Experiment registry instance.
*
* @since 0.1.0
* @var \WordPress\AI\Experiment_Registry
*/
private \WordPress\AI\Experiment_Registry $registry;
/**
* Whether experiments have been initialized.
*
* @since 0.1.0
* @var bool
*/
private bool $initialized = false;
/**
* Constructor.
*
* @since 0.1.0
*
* @param \WordPress\AI\Experiment_Registry $registry The experiment registry instance.
*/
public function __construct( Experiment_Registry $registry ) {
$this->registry = $registry;
}
/**
* Registers default experiments.
*
* This is where built-in experiments are registered. Third-party experiments
* should use the 'ai_experiments_register_experiments' action hook.
*
* @since 0.1.0
*
* @throws \WordPress\AI\Exception\Invalid_Experiment_Exception If an experiment does not implement the Experiment interface.
*/
public function register_default_experiments(): void {
$experiments = $this->get_default_experiments();
// Register all experiments with type validation.
foreach ( $experiments as $experiment ) {
// Skip invalid experiment instances.
if ( ! $experiment instanceof Experiment ) {
throw new Invalid_Experiment_Exception(
esc_html__( 'Attempted to register invalid experiment. Must implement Experiment interface.', 'ai' )
);
}
$this->registry->register_experiment( $experiment );
}
/**
* Allows registration of custom experiments.
*
* Third-party developers can use this action to register their own experiments.
*
* Example:
* ```php
* add_action( 'ai_experiments_register_experiments', function( $registry ) {
* $registry->register_experiment( new My_Custom_Experiment() );
* } );
* ```
*
* @since 0.1.0
*
* @param \WordPress\AI\Experiment_Registry $registry The experiment registry instance.
*/
do_action( 'ai_experiments_register_experiments', $this->registry );
}
/**
* Gets default built-in experiments.
*
* @since 0.1.0
*
* @return array<\WordPress\AI\Contracts\Experiment> Array of default experiment instances.
* @throws \WordPress\AI\Exception\Invalid_Experiment_Exception If an experiment class does not exist (caught internally).
*/
private function get_default_experiments(): array {
$experiment_classes = array(
\WordPress\AI\Experiments\Image_Generation\Image_Generation::class,
\WordPress\AI\Experiments\Title_Generation\Title_Generation::class,
\WordPress\AI\Experiments\Excerpt_Generation\Excerpt_Generation::class,
);
/**
* Filters the list of default experiment classes or instances.
*
* Allows developers to add, remove, or replace default experiments.
* Can accept both class names (strings) and experiment instances.
*
* @since 0.1.0
*
* @param array $experiment_classes Array of experiment class names or instances.
*/
$items = apply_filters( 'ai_experiments_default_experiment_classes', $experiment_classes );
$experiments = array();
foreach ( $items as $item ) {
try {
// Support both class names and pre-instantiated instances.
if ( is_string( $item ) && class_exists( $item ) ) {
/** @var class-string<\WordPress\AI\Contracts\Experiment> $item */
$experiments[] = new $item();
} elseif ( $item instanceof Experiment ) {
$experiments[] = $item;
} elseif ( is_string( $item ) ) {
// Class doesn't exist - throw exception.
throw new Invalid_Experiment_Exception(
sprintf(
/* translators: %s: Experiment class name. */
esc_html__( 'Experiment class "%s" does not exist.', 'ai' ),
esc_html( $item )
)
);
}
} catch ( Invalid_Experiment_Metadata_Exception $e ) {
// Skip experiments with invalid metadata.
_doing_it_wrong(
__METHOD__,
sprintf(
/* translators: 1: Experiment class name, 2: Error message. */
esc_html__( 'Failed to instantiate experiment "%1$s": %2$s', 'ai' ),
is_string( $item ) ? esc_html( $item ) : esc_html( (string) get_class( $item ) ),
esc_html( $e->getMessage() )
),
'0.1.0'
);
} catch ( Throwable $t ) {
// Skip experiments that fail to instantiate.
_doing_it_wrong(
__METHOD__,
sprintf(
/* translators: 1: Experiment class name, 2: Error message. */
esc_html__( 'Experiment instantiation error for "%1$s": %2$s', 'ai' ),
is_string( $item ) ? esc_html( $item ) : esc_html( (string) get_class( $item ) ),
esc_html( $t->getMessage() )
),
'0.1.0'
);
}
}
return $experiments;
}
/**
* Initializes all enabled experiments.
*
* Loops through all registered experiments and calls their init() method
* if they are enabled.
*
* @since 0.1.0
*/
public function initialize_experiments(): void {
if ( $this->initialized ) {
return;
}
/**
* Filters whether to enable AI experiments.
*
* @since 0.1.0
*
* @param bool $enabled Whether to enable AI experiments.
*/
$experiments_enabled = apply_filters( 'ai_experiments_enabled', true );
if ( ! $experiments_enabled ) {
$this->initialized = true;
return;
}
foreach ( $this->registry->get_all_experiments() as $experiment ) {
// Skip if experiment is disabled.
if ( ! $experiment->is_enabled() ) {
continue;
}
// Initialize the experiment.
$experiment->init();
}
/**
* Fires after all experiments have been initialized.
*
* @since 0.1.0
*/
do_action( 'ai_experiments_initialized' );
$this->initialized = true;
}
/**
* Checks if experiments have been initialized.
*
* @since 0.1.0
*
* @return bool True if initialized, false otherwise.
*/
public function is_initialized(): bool {
return $this->initialized;
}
}