<?php
declare(strict_types=1);
namespace NetInventors\NetiNextCmsElementBuilder;
use Doctrine\DBAL\Exception;
use Shopware\Core\Content\Cms\DataResolver\CmsSlotsDataResolver;
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 NetInventors\NetiNextCmsElementBuilder\Components\Setup;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
class NetiNextCmsElementBuilder extends Plugin
{
private bool $autoloaderInjected = false;
public function getAdditionalBundles(AdditionalBundleParameters $parameters): array
{
// This is not a nice solution, but there is no other way to inject the autoloader
if (!$this->autoloaderInjected) {
$this->injectAutoloader();
$this->autoloaderInjected = true;
}
return parent::getAdditionalBundles($parameters);
}
/**
* @throws \Exception
*/
public function install(InstallContext $installContext): void
{
parent::install($installContext);
(new Setup($installContext, $this->container))->install();
}
/**
* @throws Exception
* @throws \Exception
*/
public function uninstall(UninstallContext $uninstallContext): void
{
parent::uninstall($uninstallContext);
if ($uninstallContext->keepUserData()) {
return;
}
(new Setup($uninstallContext, $this->container))->uninstall();
}
public function build(ContainerBuilder $container): void
{
parent::build($container);
$container->getDefinition(CmsSlotsDataResolver::class)
->setArgument(0, new Reference('net_inventors.cms_element_builder.resolver.elements'));
}
private function injectAutoloader(): void
{
// For development
if (is_file(__DIR__ . '/Resources/build/vendor/autoload.php')) {
require __DIR__ . '/Resources/build/vendor/autoload.php';
return;
}
// For production
if (is_file(__DIR__ . '/Resources/vendor/autoload.php')) {
require __DIR__ . '/Resources/vendor/autoload.php';
return;
}
}
}