<?php declare(strict_types=1);
namespace Svea\SveaPayment;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\Plugin\Context\ActivateContext;
use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
use Shopware\Core\Framework\Plugin\Util\PluginIdProvider;
use Svea\SveaPayment\Installer\CoreInstaller;
class SveaPayment extends Plugin {
public function postInstall(InstallContext $installContext): void {
$pluginId = $this->getPluginId($installContext->getContext());
$pluginVersion = $this->getPluginVersion();
$installer = new CoreInstaller($this->container, $installContext->getContext(), $pluginId, $pluginVersion);
$installer->install();
parent::install($installContext);
}
public function activate(ActivateContext $activateContext): void {
$pluginId = $this->getPluginId($activateContext->getContext());
$installer = new CoreInstaller($this->container, $activateContext->getContext(), $pluginId);
$installer->setPaymentMethodsIsActive(true, $activateContext->getContext());
parent::activate($activateContext);
}
public function uninstall(UninstallContext $uninstallContext): void {
$pluginId = $this->getPluginId($uninstallContext->getContext());
$installer = new CoreInstaller($this->container, $uninstallContext->getContext(), $pluginId);
$installer->uninstall();
if (!$uninstallContext->keepUserData()) {
$installer->removeConfig();
//$uninstaller->deleteMailSettings();
}
parent::uninstall($uninstallContext);
}
public function deactivate(DeactivateContext $deactivateContext): void {
$pluginId = $this->getPluginId($deactivateContext->getContext());
$installer = new CoreInstaller($this->container, $deactivateContext->getContext(), $pluginId);
$installer->setPaymentMethodsIsActive(false, $deactivateContext->getContext());
parent::deactivate($deactivateContext);
}
public function getPluginId($context) {
$pluginIdProvider = $this->container->get(PluginIdProvider::class);
return $pluginIdProvider->getPluginIdByBaseClass(get_class($this), $context);
}
public function getPluginVersion() {
$pluginDirectory = dirname($this->getPath());
$composerJsonPath = $pluginDirectory . '/composer.json';
if (!file_exists($composerJsonPath)) {
throw new \Exception('composer.json not found under: ' . $pluginDirectory);
}
$composerJson = file_get_contents($composerJsonPath);
$composerData = json_decode($composerJson, true);
if (isset($composerData['version'])) {
return $composerData['version'];
}
throw new \Exception('Plugin version not found in composer.json');
}
}