<?php declare(strict_types=1);
namespace Shopware\Commercial;
use Shopware\Commercial\CustomPricing\CustomPricing;
use Shopware\Commercial\FlowBuilder\FlowSharing\FlowSharing;
use Shopware\Commercial\Licensing\Exception\LicenseExpiredException;
use Shopware\Commercial\Licensing\License;
use Shopware\Commercial\Licensing\LicenseUpdater;
use Shopware\Commercial\MultiWarehouse\MultiWarehouse;
use Shopware\Commercial\MultiWarehouse\MultiWarehouseUninstallHandler;
use Shopware\Commercial\RuleBuilderPreview\RuleBuilderPreview;
use Shopware\Commercial\System\UninstallHandler;
use Shopware\Commercial\TextGenerator\TextGenerator;
use Shopware\Core\Framework\Parameter\AdditionalBundleParameters;
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\UpdateContext;
use Shopware\Core\Framework\Store\Authentication\StoreRequestOptionsProvider;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Loader\DelegatingLoader;
use Symfony\Component\Config\Loader\LoaderResolver;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
class SwagCommercial extends Plugin
{
public function boot(): void
{
$systemConfigService = $this->container->get(SystemConfigService::class);
try {
License::set($systemConfigService);
} catch (LicenseExpiredException $e) {
// The given license key is expired. Renew now
$this->container->get(LicenseUpdater::class)->sync();
License::set($systemConfigService);
}
}
public function install(InstallContext $installContext): void
{
try {
$this->getLicenseUpdater()->sync();
} catch (\Throwable $e) {
// installation has to work always
}
}
public function uninstall(UninstallContext $uninstallContext): void
{
foreach ($this->getUninstallHandlers() as $handler) {
$handler->uninstall($this->container, $uninstallContext);
}
}
public function update(UpdateContext $updateContext): void
{
try {
$this->getLicenseUpdater()->sync();
} catch (\Throwable $e) {
// update has to work always
}
}
public function build(ContainerBuilder $container): void
{
parent::build($container);
$fileLocator = new FileLocator($this->getPath());
$loaderResolver = new LoaderResolver([
new XmlFileLoader($container, $fileLocator),
new YamlFileLoader($container, $fileLocator),
new PhpFileLoader($container, $fileLocator),
]);
$delegatingLoader = new DelegatingLoader($loaderResolver);
$delegatingLoader->load(__DIR__ . '/Licensing/DependencyInjection/services.xml');
}
public function getAdditionalBundles(AdditionalBundleParameters $parameters): array
{
return [
new CustomPricing(),
new RuleBuilderPreview(),
new FlowSharing(),
new MultiWarehouse(),
new TextGenerator(),
];
}
/**
* @return array<UninstallHandler>
*/
protected function getUninstallHandlers(): array
{
return [
new MultiWarehouseUninstallHandler(),
];
}
private function getLicenseUpdater(): LicenseUpdater
{
return new LicenseUpdater(
$this->container->get('shopware.store_client'),
$this->container->get(StoreRequestOptionsProvider::class),
$this->container->get(SystemConfigService::class)
);
}
}