allow to include bundle configuration in system configuration screen (#1235)

This commit is contained in:
Kevin Papst
2019-11-14 19:25:38 +01:00
committed by GitHub
parent c32cd36861
commit b4739f8f03
4 changed files with 106 additions and 15 deletions

View File

@@ -60,8 +60,6 @@ class AppExtension extends Extension
$this->createThemeParameter($config['theme'], $container);
$this->createUserParameter($config['user'], $container);
$container->setParameter('kimai.config', $config);
$container->setParameter('kimai.timesheet', $config['timesheet']);
$container->setParameter('kimai.timesheet.rates', $config['timesheet']['rates']);
$container->setParameter('kimai.timesheet.rounding', $config['timesheet']['rounding']);
@@ -77,6 +75,22 @@ class AppExtension extends Extension
$localTranslations[] = $config['industry']['translation'];
}
$container->setParameter('kimai.i18n_domains', $localTranslations);
// this should happen always at the end, so bundles do not mess with the base configuration
if ($container->hasParameter('kimai.bundles.config')) {
$bundleConfig = $container->getParameter('kimai.bundles.config');
if (!is_array($bundleConfig)) {
trigger_error('Invalid bundle configuration found, skipping all bundle configuration');
}
foreach ($bundleConfig as $key => $value) {
if (array_key_exists($key, $config)) {
trigger_error(sprintf('Invalid bundle configuration "%s" found, skipping', $key));
continue;
}
$config[$key] = $value;
}
}
$container->setParameter('kimai.config', $config);
}
protected function setLdapParameter(array $config, ContainerBuilder $container)

View File

@@ -96,15 +96,25 @@ class Kernel extends BaseKernel
}
}
foreach ($this->getBundleDirectories() as $bundleDir) {
$bundleName = $bundleDir->getRelativePathname();
$pluginClass = 'KimaiPlugin\\' . $bundleName . '\\' . $bundleName;
yield new $pluginClass();
}
}
private function getBundleDirectories(): array
{
$pluginsDir = $this->getProjectDir() . '/var/plugins';
if (!file_exists($pluginsDir)) {
return;
return [];
}
if ($this->environment === 'test' && getenv('TEST_WITH_BUNDLES') === false) {
return;
return [];
}
$directories = [];
$finder = new Finder();
$finder->ignoreUnreadableDirs()->directories()->name('*Bundle');
/** @var SplFileInfo $bundleDir */
@@ -116,13 +126,19 @@ class Kernel extends BaseKernel
continue;
}
if (file_exists($bundleDir->getRealPath() . '/.disabled')) {
continue;
}
$pluginClass = 'KimaiPlugin\\' . $bundleName . '\\' . $bundleName;
if (!class_exists($pluginClass)) {
continue;
}
yield new $pluginClass();
$directories[] = $bundleDir;
}
return $directories;
}
protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader)
@@ -187,17 +203,10 @@ class Kernel extends BaseKernel
// load application routes
$routes->import($confDir . '/routes' . self::CONFIG_EXTS, '/', 'glob');
if ($this->environment === 'test' && getenv('TEST_WITH_BUNDLES') === false) {
return;
/** @var SplFileInfo $bundleDir */
foreach ($this->getBundleDirectories() as $bundleDir) {
$routes->import($bundleDir->getRealPath() . '/Resources/config/routes' . self::CONFIG_EXTS, '/', 'glob');
}
// load plugin routes
$pluginsDir = $this->getProjectDir() . '/var/plugins';
if (!file_exists($pluginsDir)) {
return;
}
$routes->import($pluginsDir . '/*Bundle/Resources/config/routes' . self::CONFIG_EXTS, '/', 'glob');
}
protected function configureFosUserRoutes(RouteCollectionBuilder $routes)

View File

@@ -0,0 +1,30 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Plugin;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
abstract class AbstractPluginExtension extends Extension
{
protected function registerBundleConfiguration(ContainerBuilder $container, array $configs)
{
$bundleConfig = [$this->getAlias() => $configs];
if ($container->hasParameter('kimai.bundles.config')) {
$bundleConfig = array_merge(
$container->getParameter('kimai.bundles.config'),
$bundleConfig
);
}
$container->setParameter('kimai.bundles.config', $bundleConfig);
}
}

View File

@@ -375,5 +375,43 @@ class AppExtensionTest extends TestCase
$this->extension->load([], $container = $this->getContainer());
}
public function testWithBundleConfiguration()
{
$bundleConfig = [
'foo-bundle' => ['test'],
];
$container = $this->getContainer();
$container->setParameter('kimai.bundles.config', $bundleConfig);
$this->extension->load($this->getMinConfig(), $container);
$config = $container->getParameter('kimai.config');
self::assertEquals(['test'], $config['foo-bundle']);
}
public function testWithBundleConfigurationFailsOnDuplicatedKey()
{
$this->expectException(Notice::class);
$this->expectExceptionMessage('Invalid bundle configuration "timesheet" found, skipping');
$bundleConfig = [
'timesheet' => ['test'],
];
$container = $this->getContainer();
$container->setParameter('kimai.bundles.config', $bundleConfig);
$this->extension->load($this->getMinConfig(), $container);
}
public function testWithBundleConfigurationFailsOnNonArray()
{
$this->expectException(Notice::class);
$this->expectExceptionMessage('Invalid bundle configuration found, skipping all bundle configuration');
$container = $this->getContainer();
$container->setParameter('kimai.bundles.config', 'asdasd');
$this->extension->load($this->getMinConfig(), $container);
}
// TODO test permissions
}