. */ 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\PluginInterface; use SP\Domain\Plugin\Ports\PluginLoaderInterface; use SP\Domain\Plugin\Ports\PluginManagerInterface; use SP\Infrastructure\Common\Repositories\NoSuchItemException; use function SP\__; /** * Class PluginLoader */ final class PluginLoader extends Service implements PluginLoaderInterface { public function __construct(Application $application, private readonly PluginManagerInterface $pluginService) { parent::__construct($application); } /** * @throws ConstraintException * @throws QueryException */ public function loadFor(PluginInterface $plugin): void { try { $model = $this->pluginService->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()) ) ); } } }