Installation Add the package via Composer (GitHub repo, not Packagist):
composer require 2lenet/config-bundle
Register the bundle in config/bundles.php:
return [
// ...
\TwoLenet\ConfigBundle\TwoLenetConfigBundle::class => ['all' => true],
];
Create a Basic Config Entity
Define a Config entity in src/Entity/ (e.g., App\Entity\Config) implementing ConfigInterface:
use TwoLenet\ConfigBundle\Entity\ConfigInterface;
use TwoLenet\ConfigBundle\Entity\Traits\ConfigTrait;
class Config implements ConfigInterface
{
use ConfigTrait; // Provides core functionality
}
First Use Case: CRUD Integration
Use the bundled ConfigRepository to fetch/save configs:
use TwoLenet\ConfigBundle\Repository\ConfigRepository;
$config = $this->getDoctrine()->getRepository(ConfigRepository::class)->findOneBy(['key' => 'app.theme']);
if (!$config) {
$config = new Config();
$config->setKey('app.theme')->setValue('dark');
$this->getDoctrine()->getManager()->persist($config);
}
Configuration Management
ConfigTrait to leverage built-in methods like setKey(), setValue(), or getConfig().ConfigRepository for CRUD operations (e.g., findByKey(), save()).// Load config
$theme = $config->getConfig('app.theme');
// Update config
$config->setConfig(['app.theme' => 'light']);
$entityManager->flush();
Integration with CruditBundle
# config/packages/crudit.yaml
crudit:
resources:
App\Entity\Config:
fields: [key, value]
edit: true
Environment-Specific Configs
ConfigInterface to scope configs by environment (e.g., setEnvironment('prod')).app., service.) to avoid collisions.$cache = $this->get('cache.app');
$config = $cache->get('config.app.theme', fn() => $configRepo->findByKey('app.theme'));
No Packagist Support
composer.json (as shown in README). Monitor for future Packagist updates.Limited Documentation
ConfigTrait source for undocumented methods (e.g., isValid()).validate() in your entity to enforce custom rules:
public function validate(): bool
{
return !empty($this->getKey()) && strlen($this->getValue()) <= 255;
}
Repository Assumptions
ConfigRepository assumes a key and value column. Customize via Doctrine extensions if your schema differs.Custom Validation
ConfigInterface to add validation logic:
public function isValid(): bool {
return parent::isValid() && $this->getValue() === 'dark' || $this->getValue() === 'light';
}
Event Listeners
prePersist):
// src/EventListener/ConfigListener.php
public function onConfigPersist(Config $config, EventArgs $args)
{
if ($config->getKey() === 'app.debug') {
$this->container->get('monolog.logger')->info('Debug mode changed');
}
}
Performance
$configs = $configRepo->createQueryBuilder('c')
->where('c.key LIKE :prefix')
->setParameter('prefix', 'app.%')
->getQuery()
->getResult();
config/packages/dev/doctrine.yaml:
doctrine:
dbal:
logging: true
php artisan ide-helper:generate to resolve trait method conflicts in IDEs.How can I help you explore Laravel packages today?