## Getting Started
### Minimal Setup
1. **Installation**:
```bash
composer require antwebes/badge-bundle
Register the bundle in config/bundles.php:
return [
// ...
Antwebes\BadgeBundle\AntwebesBadgeBundle::class => ['all' => true],
];
Database Configuration:
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
First Use Case:
UserBadge) extending Antwebes\BadgeBundle\Model\BadgeInterface:
use Antwebes\BadgeBundle\Model\BadgeInterface;
class UserBadge implements BadgeInterface
{
private $id;
private $name;
private $description;
private $icon;
// Implement BadgeInterface methods (getters/setters)
}
config/packages/antwebes_badge.yaml:
antwebes_badge:
badges:
user_badge:
class: App\Entity\UserBadge
requirements:
- { property: 'posts', operator: '>', value: 10 }
- { property: 'comments', operator: '>', value: 5 }
Check Badges for a User:
use Antwebes\BadgeBundle\Manager\BadgeManagerInterface;
$badgeManager = $this->container->get('antwebes_badge.manager');
$badges = $badgeManager->getBadgesForUser($user);
Badge Assignment Logic:
kernel.request) to check and assign badges dynamically:
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Antwebes\BadgeBundle\Event\BadgeCheckEvent;
$badgeManager->checkAndAssignBadges($user);
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class ProcessBadgesCommand extends Command
{
protected function execute(InputInterface $input, OutputInterface $output)
{
$badgeManager = $this->getContainer()->get('antwebes_badge.manager');
$badgeManager->processAllUsers();
}
}
Custom Requirements:
BadgeRequirement class to add custom logic:
use Antwebes\BadgeBundle\Model\BadgeRequirementInterface;
class CustomRequirement implements BadgeRequirementInterface
{
public function isSatisfied($user, $badge)
{
// Custom logic (e.g., API call, external service)
return $this->externalService->validate($user);
}
}
antwebes_badge.yaml:
antwebes_badge:
badges:
custom_badge:
requirements:
- { class: App\Badge\CustomRequirement }
Displaying Badges:
{% for badge in badges %}
<div class="badge">
<img src="{{ asset(badge.icon) }}" alt="{{ badge.name }}">
<span>{{ badge.name }}</span>
</div>
{% endfor %}
User entity to store awarded badges:
class User extends BaseUser
{
/** @ORM\ManyToMany(targetEntity="App\Entity\UserBadge") */
private $badges;
}
Badge Tiers:
antwebes_badge:
badges:
bronze:
requirements: [{ property: 'posts', operator: '>', value: 10 }]
silver:
requirements:
- { property: 'posts', operator: '>', value: 50 }
- { property: 'comments', operator: '>', value: 20 }
gold:
requirements:
- { property: 'posts', operator: '>', value: 100 }
- { property: 'comments', operator: '>', value: 50 }
Doctrine ORM Assumptions:
BadgeManager and BadgeRepository services.config/services.yaml:
services:
antwebes_badge.manager:
class: App\Badge\CustomBadgeManager
arguments: ['@antwebes_badge.repository.custom']
Requirement Property Access:
property keys to access user attributes. If the property is private or uses getters/setters, the bundle may fail silently.getPosts() instead of posts):
requirements:
- { property: 'getPosts', operator: '>', value: 10 }
Event Dispatching:
user.post_create):
use Antwebes\BadgeBundle\Event\BadgeCheckEvent;
public function onPostCreate(PostEvent $event)
{
$badgeManager->checkAndAssignBadges($event->getUser());
}
Performance:
$badgeManager->getBadgesForUser($user, true); // Cache results
Enable Debug Mode:
dev environment. Enable debug mode in config/packages/dev/antwebes_badge.yaml:
antwebes_badge:
debug: true
Check Requirement Validation:
$badgeManager->checkAndAssignBadges($user, true); // Verbose mode
Database Schema:
badge and user_badge tables. The bundle expects:
badge table with id, name, description, icon.user_badge junction table for awarded badges.Custom Storage:
Antwebes\BadgeBundle\Repository\BadgeRepositoryInterface for non-Doctrine backends (e.g., MongoDB, Redis).class RedisBadgeRepository implements BadgeRepositoryInterface
{
public function findByUser(UserInterface $user)
{
return $this->redis->hGetAll('badge:' . $user->getId());
}
}
Badge Events:
use Antwebes\BadgeBundle\Event\BadgeEvent;
$dispatcher->dispatch(new BadgeEvent($user, $badge, BadgeEvent::BADGE_AWARDED));
Badge UI Components:
{# templates/components/badge.html.twig #}
<div class="badge {{ badge.type }}">
<i class="{{ badge.icon }}"></i>
<span>{{ badge.name }}</span>
</div>
{% include 'components/badge.html.twig' with { badge: badge } %}
Badge Analytics:
$badgeManager->getBadgeStats('user_badge'); // Hypothetical method
$this->logger->info('Badge awarded', [
'user_id' => $user->getId(),
'badge' => $badge->getName(),
]);
Localization:
# config/packages/antwebes_badge.yaml
antwebes
How can I help you explore Laravel packages today?