creavo/option-bundle
Symfony bundle to store and retrieve application options/settings via a service, Twig helper, or console commands. Persists values in Doctrine, supports typed options, optional eager loading, and PSR-16 simple cache for faster reads.
Installation:
composer require creavo/option-bundle
Add to config/bundles.php (Symfony 4+):
return [
// ...
Creavo\OptionBundle\CreavoOptionBundle::class => ['all' => true],
];
Configure (config/packages/creavo_option.yaml):
creavo_option:
fetch_all: false # Set to `true` if you need all options loaded eagerly
simple_cache_service: 'cache.app' # Optional: PSR-16 cache service
Run migrations:
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
Set a basic option via CLI:
php bin/console crv:ob:set site_maintenance_mode "false" boolean system
Retrieve it in PHP:
$maintenanceMode = $this->get('crv.option')->get('site_maintenance_mode');
Service Integration:
crv.option service into controllers/services:
public function __construct(private OptionService $optionService) {}
set()/get() for dynamic values:
$this->optionService->set('feature_flag.new_ui', true, 'boolean', 'features');
$isEnabled = $this->optionService->get('feature_flag.new_ui');
Twig Integration:
{% if crv_ob_setting('feature_flag.new_ui') %}
<div class="new-ui">...</div>
{% endif %}
Console Automation:
# Set multiple options from a file
while IFS= read -r line; do
php bin/console crv:ob:set "$line"
done < options.txt
Caching Strategies:
$this->optionService->getUnCached('live_stats.refresh_rate');
fetch_all: true in config for read-heavy apps (reduces DB queries).Type-Safe Values:
boolean, integer, dateTime) for validation:
$this->optionService->set('max_retries', 3, 'integer', 'limits');
Section-Based Organization:
system, features, limits) for logical separation.Cache Invalidation:
getUnCached() bypasses cache but hits the DB. Use sparingly in performance-critical paths.php bin/console cache:clear
Type Mismatches:
"true" as boolean) cause runtime errors. Validate types during set():
if (!is_bool($value)) {
throw new \InvalidArgumentException('Value must be boolean');
}
Schema Updates:
php bin/console doctrine:migrations:execute 'DoctrineMigrations\VersionYYYYMMDDHHMMSS'
Inspect Options:
php bin/console crv:ob:list # List all options
php bin/console crv:ob:get option_name # Debug a specific option
Query Logging: Enable Doctrine logging to trace DB queries:
# config/packages/dev/doctrine.yaml
doctrine:
dbal:
logging: true
Custom Cache Backend:
Psr\SimpleCache\CacheInterface and inject via config:
creavo_option:
simple_cache_service: 'my_custom_cache'
Event Listeners:
option.set/option.get events (if the bundle supports them; check source for hooks).Validation Rules:
Option entity to add custom validation (e.g., regex for strings):
// src/Entity/Option.php
use Symfony\Component\Validator\Constraints as Assert;
/**
* @Assert\Regex("/^[a-z0-9_-]+$/i")
*/
private $name;
How can I help you explore Laravel packages today?