Symfony bundle that gives you an easy configuration for your app. Perfect to use with the famous CRUD Crudit
The bundle is not yet on packagist make sure to add the following to your composer.json
file:
{
"url": "https://github.com/2lenet/ConfigBundle",
"type": "git"
}
Install with composer:
composer require 2lenet/config-bundle
The bundle is flexible and built to suit your project it is shiped only with trait to use in your own config entity.
You will also get a Symfony Repository ready to use.
Create in your entity directory the class Config
it has to implements the ConfigInterface if no customization is
needed you can use:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Lle\ConfigBundle\Traits\ConfigTrait;
use App\Repository\ConfigRepository;
use Lle\ConfigBundle\Contracts\ConfigInterface;
/**
* @ORM\Entity(repositoryClass=ConfigRepository::class)
*/
class Config implements ConfigInterface
{
use ConfigTrait;
}
Your repository file has to extend the ConfigRepository from the bundle:
<?php
namespace App\Repository;
use App\Entity\Config;
use Doctrine\Persistence\ManagerRegistry;
use Lle\ConfigBundle\Repository\AbstractConfigRepository;
/**
* @method Config|null find($id, $lockMode = null, $lockVersion = null)
* @method Config|null findOneBy(array $criteria, array $orderBy = null)
* @method Config[] findAll()
* @method Config[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class ConfigRepository extends AbstractConfigRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Config::class);
}
...
}
In your project add the folowing to the config file: /config/packages/doctrine.yaml
doctrine:
orm:
resolve_target_entities:
Lle\ConfigBundle\Contracts\ConfigInterface: App\Entity\Config
In /config/routes.yaml
add:
lle_config:
resource: "@LleConfigBundle/Resources/config/routes.yaml"
You can then create a migration
bin/console make:migration
Check the migration file created and ask doctrine to execute the migration :
bin/console doctrine:migrations:migrate
You are ready to go!
If you need more options or entity fields you can add them in your entity class:
<?php
namespace App\Entity;
use App\Repository\ConfigRepository;
use Doctrine\ORM\Mapping as ORM;
use Lle\ConfigBundle\Contracts\ConfigInterface;
use Lle\ConfigBundle\Traits\ConfigTrait;
/**
* @ORM\Entity(repositoryClass=ConfigRepository::class)
*/
class Config implements ConfigInterface
{
use ConfigTrait;
/**
* @ORM\ManyToOne(targetEntity=Establishment::class, inversedBy="configs")
* @ORM\JoinColumn(nullable=false)
*/
private ?Establishment $establishment;
public function getEstablishment(): ?Establishment
{
return $this->establishment;
}
public function setEstablishment(?Establishment $establishment): self
{
$this->establishment = $establishment;
return $this;
}
}
You may also need more options than the ones in the Repository file, in that case create a new repository class in your project. Don't forget to update the namspace used in your entity (see previous exemple).
To use the bundle, inject in your services the config repository and use one of the available methods. The bundle will check if the configuration exist otherwise a new configuration will be created.
The bundle offers support for configuration in the following formats :
- boolean
- string
- text
- integer
public function getBool($group, $label, bool $default): bool
public function setBool(string $group, string $label, bool $value): void
public function getString($group, $label, string $default): string
public function setString($group, $label, string $value): void
public function getText($group, $label, string $default): string
public function setText($group, $label, string $value): void
public function getInt($group, $label, string $default): int
{{ get_config_value('type', 'group', 'label', 'default') }}
A command allows you to initialise new configurations. We suggest to execute it everytime your app starts.
bin/console lle:config:warmup
To configure the default values, create a class that implements WarmupInterface.
<?php
namespace App\Warmup;
use Lle\ConfigBundle\Contracts\WarmupInterface;
use Lle\ConfigBundle\Repository\AbstractConfigRepository;
class ConfigWarmup implements WarmupInterface
{
public function warmup(AbstractConfigRepository $configRepository): void
{
$configRepository->initBool('CONFIG', 'active', true);
}
}
Do not use set*(), because it will overwrite defined configurations.