. */ namespace SP\Plugin; use Defuse\Crypto\Exception\CryptoException; use SP\Domain\Common\Services\ServiceException; use SP\Domain\Core\Exceptions\ConstraintException; use SP\Domain\Core\Exceptions\NoSuchPropertyException; use SP\Domain\Core\Exceptions\QueryException; use SP\Domain\Plugin\Ports\PluginCompatilityInterface; use SP\Domain\Plugin\Ports\PluginInterface; use SP\Domain\Plugin\Ports\PluginLoaderInterface; use SP\Domain\Plugin\Ports\PluginOperationInterface; use SP\Infrastructure\Common\Repositories\NoSuchItemException; /** * Class PluginBase */ abstract class PluginBase implements PluginInterface { protected ?string $base = null; protected ?string $themeDir = null; protected mixed $data; /** * @throws ConstraintException * @throws NoSuchItemException * @throws QueryException */ public function __construct( protected readonly PluginOperationInterface $pluginOperation, private readonly PluginCompatilityInterface $pluginCompatilityService, private readonly PluginLoaderInterface $pluginLoadService ) { $this->load(); } /** * @throws ConstraintException * @throws NoSuchItemException * @throws QueryException */ private function load(): void { if ($this->pluginCompatilityService->checkFor($this)) { $this->pluginLoadService->loadFor($this); } } public function getThemeDir(): ?string { return $this->themeDir; } public function getData(): mixed { return $this->data; } /** * @param int $id * @param mixed $data * * @throws CryptoException * @throws ConstraintException * @throws NoSuchPropertyException * @throws QueryException * @throws ServiceException */ final public function saveData(int $id, mixed $data): void { if ($this->data === null) { $this->pluginOperation->create($id, $data); } else { $this->pluginOperation->update($id, $data); } $this->data = $data; } protected function setLocales(): void { $locales = sprintf('%s%slocales', $this->getBase(), DIRECTORY_SEPARATOR); $name = strtolower($this->getName()); bindtextdomain($name, $locales); bind_textdomain_codeset($name, 'UTF-8'); } public function getBase(): ?string { return $this->base; } }