<?php
declare(strict_types=1);
namespace NetInventors\NetiNextEasyCoupon;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use NetInventors\NetiNextEasyCoupon\Components\RuleBasedContainerFile;
use NetInventors\NetiNextEasyCoupon\Components\RuleBasedContainerFileLoader;
use NetInventors\NetiNextEasyCoupon\Components\Setup;
use NetInventors\NetiNextEasyCoupon\Service\CheckService;
use NetInventors\NetiNextEasyCoupon\Service\VoucherCodeGenerator\ValidatorPass as VoucherCodeGeneratorValidatorPass;
use NetInventors\NetiNextEasyCoupon\Service\VoucherRedemption\ValidatorPass as VoucherRedemptionValidatorPass;
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 Symfony\Component\Config\Exception\FileLocatorFileNotFoundException;
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 NetiNextEasyCoupon extends Plugin
{
public function build(ContainerBuilder $container): void
{
parent::build($container);
$container->addCompilerPass(new VoucherCodeGeneratorValidatorPass());
$container->addCompilerPass(new VoucherRedemptionValidatorPass());
$ruleBasedContainerFileLoader = new RuleBasedContainerFileLoader(
$container,
$this->getPath()
);
/** @var string */
$version = $container->getParameter('kernel.shopware_version');
$ruleBasedContainerFileLoader->load(new RuleBasedContainerFile(
$this->getPath() . '/Resources/config/migrations/6.3.5.0/NEXT-12478-after.xml',
function () use ($version) {
return \version_compare($version, '6.3.5.0', '>=')
&& \version_compare($version, '6.4.3.0', '<');
}
));
$ruleBasedContainerFileLoader->load(new RuleBasedContainerFile(
$this->getPath() . '/Resources/config/migrations/6.4.3.0/NEXT-15687.xml',
function () use ($version) {
return \version_compare($version, '6.4.3.0', '>=');
}
));
$this->registerEnvBasedContainerFile($container);
$ruleBasedContainerFileLoader->load(new RuleBasedContainerFile(
$this->getPath() . '/Resources/config/services/flow.xml',
function () use ($version) {
return \version_compare($version, CheckService::MINIMUM_FLOW_SHOPWARE_VERSION, '>=');
}
));
}
public function install(InstallContext $installContext): void
{
parent::install($installContext);
$setup = new Setup($this->container, $installContext);
$setup->install();
$setup->installImportExportProfile($installContext->getContext());
}
/**
* @param UninstallContext $uninstallContext
*
* @throws DBALException
*/
public function uninstall(UninstallContext $uninstallContext): void
{
parent::uninstall($uninstallContext);
if (false === $uninstallContext->keepUserData()) {
$connection = $this->container->get(Connection::class);
if (!$connection instanceof Connection) {
return;
}
$setup = new Setup($this->container, $uninstallContext);
$setup->uninstall();
}
}
public function update(UpdateContext $updateContext): void
{
$setup = new Setup($this->container, $updateContext);
$setup->installImportExportProfile($updateContext->getContext());
$setup->update();
}
private function registerEnvBasedContainerFile(ContainerBuilder $container): void
{
/**
* Suppress weird Psalm issue, that only appears in ci/cd pipelines.
*
* @psalm-suppress UndefinedDocblockClass
*/
$env = $container->getParameter('kernel.environment');
if (!\is_string($env) || '' === $env) {
return;
}
$fileLocator = new FileLocator($this->getPath());
$loaderResolver = new LoaderResolver([
new XmlFileLoader($container, $fileLocator),
new YamlFileLoader($container, $fileLocator),
new PhpFileLoader($container, $fileLocator),
]);
$delegatingLoader = new DelegatingLoader($loaderResolver);
foreach (glob($this->getPath() . "/Resources/config/{$env}/*") as $path) {
try {
$fileLocator->locate($path);
$delegatingLoader->load($path);
} catch (FileLocatorFileNotFoundException $exception) {
// Nothing to catch here
}
}
}
}