| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\Mcp\Registry; |
| 4 |
|
| 5 |
use Elementor\Modules\Mcp\Abilities\Abstract_Ability; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; |
| 9 |
} |
| 10 |
|
| 11 |
class Ability_Registry { |
| 12 |
|
| 13 |
/** @var array<string, Abstract_Ability> */ |
| 14 |
private array $abilities = []; |
| 15 |
|
| 16 |
public function add( Abstract_Ability $ability ): void { |
| 17 |
$this->abilities[ $ability->get_id() ] = $ability; |
| 18 |
} |
| 19 |
|
| 20 |
/** @return Abstract_Ability[] */ |
| 21 |
public function all(): array { |
| 22 |
return array_values( $this->abilities ); |
| 23 |
} |
| 24 |
|
| 25 |
/** @return Abstract_Ability[] */ |
| 26 |
public function tools(): array { |
| 27 |
return $this->filter_by_kind( Abstract_Ability::KIND_TOOL ); |
| 28 |
} |
| 29 |
|
| 30 |
/** @return Abstract_Ability[] */ |
| 31 |
public function resources(): array { |
| 32 |
return $this->filter_by_kind( Abstract_Ability::KIND_RESOURCE ); |
| 33 |
} |
| 34 |
|
| 35 |
public function find_by_id( string $id ): ?Abstract_Ability { |
| 36 |
return $this->abilities[ $id ] ?? null; |
| 37 |
} |
| 38 |
|
| 39 |
public function find_by_proxy_slug( string $slug ): ?Abstract_Ability { |
| 40 |
foreach ( $this->abilities as $ability ) { |
| 41 |
if ( ! $ability->is_exposed_via_proxy() ) { |
| 42 |
continue; |
| 43 |
} |
| 44 |
|
| 45 |
if ( Abstract_Ability::KIND_TOOL === $ability->get_kind() && $ability->get_proxy_slug() === $slug ) { |
| 46 |
return $ability; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
return null; |
| 51 |
} |
| 52 |
|
| 53 |
public function find_resource_by_uri( string $uri ): ?Abstract_Ability { |
| 54 |
foreach ( $this->abilities as $ability ) { |
| 55 |
if ( Abstract_Ability::KIND_RESOURCE !== $ability->get_kind() ) { |
| 56 |
continue; |
| 57 |
} |
| 58 |
|
| 59 |
if ( $ability->get_uri() === $uri ) { |
| 60 |
return $ability; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
return null; |
| 65 |
} |
| 66 |
|
| 67 |
/** @return Abstract_Ability[] */ |
| 68 |
private function filter_by_kind( string $kind ): array { |
| 69 |
return array_values( array_filter( |
| 70 |
$this->abilities, |
| 71 |
fn( Abstract_Ability $ability ) => $kind === $ability->get_kind() |
| 72 |
) ); |
| 73 |
} |
| 74 |
} |
| 75 |
|