Skip to content
This repository has been archived by the owner on Mar 19, 2023. It is now read-only.

Commit

Permalink
Chore: Be compatible with sonata-theme 1.x and 2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
ceesvanegmond committed Nov 3, 2017
1 parent dc2c4cd commit 456b394
Showing 1 changed file with 48 additions and 3 deletions.
51 changes: 48 additions & 3 deletions DependencyInjection/WearejustFormExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use RuntimeException;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
Expand All @@ -18,11 +19,16 @@ class WearejustFormExtension extends Extension implements PrependExtensionInterf
{
/**
* {@inheritdoc}
* @throws \RuntimeException
*/
public function load(array $configs, ContainerBuilder $container)
{
$loadedBundles = $container->getParameter('kernel.bundles');

if ($this->isWearejustSonataThemeLoaded($loadedBundles)) {
$this->guardAgainstInvalidOrderIfWearejustSonataTheme($loadedBundles);
}

$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

Expand Down Expand Up @@ -50,17 +56,29 @@ public function prepend(ContainerBuilder $container)
{
$loadedBundles = $container->getParameter('kernel.bundles');

if (! $this->isWearejustSonataThemeLoaded($loadedBundles)) {
return;
}

$configs = $container->getExtensionConfig($this->getAlias());
$config = $this->processConfiguration(new Configuration(), $configs);

$assets = ['js' => [], 'css' => []];
$assets = $this->addSwitcheryConfig($assets, $config);
$assets = $this->addPrestataImageConfig($assets, $loadedBundles, $config);

$container->prependExtensionConfig('just_sonata_theme', [
$assetConfig = [
'extra_css_assets' => $assets['css'],
'extra_js_assets' => $assets['js']
]);
];

try {
$container->getExtension('wearejust_sonata_theme');

$container->prependExtensionConfig('wearejust_sonata_theme', $assetConfig);
} catch (LogicException $e) {
$container->prependExtensionConfig('just_sonata_theme', $assetConfig);
}
}

/**
Expand Down Expand Up @@ -127,4 +145,31 @@ private function addSwitcheryConfig(array $assets, array $config)

return $assets;
}
}

/**
* @param $loadedBundles
*
* @throws \RuntimeException
*/
private function guardAgainstInvalidOrderIfWearejustSonataTheme($loadedBundles)
{
$bundles = array_keys($loadedBundles);

$parentThemePosition = array_search('WearejustSonataThemeBundle', $bundles, true) ?: array_search('JustSonataThemeBundle', $bundles, true);
$currentBundlePosition = array_search('WearejustFormBundle', $bundles, true);

if ($parentThemePosition && $currentBundlePosition > $parentThemePosition) {
throw new RuntimeException('Package [WearejustFormBundle] loaded before [WearejustSonataThemeBundle/JustSonataThemeBundle], please change order in AppKernel.php');
}
}

/**
* @param array $bundles
*
* @return mixed
*/
private function isWearejustSonataThemeLoaded(array $bundles)
{
return array_key_exists('WearejustSonataThemeBundle', $bundles) || array_key_exists('JustSonataThemeBundle', $bundles);
}
}

0 comments on commit 456b394

Please sign in to comment.