Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Config Bundle Laravel Package

2lenet/config-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. 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],
    ];
    
  2. 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
    }
    
  3. 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);
    }
    

Implementation Patterns

Core Workflows

  1. Configuration Management

    • Trait-Based Design: Extend ConfigTrait to leverage built-in methods like setKey(), setValue(), or getConfig().
    • Repository Pattern: Use ConfigRepository for CRUD operations (e.g., findByKey(), save()).
    • Example:
      // Load config
      $theme = $config->getConfig('app.theme');
      
      // Update config
      $config->setConfig(['app.theme' => 'light']);
      $entityManager->flush();
      
  2. Integration with CruditBundle

    • Pair with CruditBundle for admin UI:
      # config/packages/crudit.yaml
      crudit:
          resources:
              App\Entity\Config:
                  fields: [key, value]
                  edit: true
      
  3. Environment-Specific Configs

    • Use ConfigInterface to scope configs by environment (e.g., setEnvironment('prod')).

Best Practices

  • Namespace Isolation: Prefix keys (e.g., app., service.) to avoid collisions.
  • Caching: Cache fetched configs (e.g., via Symfony Cache component) to reduce DB calls:
    $cache = $this->get('cache.app');
    $config = $cache->get('config.app.theme', fn() => $configRepo->findByKey('app.theme'));
    

Gotchas and Tips

Pitfalls

  1. No Packagist Support

    • Workaround: Use GitHub URL in composer.json (as shown in README). Monitor for future Packagist updates.
  2. Limited Documentation

    • Debugging: Check ConfigTrait source for undocumented methods (e.g., isValid()).
    • Example: Override validate() in your entity to enforce custom rules:
      public function validate(): bool
      {
          return !empty($this->getKey()) && strlen($this->getValue()) <= 255;
      }
      
  3. Repository Assumptions

    • Gotcha: The bundled ConfigRepository assumes a key and value column. Customize via Doctrine extensions if your schema differs.

Extension Points

  1. Custom Validation

    • Extend ConfigInterface to add validation logic:
      public function isValid(): bool {
          return parent::isValid() && $this->getValue() === 'dark' || $this->getValue() === 'light';
      }
      
  2. Event Listeners

    • Hook into config changes via Symfony events (e.g., 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');
          }
      }
      
  3. Performance

    • Tip: Use DQL for bulk fetches:
      $configs = $configRepo->createQueryBuilder('c')
          ->where('c.key LIKE :prefix')
          ->setParameter('prefix', 'app.%')
          ->getQuery()
          ->getResult();
      

Debugging Tips

  • Doctrine Events: Enable SQL logging in config/packages/dev/doctrine.yaml:
    doctrine:
        dbal:
            logging: true
    
  • Trait Overrides: Use php artisan ide-helper:generate to resolve trait method conflicts in IDEs.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor