2lenet / config-bundle
Config Bundle
Installs: 3 999
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- 2lenet/crudit-bundle: *
- symfony/framework-bundle: ^6.0|^7.0
- symfony/orm-pack: *
- twig/twig: ^3.4
Requires (Dev)
- ergebnis/phpstan-rules: ^2.0
- phpstan/phpstan: ^1.9
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-doctrine: ^1.3
- phpstan/phpstan-mockery: ^1.1
- phpstan/phpstan-phpunit: ^1.3
- phpstan/phpstan-strict-rules: ^1.5
- phpstan/phpstan-symfony: ^1.2
- phpunit/phpunit: ^10.0
- slevomat/coding-standard: ^8.0
- squizlabs/php_codesniffer: ^3.5
- dev-main
- 1.2.5
- 1.2.4
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.0
- 1.0.2
- 1.0.1
- 1.0.0
- dev-sebheitzmann-patch-1
- dev-feature/cache-multitenant
- dev-config_cache
- dev-config_warmup
- dev-ModifController
- dev-sf-7
- dev-majSfInsight
- dev-AddService
- dev-label_traductible
- dev-feature/add-twig-function
- dev-edit_in_place
- dev-initBundle
This package is auto-updated.
Last update: 2024-11-18 14:47:54 UTC
README
Symfony bundle that gives you an easy configuration for your app. Perfect to use with the famous CRUD Crudit
Installation
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!
Customization
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).
Usage
General overview
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.
Supported configurations
The bundle offers support for configuration in the following formats :
- boolean
- string
- text
- integer
Available methods
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
Twig integration
{{ get_config_value('type', 'group', 'label', 'default') }}
Initialise new configurations (Warm-up)
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.