carlinhus/database-config-bundle
Installation:
composer require carlinhus/database-config-bundle:dev-master
Update AppKernel.php to include the bundle and extend getContainerBuilder():
public function registerBundles() {
return [
new Carlinhus\DatabaseConfigBundle\CarlinhusDatabaseConfigBundle(),
// ...
];
}
protected function getContainerBuilder() {
return new \Carlinhus\DatabaseConfigBundle\DependencyInjection\Compiler\ContainerBuilder(
new \Symfony\Component\DependencyInjection\ParameterBag\ParameterBag($this->getKernelParameters())
);
}
Run migrations:
php bin/console doctrine:schema:update --force
First use case: Define a config entry in the database via a custom CLI command or admin panel:
INSERT INTO database_config (name, value, bundle) VALUES ('app.some_param', 'dynamic_value', 'app');
Access it in a service:
$value = $this->container->getParameter('app.some_param');
database_config (check src/Carlinhus/DatabaseConfigBundle/Resources/doc/schema.sql).config.yml/parameters.yml.Dynamic Configuration Management:
app.debug or monolog.level in the DB for runtime toggling.Admin Panel Integration:
database_config entries.// src/AppBundle/Controller/ConfigController.php
public function updateConfig(Request $request, EntityManager $em) {
$config = $em->getRepository(Config::class)->find($request->get('id'));
$config->setValue($request->get('value'));
$em->flush();
// Clear cache to reflect changes (see "Gotchas").
}
Bundle-Specific Configs:
fos_user.password_min_length).$configs = $em->createQuery('SELECT c FROM AppBundle:DatabaseConfig c WHERE c.bundle = :bundle')
->setParameter('bundle', 'fos_user')
->getResult();
Validation Layer:
// src/AppBundle/EventListener/ConfigListener.php
public function preUpdate(Config $config) {
if (!is_scalar($config->getValue()) && !is_array($config->getValue())) {
throw new \InvalidArgumentException('Value must be scalar or array');
}
}
$this->container->get('carlinhus_database_config.cache_clearer')->clear();
dev/prod) via a custom query or bundle column.%parameter% syntax for defaults:
# config.yml
app.some_param: "%app.some_param_default%"
Override in DB with app.some_param.Cache Stale Data:
database_config aren’t reflected until cache is cleared.$container->get('carlinhus_database_config.cache_clearer')->clear() after updates.CacheWarmer to auto-clear on config changes (see Symfony CacheWarmer).Bundle Name Collisions:
name + bundle pairs will overwrite each other.app.feature_x.enabled instead of feature.enabled).Non-Scalar Values:
value to be serializable (e.g., no objects).$raw = $config->getValue(); // '{"key":"value"}'
$data = json_decode($raw, true);
Doctrine ORM Conflicts:
database_config table isn’t managed by another bundle.config.yml:
doctrine:
orm:
mappings:
carlinhus_database_config:
type: yaml
dir: "%kernel.root_dir%/config/doctrine"
prefix: Carlinhus\DatabaseConfigBundle\Entity
alias: DatabaseConfig
dump($this->container->getParameterBag()->all());
# config.yml
doctrine:
dbal:
logging: true
# config.yml
carlinhus_database_config:
debug: true
Custom Config Sources:
ConfigLoader to support additional sources (e.g., Redis):
class RedisConfigLoader extends \Carlinhus\DatabaseConfigBundle\Loader\ConfigLoader {
public function load() {
$redis = $this->container->get('redis');
return $redis->hGetAll('config:app');
}
}
carlinhus_database_config.loader tag.Event Dispatching:
carlinhus.database_config.pre_load and post_load events to modify configs:
// src/AppBundle/EventListener/ConfigEventListener.php
public function onPreLoad(ConfigLoadEvent $event) {
$event->setConfig('app.timeout', 30); // Override dynamically
}
services.yml:
services:
app.config_listener:
class: AppBundle\EventListener\ConfigEventListener
tags:
- { name: kernel.event_listener, event: carlinhus.database_config.pre_load }
Custom Validation:
ConfigValidator to enforce rules (e.g., regex for emails):
class CustomConfigValidator extends \Carlinhus\DatabaseConfigBundle\Validator\ConfigValidator {
public function validate($value, $config) {
if ($config->getName() === 'app.email' && !filter_var($value, FILTER_VALIDATE_EMAIL)) {
throw new \InvalidArgumentException('Invalid email');
}
}
}
config.yml:
carlinhus_database_config:
validator: app.custom_config_validator
How can I help you explore Laravel packages today?