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

My Test Bundle Laravel Package

dennisberg91/my-test-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require dennisberg91/my-test-bundle
    

    Add the bundle to your config/bundles.php:

    return [
        // ...
        Dennisberg91\TestBundle\Dennisberg91TestBundle::class => ['all' => true],
    ];
    
  2. First Use Case: Locate the MessageBuilder service in your container. Since autowiring is broken, manually define it in config/services.yaml:

    services:
        Dennisberg91\TestBundle\Service\MessageBuilder:
            arguments: ['@some_dependency']
    
  3. Verify: Inject MessageBuilder into a controller or command to confirm functionality:

    use Dennisberg91\TestBundle\Service\MessageBuilder;
    
    class SomeController extends AbstractController
    {
        public function __construct(private MessageBuilder $messageBuilder) {}
    }
    

Implementation Patterns

Service Configuration

  1. Autowiring Fix:

    • Why it fails: The MessageBuilder lacks a default constructor or proper type-hinted dependencies in its class definition.
    • Solution: Update the class to include a constructor with type-hinted dependencies:
      class MessageBuilder
      {
          public function __construct(private SomeDependency $dependency) {}
      }
      
    • Alternative: Use autoconfigure: false in services.yaml and manually configure:
      services:
          Dennisberg91\TestBundle\Service\MessageBuilder:
              autoconfigure: false
              arguments:
                  $dependency: '@some_dependency'
      
  2. Environment Variables:

    • Use Symfony’s %env() syntax in config/packages/dennisberg91_test.yaml:
      parameters:
          message_builder.prefix: '%env(MESSAGE_BUILDER_PREFIX)%'
      
    • Inject parameters into MessageBuilder:
      class MessageBuilder
      {
          public function __construct(private string $prefix) {}
      }
      
    • Load variables via .env:
      MESSAGE_BUILDER_PREFIX=Hello
      
  3. Dependency Injection Workflow:

    • Step 1: Define configuration in config/packages/dennisberg91_test.yaml.
    • Step 2: Extend the bundle’s configuration class (Dennisberg91TestExtension) to merge custom parameters.
    • Step 3: Use the compile() method to validate/process parameters.
  4. Integration with Laravel:

    • Symfony Bridge: Use symfony/var-dumper and symfony/dependency-injection for compatibility.
    • Service Provider: Register the bundle in config/app.php under providers:
      Dennisberg91\TestBundle\Dennisberg91TestBundle::class,
      
    • Environment Variables: Load via Laravel’s .env and access with config('dennisberg91_test.prefix').

Gotchas and Tips

Pitfalls

  1. Autowiring Limitations:

    • Issue: If MessageBuilder has private/protected properties without setters, autowiring fails.
    • Fix: Use @Assert\Assert annotations or constructor injection for immutable dependencies.
  2. Environment Variable Scope:

    • Issue: Variables defined in .env may not propagate to all environments (e.g., test vs. prod).
    • Fix: Use Symfony’s ParameterBag or Laravel’s config() helper with environment-aware defaults:
      $prefix = config('dennisberg91_test.prefix', env('MESSAGE_BUILDER_PREFIX', 'Default'));
      
  3. Bundle Overrides:

    • Issue: Custom configurations in config/packages/ may be overridden by later bundle loads.
    • Fix: Use prepend: true in bundles.php or extend the bundle’s configuration class.
  4. Testing Quirks:

    • Issue: Unit tests in bundles must mock Symfony services (e.g., ContainerInterface).
    • Fix: Use PHPUnit\Framework\TestCase and symfony/service-test for container-aware tests.

Debugging

  1. Service Dumping:

    • Dump the container to inspect services:
      php bin/console debug:container MessageBuilder
      
    • Laravel equivalent:
      php artisan container:dump
      
  2. Configuration Validation:

    • Validate YAML syntax with:
      php bin/console config:validate
      
    • Laravel: Use config:clear after changes.
  3. Dependency Graph:

    • Visualize dependencies with:
      php bin/console debug:container --format=graphviz
      

Extension Points

  1. Customizing MessageBuilder:

    • Strategy: Create a decorator or compiler pass to modify behavior:
      class CustomMessageBuilderDecorator implements MessageBuilderInterface
      {
          public function __construct(private MessageBuilder $decorated) {}
      }
      
    • Register in services.yaml:
      services:
          Dennisberg91\TestBundle\Service\MessageBuilder:
              decorates: 'dennisberg91_test.message_builder'
      
  2. Adding Commands:

    • Extend the bundle by creating a custom command in src/Command/ and tag it in services.yaml:
      tags: ['console.command']
      
  3. Event Listeners:

    • Attach listeners to bundle events (e.g., KernelEvents::CONTROLLER):
      use Symfony\Component\HttpKernel\Event\ControllerEvent;
      use Symfony\Component\HttpKernel\KernelEvents;
      
      $dispatcher->addListener(KernelEvents::CONTROLLER, function (ControllerEvent $event) {
          // Logic here
      });
      

Testing Tips

  1. Test Location:

    • Place unit tests in tests/Unit/ (Laravel) or tests/Unit/ (Symfony) with the bundle namespace:
      tests/Unit/Dennisberg91/TestBundle/Service/
      
  2. Test Example:

    • MessageBuilder Test:
      namespace Tests\Unit\Dennisberg91\TestBundle\Service;
      
      use Dennisberg91\TestBundle\Service\MessageBuilder;
      use PHPUnit\Framework\TestCase;
      
      class MessageBuilderTest extends TestCase
      {
          public function testBuildMessage()
          {
              $builder = new MessageBuilder('Hello');
              $this->assertEquals('Hello World', $builder->build('World'));
          }
      }
      
    • Integration Test (Symfony):
      use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
      
      class MessageBuilderIntegrationTest extends KernelTestCase
      {
          public function testMessageBuilderInContainer()
          {
              self::bootKernel();
              $builder = self::$kernel->getContainer()->get(MessageBuilder::class);
              $this->assertInstanceOf(MessageBuilder::class, $builder);
          }
      }
      
  3. Mocking:

    • Use createMock() for external dependencies:
      $dependency = $this->createMock(SomeDependency::class);
      $builder = new MessageBuilder($dependency);
      
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.
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours