aygon/database-config-bundle
Installation Add the bundle via Composer:
composer require aygon/database-config-bundle
Enable it in config/bundles.php:
return [
// ...
Aygon\DatabaseConfigBundle\AygonDatabaseConfigBundle::class => ['all' => true],
];
Configuration
Define database configurations in config/packages/aygon_database_config.yaml:
aygon_database_config:
databases:
default:
driver: "%env(DATABASE_DRIVER)%"
host: "%env(DATABASE_HOST)%"
port: "%env(int:DATABASE_PORT)%"
dbname: "%env(DATABASE_NAME)%"
user: "%env(DATABASE_USER)%"
password: "%env(DATABASE_PASSWORD)%"
charset: "%env(DATABASE_CHARSET)%"
prefix: "%env(DATABASE_PREFIX)%"
read_replica:
driver: "%env(READ_REPLICA_DRIVER)%"
# ... (other replica-specific configs)
First Use Case
Inject the DatabaseConfig service into a controller or service:
use Aygon\DatabaseConfigBundle\Service\DatabaseConfig;
class SomeController extends AbstractController
{
public function __construct(private DatabaseConfig $databaseConfig) {}
public function index()
{
$defaultConfig = $this->databaseConfig->getConfig('default');
// Use $defaultConfig['host'], $defaultConfig['dbname'], etc.
}
}
Use the bundle to dynamically switch database connections in Doctrine:
// In a service or controller
$connectionName = $request->get('connection', 'default');
$config = $this->databaseConfig->getConfig($connectionName);
// Override Doctrine's default connection
$entityManager->getConnection()->getParams() = $config;
Leverage Symfony’s %env() to load configs from .env:
# config/packages/aygon_database_config.yaml
aygon_database_config:
databases:
staging:
host: "%env(STAGING_DB_HOST)%"
port: "%env(int:STAGING_DB_PORT)%"
Extend the bundle’s DatabaseConfig service to auto-configure Doctrine:
// src/Service/DoctrineConfigurator.php
class DoctrineConfigurator
{
public function __construct(
private DatabaseConfig $databaseConfig,
private EntityManagerInterface $em
) {
$this->configureDoctrine();
}
private function configureDoctrine(): void
{
$config = $this->databaseConfig->getConfig('default');
$this->em->getConnection()->getParams()->update($config);
}
}
Access configs in console commands:
// src/Command/BackupCommand.php
protected function configure(): void
{
$this->databaseConfig = $this->get('aygon_database_config.database_config');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$dbConfig = $this->databaseConfig->getConfig('default');
$output->writeln(sprintf('Backing up %s@%s', $dbConfig['dbname'], $dbConfig['host']));
// ...
}
Symfony Version Lock The bundle only supports Symfony 2.1.x (not 2.2+ or 3/4/5). Avoid upgrading Symfony without checking compatibility.
Doctrine Migrations Dependency
The bundle requires aygon/doctrine-migrations-bundle (v1.x). If you don’t use migrations, manually install the dependency or fork the bundle.
No Built-in Validation Configs are loaded as-is. Validate inputs in your services:
$config = $this->databaseConfig->getConfig('test');
if (empty($config['host'])) {
throw new \RuntimeException('Database host is missing!');
}
dump() to inspect loaded configs:
dump($this->databaseConfig->getAllConfigs());
.env variables are loaded (e.g., php bin/console debug:config aygon_database_config).Custom Config Loaders
Override the bundle’s DatabaseConfig service to add logic:
# config/services.yaml
services:
App\Service\CustomDatabaseConfig:
decorates: 'aygon_database_config.database_config'
arguments: ['@.inner']
// src/Service/CustomDatabaseConfig.php
class CustomDatabaseConfig extends DatabaseConfig
{
public function getConfig(string $name): array
{
$config = parent::getConfig($name);
$config['read_only'] = $name === 'read_replica';
return $config;
}
}
Dynamic Configs Fetch configs from an API or cache:
// Extend DatabaseConfig to fetch remote configs
public function getConfig(string $name): array
{
if ($name === 'remote') {
return $this->fetchFromApi();
}
return parent::getConfig($name);
}
Testing
Mock the DatabaseConfig service in tests:
$this->container->set('aygon_database_config.database_config', $this->createMock(DatabaseConfig::class));
How can I help you explore Laravel packages today?