<?php declare(strict_types=1);
namespace BaumarktTheme;
use Shopware\Core\Framework\Plugin;
use Shopware\Storefront\Framework\ThemeInterface;
use BaumarktTheme\Service\CategoryCustomFieldsetService;
use BaumarktTheme\Service\ProductCustomFieldsetService;;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Plugin\Context\ActivateContext;
use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;
class BaumarktTheme extends Plugin implements ThemeInterface
{
public function install(InstallContext $installContext): void
{
parent::install($installContext);
$this->applyUpdates(
$installContext->getContext(),
null,
$installContext->getCurrentPluginVersion()
);
}
public function update(UpdateContext $updateContext): void
{
parent::update($updateContext);
$this->applyUpdates(
$updateContext->getContext(),
$updateContext->getCurrentPluginVersion(),
$updateContext->getUpdatePluginVersion()
);
}
public function uninstall(UninstallContext $uninstallContext): void
{
parent::uninstall($uninstallContext);
$customFieldSetService = new CategoryCustomFieldsetService(
$this->container->get('custom_field_set.repository'),
$uninstallContext->getContext()
);
$customFieldSetService->remove();
}
public function activate(ActivateContext $context): void
{
}
public function deactivate(DeactivateContext $context): void
{
}
public function getThemeConfigPath(): string
{
return 'theme.json';
}
private function applyUpdates(Context $context, $oldVersion = null, $newVersion = null)
{
$versionClosures = [
'0.1.1' => function () use ($context) {
$customFieldSetService = new CategoryCustomFieldsetService(
$this->container->get('custom_field_set.repository'),
$context
);
$customFieldSetService->remove();
$customFieldSetService->create();
return true;
},
'0.1.2' => function () use ($context) {
$productFieldSetService = new ProductCustomFieldsetService(
$this->container->get('custom_field_set.repository'),
$context
);
$productFieldSetService->remove();
$productFieldSetService->create();
return true;
},
'0.1.3' => function () use ($context) {
$customFieldSetService = new CategoryCustomFieldsetService(
$this->container->get('custom_field_set.repository'),
$context
);
$customFieldSetService->remove();
$customFieldSetService->create();
return true;
},
'0.1.5' => function () use ($context) {
$productFieldSetService = new ProductCustomFieldsetService(
$this->container->get('custom_field_set.repository'),
$context
);
$productFieldSetService->remove();
$productFieldSetService->create();
return true;
},
'0.1.6' => function () use ($context) {
$productFieldSetService = new ProductCustomFieldsetService(
$this->container->get('custom_field_set.repository'),
$context
);
$productFieldSetService->remove();
$productFieldSetService->create();
return true;
},
'0.1.7' => function () use ($context) {
$productFieldSetService = new ProductCustomFieldsetService(
$this->container->get('custom_field_set.repository'),
$context
);
$productFieldSetService->remove();
$productFieldSetService->create();
return true;
},
'0.1.8' => function () use ($context) {
$customFieldSetService = new CategoryCustomFieldsetService(
$this->container->get('custom_field_set.repository'),
$context
);
$customFieldSetService->remove();
$customFieldSetService->create();
return true;
},
'0.1.9' => function () use ($context) {
$customFieldSetService = new ProductCustomFieldsetService(
$this->container->get('custom_field_set.repository'),
$context
);
$customFieldSetService->remove();
$customFieldSetService->create();
return true;
},
];
foreach ($versionClosures as $version => $versionClosure) {
if ($oldVersion === null ||
(version_compare($oldVersion, $version, '<')
&& version_compare($version, $newVersion, '<='))
) {
if (!$versionClosure($this)) {
return false;
}
}
}
return true;
}
}