. */ namespace SP\Domain\Plugin\Services; use SP\Core\Application; use SP\Core\Events\Event; use SP\Core\Events\EventMessage; use SP\Domain\Common\Services\Service; use SP\Domain\Core\Exceptions\ConstraintException; use SP\Domain\Core\Exceptions\QueryException; use SP\Domain\Plugin\Ports\Plugin; use SP\Domain\Plugin\Ports\PluginLoaderService; use SP\Domain\Plugin\Ports\PluginManagerService; use SP\Infrastructure\Common\Repositories\NoSuchItemException; use function SP\__; /** * Class PluginLoader */ final class PluginLoader extends Service implements PluginLoaderService { public function __construct(Application $application, private readonly PluginManagerService $pluginManagerService) { parent::__construct($application); } /** * @throws ConstraintException * @throws QueryException */ public function loadFor(Plugin $plugin): void { try { $model = $this->pluginManagerService->getByName($plugin->getName()); } catch (NoSuchItemException $e) { $this->eventDispatcher->notify( 'plugin.load', new Event( $e, EventMessage::factory() ->addDetail(__('Plugin not registered'), $plugin->getName()) ) ); return; } if ($model->getEnabled()) { $this->eventDispatcher->attach($plugin); $this->eventDispatcher->notify( 'plugin.load', new Event( $this, EventMessage::factory() ->addDetail(__('Plugin loaded'), $plugin->getName()) ) ); } else { $this->eventDispatcher->notify( 'plugin.load', new Event( $this, EventMessage::factory() ->addDetail(__('Plugin not loaded (disabled)'), $plugin->getName()) ) ); } } }