| 1 |
<?php |
| 2 |
|
| 3 |
namespace Booktics\Calendar; |
| 4 |
|
| 5 |
use Booktics\Contracts\Hookable_Service_Contract; |
| 6 |
|
| 7 |
class Calendar_Registry implements Hookable_Service_Contract { |
| 8 |
private static $calendars = array(); |
| 9 |
|
| 10 |
/** |
| 11 |
* Register a calendar service |
| 12 |
* |
| 13 |
* @param string $class |
| 14 |
* |
| 15 |
* @return void |
| 16 |
*/ |
| 17 |
public static function register_calendar( string $class ): void { |
| 18 |
if ( is_subclass_of( $class, Calendar_Interface::class ) ) { |
| 19 |
$calendar_id = $class::get_id(); |
| 20 |
self::$calendars[ $calendar_id ] = $class; |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Get a calendar instance by ID |
| 26 |
* |
| 27 |
* @param string $calendar_id |
| 28 |
* |
| 29 |
* @return Calendar_Interface|null |
| 30 |
*/ |
| 31 |
public static function get_calendar( string $calendar_id ): ?Calendar_Interface { |
| 32 |
$class = self::$calendars[ $calendar_id ] ?? null; |
| 33 |
|
| 34 |
return $class ? new $class() : null; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Get all registered calendar IDs |
| 39 |
* |
| 40 |
* @return array |
| 41 |
*/ |
| 42 |
public static function get_available_calendars(): array { |
| 43 |
return array_keys( self::$calendars ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Register the service |
| 48 |
* |
| 49 |
* @return void |
| 50 |
*/ |
| 51 |
public function register() { |
| 52 |
// Register any core calendars here if needed |
| 53 |
} |
| 54 |
} |
| 55 |
|