<?php declare(strict_types=1);
namespace Shopware\Production\Subscriber;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Plugin\PluginLifecycleService;
use Shopware\Core\Framework\Plugin\PluginManagementService;
use Shopware\Core\Framework\Store\Event\FirstRunWizardFinishedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class FirstRunWizardSubscriber implements EventSubscriberInterface
{
private const DELETABLE_PLUGINS = [
'SwagMarkets',
'SwagPayPal'
];
private PluginLifecycleService $pluginLifecycleService;
private string $projectDir;
private EntityRepositoryInterface $pluginRepository;
private Context $context;
private PluginManagementService $pluginManagementService;
public static function getSubscribedEvents()
{
return [FirstRunWizardFinishedEvent::class => 'onFirstRunWizardFinished'];
}
public function __construct(
string $projectDir,
PluginLifecycleService $pluginLifecycleService,
PluginManagementService $pluginManagementService,
EntityRepositoryInterface $pluginRepository
)
{
$this->pluginLifecycleService = $pluginLifecycleService;
$this->projectDir = $projectDir;
$this->pluginRepository = $pluginRepository;
$this->context = Context::createDefaultContext();
$this->pluginManagementService = $pluginManagementService;
}
public function onFirstRunWizardFinished(FirstRunWizardFinishedEvent $event)
{
foreach (self::DELETABLE_PLUGINS as $plugin) {
try {
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('name', $plugin));
$plugin = $this->pluginRepository->search($criteria, $this->context)->first();
$this->pluginLifecycleService->uninstallPlugin($plugin, $this->context, false);
$this->pluginManagementService->deletePlugin($plugin, $this->context);
} catch (\Throwable $e) {
}
}
}
}