container = new Container(); } /** * Returns the plugin instance. * * @since 0.1.0 * * @return Plugin */ public static function instance(): self { if ( null === self::$instance ) { self::$instance = new self(); } return self::$instance; } /** * Returns the service container. * * @since 0.1.0 * * @return Container */ public function container(): Container { return $this->container; } /** * Registers services and hooks them into WordPress. Runs once on `plugins_loaded`. * * @since 0.1.0 * * @return void */ public function boot(): void { if ( $this->booted ) { return; } $this->booted = true; $this->register_services(); foreach ( $this->hookable_services() as $id ) { $service = $this->container->get( $id ); if ( $service instanceof Hookable ) { $service->register(); } } /** * Fires once the plugin has finished booting. * * @since 0.1.0 * * @param Plugin $plugin Plugin instance. */ do_action( 'interviewnea_loaded', $this ); } /** * Defines every service the plugin knows about. * * @return void */ private function register_services(): void { $c = $this->container; // Infrastructure. $c->singleton( I18n::class, static function () { return new I18n(); } ); $c->singleton( Settings::class, static function () { return new Settings(); } ); $c->singleton( Capabilities::class, static function () { return new Capabilities(); } ); $c->singleton( Clock::class, static function () { return new SystemClock(); } ); $c->singleton( RateLimiter::class, static function () { return new RateLimiter(); } ); $c->singleton( PortalUrl::class, static function ( Container $c ) { return new PortalUrl( $c->get( Settings::class ) ); } ); // Database and lifecycle. $c->singleton( Migrator::class, static function () { return new Migrator( $GLOBALS['wpdb'] ); } ); $c->singleton( Installer::class, static function ( Container $c ) { return new Installer( $c->get( Migrator::class ), $c->get( Capabilities::class ) ); } ); $c->singleton( Upgrader::class, static function ( Container $c ) { return new Upgrader( $c->get( Installer::class ) ); } ); // Repositories. $c->singleton( SessionRepository::class, static function () { return new SessionRepository( $GLOBALS['wpdb'] ); } ); $c->singleton( CandidateRepository::class, static function () { return new CandidateRepository( $GLOBALS['wpdb'] ); } ); $c->singleton( EventRepository::class, static function () { return new EventRepository( $GLOBALS['wpdb'] ); } ); $c->singleton( EmailLogRepository::class, static function () { return new EmailLogRepository( $GLOBALS['wpdb'] ); } ); // Domain. $c->singleton( QueueService::class, static function ( Container $c ) { return new QueueService( $GLOBALS['wpdb'], $c->get( SessionRepository::class ), $c->get( CandidateRepository::class ), $c->get( EventRepository::class ), $c->get( Settings::class ), $c->get( Clock::class ) ); } ); $c->singleton( EmailComposer::class, static function ( Container $c ) { return new EmailComposer( $c->get( Settings::class ), $c->get( PortalUrl::class ) ); } ); $c->singleton( ReminderService::class, static function ( Container $c ) { return new ReminderService( $c->get( SessionRepository::class ), $c->get( CandidateRepository::class ), $c->get( EmailLogRepository::class ), $c->get( EmailComposer::class ), $c->get( Settings::class ), $c->get( Clock::class ) ); } ); $c->singleton( SessionService::class, static function ( Container $c ) { return new SessionService( $c->get( SessionRepository::class ), $c->get( CandidateRepository::class ), $c->get( EventRepository::class ), $c->get( ReminderService::class ), $c->get( PortalUrl::class ) ); } ); $c->singleton( CandidateService::class, static function ( Container $c ) { return new CandidateService( $c->get( SessionRepository::class ), $c->get( CandidateRepository::class ), $c->get( QueueService::class ), $c->get( ReminderService::class ) ); } ); $c->singleton( CsvImporter::class, static function ( Container $c ) { return new CsvImporter( $c->get( SessionRepository::class ), $c->get( CandidateService::class ) ); } ); $c->singleton( LogRetention::class, static function ( Container $c ) { return new LogRetention( $c->get( EmailLogRepository::class ), $c->get( Settings::class ) ); } ); $c->singleton( Privacy::class, static function ( Container $c ) { return new Privacy( $c->get( CandidateRepository::class ), $c->get( SessionRepository::class ), $c->get( EmailLogRepository::class ), $c->get( EventRepository::class ), $c->get( ReminderService::class ) ); } ); // REST. $c->singleton( PublicController::class, static function ( Container $c ) { return new PublicController( $c->get( QueueService::class ), $c->get( RateLimiter::class ) ); } ); $c->singleton( SessionsController::class, static function ( Container $c ) { return new SessionsController( $c->get( SessionRepository::class ), $c->get( SessionService::class ), $c->get( QueueService::class ) ); } ); $c->singleton( CandidatesController::class, static function ( Container $c ) { return new CandidatesController( $c->get( SessionRepository::class ), $c->get( CandidateRepository::class ), $c->get( CandidateService::class ), $c->get( QueueService::class ), $c->get( ReminderService::class ), $c->get( CsvImporter::class ) ); } ); $c->singleton( LogsController::class, static function ( Container $c ) { return new LogsController( $c->get( SessionRepository::class ), $c->get( EmailLogRepository::class ), $c->get( EventRepository::class ), $c->get( ReminderService::class ) ); } ); $c->singleton( SettingsController::class, static function ( Container $c ) { return new SettingsController( $c->get( Settings::class ), $c->get( ReminderService::class ) ); } ); $c->singleton( Router::class, static function ( Container $c ) { return new Router( $c, array( PublicController::class, SessionsController::class, CandidatesController::class, LogsController::class, SettingsController::class, ) ); } ); // Admin and front end. $c->singleton( Assets::class, static function ( Container $c ) { return new Assets( $c->get( I18n::class ), $c->get( Settings::class ) ); } ); $c->singleton( AppShell::class, static function () { return new AppShell(); } ); $c->singleton( Menu::class, static function ( Container $c ) { return new Menu( $c->get( AppShell::class ) ); } ); $c->singleton( CsvExport::class, static function ( Container $c ) { return new CsvExport( $c->get( SessionRepository::class ), $c->get( CandidateRepository::class ) ); } ); $c->singleton( WaitingRoom::class, static function ( Container $c ) { return new WaitingRoom( $c->get( SessionRepository::class ), $c->get( Settings::class ), $c->get( Assets::class ) ); } ); } /** * Services that hook into WordPress, in registration order. * * @return string[] */ private function hookable_services(): array { return array( Upgrader::class, Router::class, ReminderService::class, LogRetention::class, Privacy::class, Assets::class, Menu::class, CsvExport::class, WaitingRoom::class, ); } }