Installation
composer require gnuckorg/dastat-bundle
Enable the bundle in config/bundles.php:
return [
// ...
Gnuckorg\DaStatBundle\DaStatBundle::class => ['all' => true],
];
First Use Case: Basic Statistic
Define a simple statistic loader in src/Statistic/Loader/MyStatisticLoader.php:
namespace App\Statistic\Loader;
use Gnuckorg\DaStatBundle\Loader\LoaderInterface;
class MyStatisticLoader implements LoaderInterface
{
public function load(): array
{
return [
'total_users' => 100,
'active_users' => 50,
];
}
}
Register the Loader
Add the loader to config/packages/dastat.yaml:
dastat:
loaders:
- App\Statistic\Loader\MyStatisticLoader
Render the Statistic
Use the statistic Twig function in a template:
{% statistic('total_users') %}
Data Loading Patterns
public function load(): array
{
return $this->entityManager->getRepository(User::class)
->createQueryBuilder('u')
->select('COUNT(u.id)')
->getQuery()
->getSingleScalarResult();
}
public function load(): array
{
$response = $this->httpClient->request('GET', 'https://api.example.com/stats');
return json_decode($response->getContent(), true);
}
public function load(): array
{
return $this->cache->get('stats_key', function () {
return $this->fetchExpensiveData();
});
}
Rendering Patterns
{% statistic('total_orders', 'default') %}
{% statistic('active_users', 'active_users_template') %}
templates/dastat/.Dependency Injection
public function __construct(private EntityManagerInterface $em, private HttpClientInterface $http)
{
}
Tagging Loaders
tags to dynamically register loaders (e.g., for per-environment stats).
# config/services.yaml
services:
App\Statistic\Loader\DevStatLoader:
tags: ['dastat.loader']
kernel.terminate for periodic updates).
public function onKernelTerminate(KernelEvents::TERMINATE, Event $event)
{
$this->statisticManager->updateAll();
}
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class RefreshStatsCommand extends Command
{
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->statisticManager->updateAll();
$output->writeln('Stats refreshed!');
return Command::SUCCESS;
}
}
# config/packages/security.yaml
access_control:
- { path: ^/admin/stats, roles: ROLE_ADMIN }
Loader Naming Collisions
App\Statistic\Loader\).Circular Dependencies
Performance Overhead
Twig Template Overrides
templates/dastat/ and named correctly (e.g., active_users.html.twig).Configuration Merging
dastat.yaml. Overrides may not behave as expected if the bundle’s default config isn’t accounted for.Loader Errors
dastat.log channel (configured in config/packages/monolog.yaml) for loader-specific errors.
monolog:
channels: ['dastat']
Missing Stats
dastat.yaml and tagged as dastat.loader if using dynamic registration.php bin/console cache:clear
Twig Rendering Issues
DaStatBundle Twig extension is enabled (it should be by default).php bin/console debug:twig
Modular Design
UserStatsLoader, OrderStatsLoader) for better maintainability.Type Safety
public function load(): array {
return [...];
}
Testing
$loader = $this->createMock(MyStatisticLoader::class);
$loader->method('load')->willReturn(['test' => 123]);
$this->container->set(MyStatisticLoader::class, $loader);
Documentation
/**
* Loads user-related statistics.
* @return array{
* total: int,
* active: int,
* registered_today: int
* }
*/
public function load(): array { ... }
Extending the Bundle
config/services.yaml to customize behavior.
services:
Gnuckorg\DaStatBundle\Manager\StatisticManager:
arguments:
$cachePool: '@cache.app'
Environment-Specific Stats
# config/packages/dastat.yaml
dastat:
enabled_loaders: '%kernel.environment%::dev' ? ['DevStatLoader'] : ['ProdStatLoader']
How can I help you explore Laravel packages today?