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

Symfony Common Bundle Laravel Package

cosmologist/symfony-common-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation: Add the bundle via Composer (if still available):

    composer require cosmologist/symfony-common-bundle
    

    Register the bundle in config/bundles.php:

    return [
        // ...
        Cosmologist\Bundle\SymfonyCommonBundle\SymfonyCommonBundle::class => ['all' => true],
    ];
    
  2. First Use Case:

    • Debugging: Use the RunnerCommand for quick debugging in PhpStorm by setting up an external tool (as per README). Test by running it on a method/class under a breakpoint.
    • Doctrine Integration: Enable the ExtraConnection wrapper in config/packages/doctrine.yaml:
      doctrine:
          dbal:
              default_connection: default
              connections:
                  default:
                      wrapper_class: Cosmologist\Bundle\SymfonyCommonBundle\Doctrine\ExtraConnection
                      # ... other config
      

Implementation Patterns

Debugging Workflow

  • PhpStorm Integration:

    • Configure the external tool as described in the README.
    • Use RunnerCommand to execute a method/class from the cursor position. Manually set breakpoints in Xdebug to inspect execution.
    • Ideal for rapid iteration during development (e.g., testing repository methods, services, or controllers).
  • Command-Line Usage: Run the command directly via CLI:

    php bin/console symfony-common:runner <FullyQualifiedClassName> <MethodName> [--args="..."]
    

    Example:

    php bin/console symfony-common:runner App\\Service\\UserService getUserById --args="1"
    

Doctrine Integration Patterns

  • Transaction Hooks: Leverage postBeginTransaction, postCommit, and postRollback events for side effects (e.g., logging, analytics, or caching). Example listener:

    use Cosmologist\Bundle\SymfonyCommonBundle\Event\PostCommitEvent;
    
    public function onPostCommit(PostCommitEvent $event)
    {
        $this->logger->info('Transaction committed for connection: ' . $event->getConnection()->getDatabase());
    }
    

    Register the listener in services.yaml:

    services:
        App\EventListener\TransactionListener:
            tags:
                - { name: kernel.event_listener, event: cosmologist.post_commit, method: onPostCommit }
    
  • Query Helper Methods: Use Connection::fetchAllIndexed for simplified query results (e.g., pivot tables, multi-column lookups). Example:

    $results = $connection->fetchAllIndexed('SELECT * FROM user_roles WHERE user_id = ?', [1], 'role_id');
    // Returns array with role_id as keys: ['admin' => [...], 'user' => [...]]
    
  • Custom DBAL Events: Extend the ExtraConnection class to add domain-specific events or methods:

    class CustomConnection extends ExtraConnection
    {
        public function batchInsert($table, array $data, $batchSize = 100)
        {
            // Custom logic
        }
    }
    

    Update doctrine.yaml to use the custom class.


Gotchas and Tips

Pitfalls

  1. Archived Status:

    • The package is archived (last release in 2018) and lacks active maintenance. Use with caution in production; prefer modern alternatives like symfony/var-dumper or doctrine/doctrine-bundle for debugging/Doctrine features.
    • Mitigation: Fork the repo or wrap functionality in a custom bundle for long-term use.
  2. PhpStorm Debugging Quirks:

    • The RunnerCommand does not auto-stop at the specified line; manual breakpoints are required. This can be confusing for developers expecting seamless debugging.
    • Tip: Combine with Xdebug’s xdebug.start_with_request=trigger for more control.
  3. Doctrine Compatibility:

    • The ExtraConnection wrapper may conflict with newer Doctrine DBAL versions (e.g., ^3.0). Test thoroughly with your stack.
    • Tip: Override methods in a custom connection class to adapt to breaking changes.
  4. Event Naming:

    • Events like cosmologist.post_commit are not standard Symfony events. Ensure listeners are properly tagged and registered.

Debugging Tips

  • Log Events: Add logging to transaction events to verify they fire:

    public function onPostCommit(PostCommitEvent $event)
    {
        error_log('PostCommit triggered for: ' . $event->getConnection()->getWrappedConnection()->getDatabase());
    }
    
  • Query Debugging: Use fetchAllIndexed sparingly in production (performance overhead). Prefer Doctrine repositories or QueryBuilder for complex queries.

Extension Points

  1. Custom Events: Extend the bundle’s event system by adding new events to ExtraConnection:

    // In a custom connection class
    public function postSave(EntityManager $em, Entity $entity)
    {
        $this->dispatchEvent(new PostSaveEvent($em, $entity));
    }
    
  2. Twig Extensions: The README mentions "Twig etc." but provides no details. Inspect the bundle for Twig\Extension classes or add your own:

    # config/packages/twig.yaml
    twig:
        extensions:
            - App\Twig\CustomExtension
    
  3. Command Enhancements: Extend RunnerCommand to support more use cases (e.g., testing private methods, static calls):

    // Override execute() in a custom command
    public function execute(InputInterface $input, OutputInterface $output)
    {
        $class = $input->getArgument('class');
        $method = $input->getArgument('method');
        $reflection = new \ReflectionClass($class);
        $method = $reflection->getMethod($method);
        $method->invokeArgs($reflection->newInstance(), $input->getArgument('args'));
    }
    
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