| 1 |
<?php |
| 2 |
|
| 3 |
namespace Code_Snippets; |
| 4 |
|
| 5 |
class Snippet_Handler_Registry { |
| 6 |
/** |
| 7 |
* @var Snippet_Type_Handler_Interface[] |
| 8 |
*/ |
| 9 |
private array $handlers = []; |
| 10 |
|
| 11 |
/** |
| 12 |
* Constructor |
| 13 |
* |
| 14 |
* @param Snippet_Type_Handler_Interface[] $handlers |
| 15 |
*/ |
| 16 |
public function __construct( array $handlers ) { |
| 17 |
foreach ( $handlers as $type => $handler ) { |
| 18 |
$this->register_handler( $type, $handler ); |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Registers a handler for a snippet type. |
| 24 |
* |
| 25 |
* @param string $type |
| 26 |
* @param Snippet_Type_Handler_Interface $handler |
| 27 |
* @return void |
| 28 |
*/ |
| 29 |
public function register_handler( string $type, Snippet_Type_Handler_Interface $handler ): void { |
| 30 |
$this->handlers[ $type ] = $handler; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Gets the handler for a snippet type. |
| 35 |
* |
| 36 |
* @param string $type |
| 37 |
* |
| 38 |
* @return Snippet_Type_Handler_Interface|null |
| 39 |
*/ |
| 40 |
public function get_handler( string $type ): ?Snippet_Type_Handler_Interface { |
| 41 |
if ( ! isset( $this->handlers[ $type ] ) ) { |
| 42 |
return null; |
| 43 |
} |
| 44 |
|
| 45 |
return $this->handlers[ $type ]; |
| 46 |
} |
| 47 |
} |
| 48 |
|