| 1 |
<?php |
| 2 |
/** |
| 3 |
* Feature Registry class. |
| 4 |
* |
| 5 |
* @package WordPress\AI\Features |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace WordPress\AI\Features; |
| 11 |
|
| 12 |
use WordPress\AI\Contracts\Feature; |
| 13 |
|
| 14 |
// Exit if accessed directly. |
| 15 |
defined( 'ABSPATH' ) || exit; |
| 16 |
|
| 17 |
/** |
| 18 |
* Central registry for managing feature storage and retrieval. |
| 19 |
* |
| 20 |
* Provides a simple storage mechanism for registered features. |
| 21 |
* Feature initialization is handled by the Loader class. |
| 22 |
* |
| 23 |
* @since 0.6.0 |
| 24 |
*/ |
| 25 |
final class Registry { |
| 26 |
/** |
| 27 |
* Registered features. |
| 28 |
* |
| 29 |
* @since 0.6.0 |
| 30 |
* @var \WordPress\AI\Contracts\Feature[] |
| 31 |
*/ |
| 32 |
private array $features = array(); |
| 33 |
|
| 34 |
/** |
| 35 |
* Registers a feature. |
| 36 |
* |
| 37 |
* @since 0.6.0 |
| 38 |
* |
| 39 |
* @param \WordPress\AI\Contracts\Feature $feature Feature instance to register. |
| 40 |
* @return bool True if registered successfully, false if already exists or invalid. |
| 41 |
*/ |
| 42 |
public function register_feature( Feature $feature ): bool { |
| 43 |
$id = $feature::get_id(); |
| 44 |
|
| 45 |
if ( $this->has_feature( $id ) ) { |
| 46 |
return false; |
| 47 |
} |
| 48 |
|
| 49 |
$this->features[ $id ] = $feature; |
| 50 |
return true; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Gets a feature by ID. |
| 55 |
* |
| 56 |
* @since 0.6.0 |
| 57 |
* |
| 58 |
* @param string $id Feature identifier. |
| 59 |
* @return \WordPress\AI\Contracts\Feature|null Feature instance or null if not found. |
| 60 |
*/ |
| 61 |
public function get_feature( string $id ): ?Feature { |
| 62 |
return $this->features[ $id ] ?? null; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Gets all registered features. |
| 67 |
* |
| 68 |
* @since 0.6.0 |
| 69 |
* |
| 70 |
* @return \WordPress\AI\Contracts\Feature[] Array of feature instances keyed by feature ID. |
| 71 |
*/ |
| 72 |
public function get_all_features(): array { |
| 73 |
return $this->features; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Gets registered features by stability. |
| 78 |
* |
| 79 |
* @since 0.8.0 |
| 80 |
* |
| 81 |
* @param 'deprecated'|'experimental'|'stable' $stability The stability level to match. |
| 82 |
* @return \WordPress\AI\Contracts\Feature[] Array of matching feature instances keyed by feature ID. |
| 83 |
*/ |
| 84 |
public function get_features_by_stability( string $stability ): array { |
| 85 |
$features = array(); |
| 86 |
|
| 87 |
foreach ( $this->features as $feature_id => $feature ) { |
| 88 |
if ( $stability !== $feature->get_stability() ) { |
| 89 |
continue; |
| 90 |
} |
| 91 |
|
| 92 |
$features[ $feature_id ] = $feature; |
| 93 |
} |
| 94 |
|
| 95 |
return $features; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Checks if a feature is registered. |
| 100 |
* |
| 101 |
* @since 0.6.0 |
| 102 |
* |
| 103 |
* @param string $id Feature identifier. |
| 104 |
* @return bool True if registered, false otherwise. |
| 105 |
*/ |
| 106 |
public function has_feature( string $id ): bool { |
| 107 |
return isset( $this->features[ $id ] ); |
| 108 |
} |
| 109 |
} |
| 110 |
|