Skip to content

Commit

Permalink
Treat prepend and load with different logic. Apply config array only …
Browse files Browse the repository at this point in the history
…if present. Issue #180
  • Loading branch information
shakaran committed Oct 3, 2017
1 parent a64d064 commit 7b3801b
Showing 1 changed file with 57 additions and 69 deletions.
126 changes: 57 additions & 69 deletions DependencyInjection/AvanzuAdminThemeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,25 @@ public function load(array $configs, ContainerBuilder $container)
$baseConfiguration = new Configuration();

// Load the configuration from files
$configs = $container->getExtensionConfig($this->getAlias());

try
{
$config = $this->processConfiguration($baseConfiguration, $configs);
}
catch(InvalidConfigurationException $e)
{
echo 'AvanzuAdminBundle: invalid config: ' . $e->getMessage() . PHP_EOL;
// Fallback: ignore invalid config from the container user config file and use default values from base configuration
$config = $baseConfiguration;
// Fallback: ignore invalid config from the container user config file and abort use configuration in load
echo 'AvanzuAdminBundle: invalid config (load): ' . $e->getMessage() . PHP_EOL . ' The config options for the bundle AvanzuAdminBundle were skipped';
$config = [];
}

// Set the parameters from config files
$container->setParameter('avanzu_admin_theme.bower_bin', (string) (isset($config['bower_bin']) ? $config['bower_bin'] : ''));
$container->setParameter('avanzu_admin_theme.use_twig', (bool) (isset($config['use_twig']) ? $config['use_twig'] : FALSE));
$container->setParameter('avanzu_admin_theme.options', (array) (isset($config['options']) ? $config['options'] : []));
// Use the config only if it is fully validated from the processed configuration
if(!empty($config))
{
// Set the parameters from config files
$container->setParameter('avanzu_admin_theme.bower_bin', (string) (isset($config['bower_bin']) ? $config['bower_bin'] : ''));
$container->setParameter('avanzu_admin_theme.use_twig', (bool) (isset($config['use_twig']) ? $config['use_twig'] : FALSE));
$container->setParameter('avanzu_admin_theme.options', (array) (isset($config['options']) ? $config['options'] : []));
}

// Load the services (with parameters loaded), since twig require theme_manager service
try
Expand Down Expand Up @@ -69,6 +71,8 @@ public function load(array $configs, ContainerBuilder $container)

/**
* Allow an extension to prepend the extension configurations.
*
* @see https://symfony.com/doc/current/bundles/prepend_extension.html
*
* @param ContainerBuilder $container
*/
Expand All @@ -77,77 +81,61 @@ public function prepend(ContainerBuilder $container)
$baseConfiguration = new Configuration();

// Load the configuration from files


// The configuration of AvanzuAdminThemeExtension
$configs = $container->getExtensionConfig($this->getAlias());

try
{
// use the Configuration class to generate a config array with the config extension
$config = $this->processConfiguration($baseConfiguration, $configs);
}
catch(InvalidConfigurationException $e)
{
// Fallback: ignore invalid config from the container user config file and use default values from base configuration
echo 'AvanzuAdminBundle: invalid config: ' . $e->getMessage() . PHP_EOL;
$config = $baseConfiguration;
// Fallback: ignore invalid config from the container user config file and abort use configuration in prepend
echo 'AvanzuAdminBundle: invalid config (prepend): ' . $e->getMessage() . PHP_EOL . ' The config options for the bundle AvanzuAdminBundle were skipped';
$config = [];
}

// Set the parameters from config files
$container->setParameter('avanzu_admin_theme.bower_bin', (string) (isset($config['bower_bin']) ? $config['bower_bin'] : ''));
$container->setParameter('avanzu_admin_theme.use_twig', (bool) (isset($config['use_twig']) ? $config['use_twig'] : FALSE));
$container->setParameter('avanzu_admin_theme.options', (array) (isset($config['options']) ? $config['options'] : []));

// Load the services (with parameters loaded), since twig require theme_manager service
try
{
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.xml');
}
catch(FileLocatorFileNotFoundException $e) // Symfony 3.3 and 4.x are based in YAML
{
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.yml');
}
catch(InvalidArgumentException $e) // Symfony 3.3 and 4.x are based in YAML
{
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.yml');
}
catch(\Exception $e) // Symfony 3.3 and 4.x are based in YAML
// Use the config only if it is fully validated from the processed configuration
if(!empty($config))
{
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.yml');
// echo 'AvanzuAdminTheme: ' . $e->getMessage() . PHP_EOL; // Use this for your own debugging
}

$bundles = $container->getParameter('kernel.bundles');

// Inject in twig global config the theme_manager service
if ($config['use_twig'] && isset($bundles['TwigBundle'])) {
$container->prependExtensionConfig(
'twig',
[
'form_theme' => [
'AvanzuAdminThemeBundle:layout:form-theme.html.twig',
],
'globals' => [
'admin_theme' => '@avanzu_admin_theme.theme_manager',
],
]
);
}

if ($config['use_assetic'] && isset($bundles['AsseticBundle'])) {
$assets = include dirname(__FILE__) . '/../Resources/config/assets.php';

$container->prependExtensionConfig(
'assetic',
[
'assets' => $assets,
'bundles' => [
'AvanzuAdminThemeBundle',
],
]
);
// Set the parameters from config files
$container->setParameter('avanzu_admin_theme.bower_bin', (string) (isset($config['bower_bin']) ? $config['bower_bin'] : ''));
$container->setParameter('avanzu_admin_theme.use_twig', (bool) (isset($config['use_twig']) ? $config['use_twig'] : FALSE));
$container->setParameter('avanzu_admin_theme.options', (array) (isset($config['options']) ? $config['options'] : []));

// Get all the bundles
$bundles = $container->getParameter('kernel.bundles');

// Inject in twig global config the theme_manager service
if ($config['use_twig'] && isset($bundles['TwigBundle'])) {
$container->prependExtensionConfig(
'twig',
[
'form_theme' => [
'AvanzuAdminThemeBundle:layout:form-theme.html.twig',
],
'globals' => [
'admin_theme' => '@avanzu_admin_theme.theme_manager',
],
]
);
}

if ($config['use_assetic'] && isset($bundles['AsseticBundle'])) {
$assets = include dirname(__FILE__) . '/../Resources/config/assets.php';

$container->prependExtensionConfig(
'assetic',
[
'assets' => $assets,
'bundles' => [
'AvanzuAdminThemeBundle',
],
]
);
}
}
}
}

0 comments on commit 7b3801b

Please sign in to comment.