Installation:
devtime/raffler-bundle to composer.json under "require".composer update devtime/raffler-bundle.AppKernel.php.First Use Case:
config.yml under devtime_raffler:
devtime_raffler:
participants: ["Alice", "Bob", "Charlie"]
php app/console devtime:raffler:draw
{{ raffler.winner }}
Where to Look First:
src/Devtime/Bundle/RafflerBundle/Command/DrawCommand.php for CLI usage.src/Devtime/Bundle/RafflerBundle/Resources/config/services.yml for service configuration.src/Devtime/Bundle/RafflerBundle/DependencyInjection/Configuration.php for bundle settings.Participant Management:
ParticipantProvider service).devtime_raffler.participant_provider in services.yml:
devtime_raffler.participant_provider:
class: AppBundle\Service\CustomParticipantProvider
arguments: ["@doctrine.orm.entity_manager"]
Raffle Execution:
Raffler service:
$winner = $this->get('raffler')->draw();
CronBundle or TaskBundle.Frontend Integration:
Resources/public/js/) to display raffle results or participant lists.RafflerView to customize UI behavior.Event-Driven Raffles:
raffle.draw) to log or notify winners:
$dispatcher->addListener('raffle.draw', function($event) {
// Send email, log winner, etc.
});
Database Backing:
Use the Participant entity (src/Devtime/Bundle/RafflerBundle/Entity/Participant.php) as a base for your own entity, extending it with additional fields (e.g., email, priority).
Testing:
Mock the ParticipantProvider in PHPUnit tests:
$provider = $this->createMock('Devtime\Bundle\RafflerBundle\Provider\ParticipantProvider');
$provider->method('getParticipants')->willReturn(['Alice', 'Bob']);
$this->container->set('devtime_raffler.participant_provider', $provider);
Localization:
Override translation messages in Resources/translations/messages.en.yml for winner notifications or UI labels.
Backbone.js Dependencies:
{{ parent() }}
<script src="{{ asset('bundles/devtimeraffler/js/backbone.js') }}"></script>
<script src="{{ asset('bundles/devtimeraffler/js/raffler.js') }}"></script>
Randomness in Testing:
draw() method uses PHP’s mt_rand(). For deterministic tests, mock the Raffler service or use a fixed seed:
mt_srand(123); // Seed for testing
Configuration Overrides:
config.yml will ignore new settings. Run:
php app/console cache:clear
Participant Duplicates:
ParticipantProvider to deduplicate:
return array_unique($participants);
CLI Errors:
If devtime:raffler:draw fails, check:
count($participants) > 0).ParticipantProvider service is properly configured.Frontend Issues: Inspect the browser console for Backbone.js errors. Common fixes:
data-devtime-raffler attributes are correctly set on HTML elements.Custom Participant Logic:
Extend ParticipantProviderInterface to fetch participants from APIs or external systems:
class ApiParticipantProvider implements ParticipantProviderInterface {
public function getParticipants() {
return $this->httpClient->get('/api/participants')->json();
}
}
Winner Notifications:
Subclass Raffler to add post-draw actions:
class CustomRaffler extends Raffler {
protected function postDraw($winner) {
$this->mailer->sendWinnerEmail($winner);
}
}
Register it in services.yml:
devtime_raffler.raffler:
class: AppBundle\Service\CustomRaffler
Weighted Raffles:
Modify the draw() method to support weighted participants:
$weights = [0.5, 0.3, 0.2]; // 50%, 30%, 20% chance
$winner = $participants[array_rand($participants, 1, $weights)];
Multi-Winner Raffles:
Extend the bundle to draw multiple winners by updating the draw() method:
public function drawMultiple($count = 1) {
$winners = [];
for ($i = 0; $i < $count; $i++) {
$winners[] = $this->draw();
}
return $winners;
}
How can I help you explore Laravel packages today?