Skip to content

Instantly share code, notes, and snippets.

@jkrzefski
Created February 27, 2018 11:08
Show Gist options
  • Save jkrzefski/0596a359bdddced9d1e2fa42d6c3f05c to your computer and use it in GitHub Desktop.
Save jkrzefski/0596a359bdddced9d1e2fa42d6c3f05c to your computer and use it in GitHub Desktop.
<?php
namespace TestPlugin;
use KskCrowdfunding\Components\AccessEnforcer;
use Shopware\Components\CacheManager;
use Shopware\Components\Model\ModelManager;
use Shopware\Components\Plugin;
class TestPlugin extends Plugin
{
/**
* @return \Symfony\Component\DependencyInjection\ContainerInterface
* @throws \Doctrine\DBAL\ConnectionException
* @throws \Doctrine\DBAL\DBALException
* @throws \Doctrine\ORM\OptimisticLockException
* @throws \Exception
*/
private function getContainer()
{
/** @var ModelManager $modelManager */
$modelManager = $this->container->get('models');
// exit current transaction
$modelManager->flush();
$modelManager->getConnection()->commit();
// set this plugin to active so the new kernel will boot with it
$modelManager->getConnection()->exec("UPDATE s_core_plugins SET active = 1, installation_date = NOW() WHERE name = '" . $this->getName() . "';");
// clear the cache so the container will be rebuilt
/** @var CacheManager $cacheManager */
$cacheManager = $this->container->get('shopware.cache_manager');
$cacheManager->clearProxyCache();
// clone the kernel and reboot it
$kernel = clone $this->container->get('kernel');
$ae = new AccessEnforcer();
$ae->forceWrite($kernel, 'booted', false);
$kernel->boot();
// set this plugin to inactive again
$modelManager->getConnection()->exec("UPDATE s_core_plugins SET active = 0, installation_date = NULL WHERE name = '" . $this->getName() . "';");
// start a new transaction
$modelManager->getConnection()->beginTransaction();
return $kernel->getContainer();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment