tourze / counter-bundle
Symfony Counter Bundle
Installs: 16
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: ^8.1
- doctrine/common: ^3.5
- doctrine/dbal: ^3.7.0 || ^4.0
- doctrine/doctrine-bundle: ^2.13
- doctrine/orm: ^2.20 || ^3.0
- doctrine/persistence: ^3.1 || ^4
- nesbot/carbon: ^2.72 || ^3
- psr/log: ^3|^2|^1
- symfony/config: ^6.4
- symfony/console: ^6.4
- symfony/dependency-injection: ^6.4
- symfony/framework-bundle: ^6.4
- symfony/http-kernel: ^6.4
- symfony/service-contracts: ^2.5 || ^3
- symfony/yaml: ^6.4 || ^7.1
- tourze/doctrine-indexed-bundle: 0.0.*
- tourze/doctrine-timestamp-bundle: 0.0.*
- tourze/easy-admin-attribute: 0.0.*
- tourze/symfony-cron-job-bundle: 0.0.*
- tourze/symfony-lock-command-bundle: 0.0.*
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2025-04-17 13:33:37 UTC
README
A Symfony bundle that provides a complete entity counting system with features like automatic statistics, manual increment/decrement, and scheduled updates.
Features
- Automatic entity counting with performance optimization for large tables
- Manual counter management with increment/decrement support
- Scheduled counter updates via cron jobs
- Context-aware counter tracking
- Easy integration with Symfony's admin interface
- Support for custom counter providers
Installation
composer require tourze/counter-bundle
Quick Start
- Register the bundle in
config/bundles.php
:
return [ // ... CounterBundle\CounterBundle::class => ['all' => true], ];
- Create a custom counter provider:
use CounterBundle\Provider\CounterProvider; use CounterBundle\Entity\Counter; #[AutoconfigureTag('app.counter.provider')] class CustomCounterProvider implements CounterProvider { public function getCounters(): iterable { yield new Counter('users.total', 'Total Users'); yield new Counter('orders.total', 'Total Orders'); } }
- Use the counter in your service:
use CounterBundle\Repository\CounterRepository; class YourService { public function __construct( private readonly CounterRepository $counterRepository ) {} public function getStatistics(): array { return [ 'users' => $this->counterRepository->findOneBy(['name' => 'users.total'])?->getCount() ?? 0, 'orders' => $this->counterRepository->findOneBy(['name' => 'orders.total'])?->getCount() ?? 0, ]; } }
Advanced Features
Automatic Entity Counting
The bundle automatically tracks entity counts through Doctrine events:
use CounterBundle\Provider\EntityTotalCountProvider; class YourEntity { // Your entity definition } // The counter will be automatically updated on entity creation/deletion
Performance Optimization
For large tables (>1M records), the bundle uses information_schema
for count estimation:
// Automatically switches to estimation for large tables $counter = $counterRepository->findOneBy(['name' => 'large.table.total']);
Scheduled Updates
Counter values are automatically updated via a cron job:
// Runs every hour at minute 30 #[AsCronTask('30 * * * *')] #[AsCommand(name: 'counter:refresh-counter')] class RefreshCounterCommand extends LockableCommand { // Command implementation }
Contributing
Please see CONTRIBUTING.md for details.
License
The MIT License (MIT). Please see License File for more information.