Now injecting services with JMSDiExtraBundle

Signed-off-by: Gergely POLONKAI <polesz@w00d5t0ck.info>
This commit is contained in:
Gergely POLONKAI 2012-08-16 19:06:00 +02:00
parent cd361a8ca9
commit 1d0a513ea2
9 changed files with 123 additions and 84 deletions

View File

@ -2,6 +2,12 @@ imports:
- { resource: parameters.yml } - { resource: parameters.yml }
- { resource: security.yml } - { resource: security.yml }
jms_di_extra:
locations:
all_bundles: false
bundles: [ KekRozsakFrontBundle, KekRozsakSecurityBundle ]
directories: [ "%kernel.root_dir%/../src" ]
framework: framework:
#esi: ~ #esi: ~
#translator: { fallback: %locale% } #translator: { fallback: %locale% }

View File

@ -5,7 +5,12 @@ use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView; use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use JMS\DiExtraBundle\Annotation as DI;
/**
* @DI\Service("form.type_extension.help_message")
* @DI\Tag("form.type_extension", attributes={"alias" = "field"})
*/
class HelpMessageTypeExtension extends AbstractTypeExtension class HelpMessageTypeExtension extends AbstractTypeExtension
{ {
public function buildForm(FormBuilderInterface $builder, array $options) public function buildForm(FormBuilderInterface $builder, array $options)

View File

@ -3,18 +3,4 @@
<container xmlns="http://symfony.com/schema/dic/services" <container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="kek_rozsak_front.twig_extension.events" class="KekRozsak\FrontBundle\Twig\EventsExtension">
<argument type="service" id="doctrine" />
<argument type="service" id="security.context" />
<tag name="twig.extension" />
</service>
<service id="form.type_extension.help_message" class="KekRozsak\FrontBundle\Form\Extension\HelpMessageTypeExtension">
<tag name="form.type_extension" alias="field" />
</service>
<service id="bb.twig.extension" class="KekRozsak\FrontBundle\Twig\TwigBBExtension">
<argument type="service" id="service_container" />
<tag name="twig.extension" />
</service>
</services>
</container> </container>

View File

@ -4,64 +4,78 @@ namespace KekRozsak\FrontBundle\Twig;
use Symfony\Bridge\Doctrine\RegistryInterface; use Symfony\Bridge\Doctrine\RegistryInterface;
use Symfony\Component\Security\Core\SecurityContextInterface; use Symfony\Component\Security\Core\SecurityContextInterface;
use JMS\DiExtraBundle\Annotation as DI;
/**
* @DI\Service
* @DI\Tag("twig.extension")
*/
class EventsExtension extends \Twig_Extension class EventsExtension extends \Twig_Extension
{ {
protected $_doctrine; protected $_doctrine;
protected $_securityContext; protected $_securityContext;
public function __construct(RegistryInterface $doctrine, SecurityContextInterface $securityContext) /**
{ * @DI\InjectParams({
$this->_doctrine = $doctrine; * "doctrine" = @DI\Inject("doctrine"),
$this->_securityContext = $securityContext; * "securityContext" = @DI\Inject("security.context")
} * })
*
* @param \Symfony\Bridge\Doctrine\RegistryInterface $doctrine
* @param \Symfony\Component\Security\Core\SecurityContextInterface $securityContext
*/
public function __construct(RegistryInterface $doctrine, SecurityContextInterface $securityContext)
{
$this->_doctrine = $doctrine;
$this->_securityContext = $securityContext;
}
public function getGlobals() public function getGlobals()
{ {
$today = new \DateTime('now'); $today = new \DateTime('now');
$firstDay = \DateTime::createFromFormat('Y-m-d', $today->format('Y-m-01')); $firstDay = \DateTime::createFromFormat('Y-m-d', $today->format('Y-m-01'));
$firstDayWeekday = $firstDay->format('N'); $firstDayWeekday = $firstDay->format('N');
$numDays = $firstDay->format('t'); $numDays = $firstDay->format('t');
$lastDay = \DateTime::createFromFormat('Y-m-d', $today->format('Y-m-' . sprintf("%02d", $numDays))); $lastDay = \DateTime::createFromFormat('Y-m-d', $today->format('Y-m-' . sprintf("%02d", $numDays)));
/* /*
* Get all events in today's month. Iterate through this * Get all events in today's month. Iterate through this
* collection, adding each element to $monthEvents array's * collection, adding each element to $monthEvents array's
* 'day'th element array. * 'day'th element array.
*/ */
$query = $this->_doctrine->getEntityManager()->createQuery('SELECT e FROM KekRozsakFrontBundle:Event e WHERE e.cancelled = FALSE AND ((e.startDate < :firstDay AND e.endDate >= :firstDay) OR e.startDate BETWEEN :firstDay AND :lastDay)'); $query = $this->_doctrine->getEntityManager()->createQuery('SELECT e FROM KekRozsakFrontBundle:Event e WHERE e.cancelled = FALSE AND ((e.startDate < :firstDay AND e.endDate >= :firstDay) OR e.startDate BETWEEN :firstDay AND :lastDay)');
$query->setParameter('firstDay', $firstDay, \Doctrine\DBAL\Types\Type::DATE); $query->setParameter('firstDay', $firstDay, \Doctrine\DBAL\Types\Type::DATE);
$query->setParameter('lastDay', $lastDay, \Doctrine\DBAL\Types\Type::DATE); $query->setParameter('lastDay', $lastDay, \Doctrine\DBAL\Types\Type::DATE);
$events = $query->getResult(); $events = $query->getResult();
$eventList = array(); $eventList = array();
for ($i = 1; $i <= $numDays; $i++) for ($i = 1; $i <= $numDays; $i++) {
{ $date = \DateTime::createFromFormat(
$date = \DateTime::createFromFormat('Y-m-d', $today->format('Y-m-' . sprintf('%02d', $i))); 'Y-m-d',
$eventList[$i]['date'] = $date; $today->format('Y-m-' . sprintf('%02d', $i))
$eventList[$i]['events'] = array(); );
foreach ($events as $event) $eventList[$i]['date'] = $date;
{ $eventList[$i]['events'] = array();
if ($event->isOnDate($date)) foreach ($events as $event) {
{ if ($event->isOnDate($date)) {
$eventList[$i]['events'][] = $event; $eventList[$i]['events'][] = $event;
} }
} }
} }
return array( return array(
'events' => $events, 'events' => $events,
'eventList' => $eventList, 'eventList' => $eventList,
'today' => $today, 'today' => $today,
'firstDay' => $firstDay, 'firstDay' => $firstDay,
'lastDay' => $lastDay, 'lastDay' => $lastDay,
'firstDayWeekday' => $firstDayWeekday, 'firstDayWeekday' => $firstDayWeekday,
'numDays' => $numDays, 'numDays' => $numDays,
); );
} }
public function getName() public function getName()
{ {
return 'Events'; return 'Events';
} }
} }

View File

@ -2,15 +2,26 @@
namespace KekRozsak\FrontBundle\Twig; namespace KekRozsak\FrontBundle\Twig;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
use JMS\DiExtraBundle\Annotation as DI;
/**
* @DI\Service
* @DI\Tag("twig.extension")
*
*/
class TwigBBExtension extends \Twig_Extension class TwigBBExtension extends \Twig_Extension
{ {
private $container;
private $assets; private $assets;
/**
* @DI\InjectParams({
* "container" = @DI\Inject("service_container")
* })
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
*/
public function __construct(ContainerInterface $container) public function __construct(ContainerInterface $container)
{ {
$this->container = $container; $this->assets = $container->get('templating.helper.assets');
} }
public function getFilters() public function getFilters()
@ -67,8 +78,7 @@ class TwigBBExtension extends \Twig_Extension
$sentence, $sentence,
'<img src="' '<img src="'
. $this . $this
->container ->assets
->get('templating.helper.assets')
->getUrl( ->getUrl(
'upload/images/' 'upload/images/'
. (($ns == '') ? '' : $ns . '/') . (($ns == '') ? '' : $ns . '/')

View File

@ -3,17 +3,4 @@
<container xmlns="http://symfony.com/schema/dic/services" <container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="kek_rozsak_security.encoder.crypt" class="KekRozsak\SecurityBundle\Service\CryptEncoder">
</service>
<service id="kek_rozsak_security.auth.success" class="KekRozsak\SecurityBundle\Security\AuthSuccess">
<argument type="service" id="doctrine" />
<tag name="kernel.event_listener" event="security.authentication.success" />
</service>
<service id="kek_rozsak_security.twig_extension.userdataspan" class="KekRozsak\SecurityBundle\Twig\UserDataSpanExtension">
<argument type="service" id="router" />
<argument type="service" id="security.context" />
<tag name="twig.extension" />
</service>
</services>
</container> </container>

View File

@ -6,11 +6,26 @@ use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Bridge\Doctrine\RegistryInterface; use Symfony\Bridge\Doctrine\RegistryInterface;
use Symfony\Component\Security\Core\Event\AuthenticationEvent; use Symfony\Component\Security\Core\Event\AuthenticationEvent;
use JMS\DiExtraBundle\Annotation as DI;
/**
* @DI\Service
* @DI\Tag("kernel.event_listener", attributes={"event" = "security.authentication.success"})
*/
class AuthSuccess implements AuthenticationSuccessHandlerInterface class AuthSuccess implements AuthenticationSuccessHandlerInterface
{ {
/**
* The Doctrine interface
*
* @var Symfony\Bridge\Doctrine\RegistryInterface $doctrine
*/
private $doctrine; private $doctrine;
/**
* @DI\InjectParams
*
* @param \Symfony\Bridge\Doctrine\RegistryInterface $doctrine
*/
public function __construct(RegistryInterface $doctrine) public function __construct(RegistryInterface $doctrine)
{ {
$this->doctrine = $doctrine; $this->doctrine = $doctrine;
@ -34,4 +49,3 @@ class AuthSuccess implements AuthenticationSuccessHandlerInterface
$em->flush(); $em->flush();
} }
} }

View File

@ -2,7 +2,11 @@
namespace KekRozsak\SecurityBundle\Service; namespace KekRozsak\SecurityBundle\Service;
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface; use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
use JMS\DiExtraBundle\Annotation as DI;
/**
* @DI\Service("kek_rozsak_security.encoder.crypt")
*/
class CryptEncoder implements PasswordEncoderInterface class CryptEncoder implements PasswordEncoderInterface
{ {
function encodePassword($raw, $salt) function encodePassword($raw, $salt)
@ -15,4 +19,3 @@ class CryptEncoder implements PasswordEncoderInterface
return (crypt($raw, $salt) == $encoded); return (crypt($raw, $salt) == $encoded);
} }
} }

View File

@ -4,14 +4,28 @@ namespace KekRozsak\SecurityBundle\Twig;
use Symfony\Component\Security\Core\SecurityContextInterface; use Symfony\Component\Security\Core\SecurityContextInterface;
use Symfony\Bundle\FrameworkBundle\Routing\Router; use Symfony\Bundle\FrameworkBundle\Routing\Router;
use JMS\DiExtraBundle\Annotation as DI;
use KekRozsak\SecurityBundle\Entity\User; use KekRozsak\SecurityBundle\Entity\User;
/**
* @DI\Service
* @DI\Tag("twig.extension")
*/
class UserDataSpanExtension extends \Twig_Extension class UserDataSpanExtension extends \Twig_Extension
{ {
protected $_securityContext; protected $_securityContext;
protected $_router; protected $_router;
/**
* @DI\InjectParams({
* "router" = @DI\Inject("router"),
* "security" = @DI\Inject("security.context")
* })
*
* @param \Symfony\Bundle\FrameworkBundle\Routing\Router $router
* @param \Symfony\Component\Security\Core\SecurityContextInterface $security
*/
public function __construct(Router $router, SecurityContextInterface $security) public function __construct(Router $router, SecurityContextInterface $security)
{ {
$this->_router = $router; $this->_router = $router;