cosmologist/symfony-common-bundle
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],
];
First Use Case:
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.ExtraConnection wrapper in config/packages/doctrine.yaml:
doctrine:
dbal:
default_connection: default
connections:
default:
wrapper_class: Cosmologist\Bundle\SymfonyCommonBundle\Doctrine\ExtraConnection
# ... other config
PhpStorm Integration:
RunnerCommand to execute a method/class from the cursor position. Manually set breakpoints in Xdebug to inspect execution.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"
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.
Archived Status:
symfony/var-dumper or doctrine/doctrine-bundle for debugging/Doctrine features.PhpStorm Debugging Quirks:
RunnerCommand does not auto-stop at the specified line; manual breakpoints are required. This can be confusing for developers expecting seamless debugging.xdebug.start_with_request=trigger for more control.Doctrine Compatibility:
ExtraConnection wrapper may conflict with newer Doctrine DBAL versions (e.g., ^3.0). Test thoroughly with your stack.Event Naming:
cosmologist.post_commit are not standard Symfony events. Ensure listeners are properly tagged and registered.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.
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));
}
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
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'));
}
How can I help you explore Laravel packages today?