<?php declare(strict_types=1);
namespace Wbfk\ITScope;
use Doctrine\DBAL\Connection;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
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\Kernel;
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\GlobFileLoader;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Wbfk\ITScope\DependencyInjection\WbfkITScopeExtension;
use Wbfk\ITScope\Helper\InstallHelper;
class WbfkITScope extends Plugin
{
public function install(InstallContext $installContext): void
{
parent::install($installContext);
$this->upsertCustomFields($installContext);
}
public function createContainerExtension(): WbfkITScopeExtension
{
return new WbfkITScopeExtension();
}
public function build(ContainerBuilder $container): void
{
$this->buildConfig($container);
parent::build($container);
}
private function buildConfig(ContainerBuilder $container): void
{
$locator = new FileLocator('Resources/config');
$resolver = new LoaderResolver([
new YamlFileLoader($container, $locator),
new GlobFileLoader($container, $locator),
]);
$configLoader = new DelegatingLoader($resolver);
$confDir = $this->getPath().'/Resources/config';
$configLoader->load($confDir.'/{packages}/*'.Kernel::CONFIG_EXTS, 'glob');
}
public function update(UpdateContext $updateContext): void
{
parent::update($updateContext);
$this->upsertCustomFields($updateContext);
}
public function uninstall(UninstallContext $uninstallContext): void
{
if($uninstallContext->keepUserData()) {
return;
}
$connection = $this->container->get(Connection::class);
$query = <<<SQL
DROP TABLE `wbfk_it_scope_product_extension`;
DROP TABLE `wbfk_it_scope_product_warehouse_extension`;
DROP TABLE wbfk_supplier_delivery_time;
SQL;
$connection->executeStatement($query);
}
private function upsertCustomFields(InstallContext $updateContext)
{
/** @var EntityRepository $customFieldSetRepository */
$customFieldSetRepository = $this->container->get(
'custom_field_set.repository'
);
InstallHelper::upsertFieldSet(
$updateContext,
$customFieldSetRepository
);
}
}