dev-master
9999999-dev https://github.com/PROCERGS/login-cidadao-badges-templateTemplate project for the badges system.
MIT
The Requires
- php >=5.3.0
by Guilherme Donato
template badges login-cidadao
Template project for the badges system.
This project is a template/example for implementing badges compatible with login-cidadao., (*1)
First of all, generate a new Symfony 2 bundle and name it as you see fit. In this example we'll call it "Acme\BadgesBundle"., (*2)
Go on and start the interactive bundle generator:, (*3)
``` bash $ php app/console generate:bundle, (*4)
### Badge Model We'll need a Model class to represent your badges. This class can be as simple as the implementation of the `PROCERGS\LoginCidadao\BadgesBundle\Model\BadgeInterface`, thus we have: ```php <?php namespace Acme\BadgesBundle\Model; use PROCERGS\LoginCidadao\BadgesBundle\Model\BadgeInterface; class AcmeBadge implements BadgeInterface { protected $namespace; protected $name; protected $data; public function __construct($namespace, $name, $data = null) { $this->namespace = $namespace; $this->name = $name; $this->data = $data; } public function getData() { return $this->data; } public function getName() { return $this->name; } public function getNamespace() { return $this->namespace; } }
Now that we can represent our badge, let's create the class that will respond the Badge System's events.
Luckly all the boring stuff is already done by the PROCERGS\LoginCidadao\BadgesBundle\Model\AbstractBadgesEventSubscriber
abstract class. All we have to do is implement the missing methods and handle the event as needed to validate our badges., (*5)
The badge validation is done in the onBadgeEvaluate(EvaluateBadgesEvent)
method and that's where the hard work should be done. This is defined in the AbstractBadgesEventSubscriber
., (*6)
We also need the __construct()
method that will tell the application what badges are provided by this bundle. In this example we won't use translation but you could simply inject the @translator
service into this class and translate the messages in this method.
Also in the constructor you have to setup your badge's "namespace". In this example we used acme_badges
., (*7)
``` php <?php, (*8)
namespace Acme\BadgesBundle\Event;, (*9)
use PROCERGS\LoginCidadao\BadgesBundle\Model\AbstractBadgesEventSubscriber; use PROCERGS\LoginCidadao\BadgesBundle\Event\EvaluateBadgesEvent; use Acme\BadgesBundle\Model\AcmeBadge;, (*10)
class BadgesSubscriber extends AbstractBadgesEventSubscriber {, (*11)
public function __construct() { $this->registerBadge('random_even', 'A person with this badge had the luck of getting an even number in rand(1,10)'); $this->setName('acme_badges'); } public function onBadgeEvaluate(EvaluateBadgesEvent $event) { if (rand(1,10) % 2 === 0) { $event->registerBadge(new AcmeBadge($this->getName(), 'random_even', true)); } }
}, (*12)
### Almost done: services.yml Now all we have to do is tell the application we exist by configuring the bundle's services.yml like so: ``` yaml parameters: acme_badges.subscriber.class: Acme\BadgesBundle\Event\BadgesSubscriber services: acme_badges.subscriber: class: %acme_badges.subscriber.class% tags: - { name: kernel.event_subscriber }
This tells the application that the service acme_badges.subscriber
is an event subscriber so it'll get notified when one of it's subscribed events are triggered., (*13)
Assuming your bundle is correctly added in app/AppKernel.php
, all your badges will be automatically detected, no extra configuration needed., (*14)
If you wish to trigger the badge evaluation event manually you can:, (*15)
``` php use PROCERGS\LoginCidadao\BadgesBundle\BadgesEvents; use PROCERGS\LoginCidadao\BadgesBundle\Event\EvaluateBadgesEvent; use PROCERGS\LoginCidadao\BadgesBundle\Event\ListBadgesEvent;, (*16)
/** @var \PROCERGS\LoginCidadao\CoresBundle\Model\PersonInterface **/ $user = $this->getUser();, (*17)
/** @var \PROCERGS\LoginCidadao\BadgesBundle\Handler\BadgesHandler **/ $badgesHandler = $this->get('badges.handler');, (*18)
$badgesHandler->evaluate($user); var_dump($user->getBadges()); // this may or may not return the badge we created here since it's based on random stuff., (*19)
$badges = $badgesHandler->getAvailableBadges(); var_dump($badges); // this will list all available badges ```, (*20)
Template project for the badges system.
MIT
template badges login-cidadao