[CONFIG] Refactor configuration loading

This commit is contained in:
Hugo Sales 2021-08-21 22:27:34 +01:00
parent c94ef26617
commit 3587b8dc1d
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
2 changed files with 29 additions and 21 deletions

View File

@ -46,6 +46,7 @@ use App\Core\DB\DB;
use App\Core\I18n\I18n;
use App\Core\Queue\Queue;
use App\Core\Router\Router;
use App\Kernel;
use App\Security\EmailVerifier;
use App\Util\Common;
use App\Util\Exception\ConfigurationException;
@ -53,7 +54,9 @@ use App\Util\Formatting;
use Doctrine\ORM\EntityManagerInterface;
use HtmlSanitizer\SanitizerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
@ -219,6 +222,29 @@ class GNUsocial implements EventSubscriberInterface
return $event;
}
/**
* Load configuration files
*/
public static function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void
{
// Overriding doesn't work as we want, overrides the top-most key, do it manually
$local_file = INSTALLDIR . '/social.local.yaml';
if (!file_exists($local_file)) {
file_put_contents($local_file, "parameters:\n locals:\n gnusocial:\n");
}
// Load .local
$loader->load($local_file);
$locals = $container->getParameter('locals');
// Load normal config
$loader->load(INSTALLDIR . '/social' . Kernel::CONFIG_EXTS, 'glob');
$container->setParameter('gnusocial_defaults', $container->getParameter('gnusocial'));
$configs = array_replace_recursive($container->getParameter('gnusocial'), $locals['gnusocial']);
$container->setParameter('gnusocial', $configs);
}
/**
* Tell Symfony which events we want to listen to, which Symfony detects and auto-wires
* due to this implementing the `EventSubscriberInterface`

View File

@ -30,6 +30,7 @@
namespace App;
use App\Core\GNUsocial;
use App\DependencyInjection\Compiler\ModuleManagerPass;
use App\DependencyInjection\Compiler\SchemaDefDriver;
use const PHP_VERSION_ID;
@ -44,7 +45,7 @@ class Kernel extends BaseKernel
{
use MicroKernelTrait;
private const CONFIG_EXTS = '.{php,xml,yaml,yml}';
public const CONFIG_EXTS = '.{php,xml,yaml,yml}';
public function __construct(string $environment, bool $debug)
{
@ -112,26 +113,7 @@ class Kernel extends BaseKernel
$loader->load($confDir . '/{services}' . self::CONFIG_EXTS, 'glob');
$loader->load($confDir . '/{services}_' . $this->environment . self::CONFIG_EXTS, 'glob');
$module_templates = array_merge(glob(INSTALLDIR . '/components/*/templates'), glob(INSTALLDIR . '/plugins/*/templates'));
$templates = ['%kernel.project_dir%/templates' => 'default_path', '%kernel.project_dir%/public' => 'public_path'];
foreach ($module_templates as $t) {
$templates[$t] = null;
}
$container->loadFromExtension('twig', ['paths' => $templates]);
// Overriding doesn't work as we want, overrides the top-most key, do it manually
$local_file = INSTALLDIR . '/social.local.yaml';
if (!file_exists($local_file)) {
file_put_contents($local_file, "parameters:\n gnusocial:\n");
}
$loader->load($local_file);
$locals = $container->getParameter('gnusocial');
$loader->load(INSTALLDIR . '/social' . self::CONFIG_EXTS, 'glob');
$container->setParameter('gnusocial_defaults', $defaults = $container->getParameter('gnusocial'));
if (is_array($locals)) {
$configs = array_replace_recursive($defaults, $locals);
$container->setParameter('gnusocial', $configs);
}
GNUsocial::configureContainer($container, $loader);
}
/**