Skip to content

Instantly share code, notes, and snippets.

@mazenovi
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mazenovi/9858621 to your computer and use it in GitHub Desktop.
Save mazenovi/9858621 to your computer and use it in GitHub Desktop.
<service id="vich_uploader.listener.uploader.propel" class="Vich\UploaderBundle\EventListener\PropelUploaderListener">
<tag name="propel.event_subscriber" class="Cerdi\KeepContactBundle\Model\Offer"/>
<tag name="propel.event_subscriber" class="Cerdi\KeepContactBundle\Model\Internship"/>
<tag name="propel.event_subscriber" class="Cerdi\KeepContactBundle\Model\Thesis"/>
<tag name="propel.event_subscriber" class="Cerdi\KeepContactBundle\Model\Person"/>
<tag name="propel.event_subscriber" class="Cerdi\KeepContactBundle\Model\Organization"/>
<tag name="propel.event_subscriber" class="Cerdi\KeepContactBundle\Model\Document"/>
<argument type="service" id="vich_uploader.adapter"/>
<argument type="service" id="vich_uploader.upload_handler"/>
</service>
...
<service id="bazinga.propel_event_dispatcher.dispatcher.cerdi_keepcontactbundle_model_organization" class="Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher">
<tag name="propel.event_dispatcher"/>
<argument type="service" id="service_container"/>
<call method="addSubscriberService">
<argument>vich_uploader.listener.uploader.propel</argument>
<argument>Vich\UploaderBundle\EventListener\PropelUploaderListener</argument>
</call>
</service>
<service id="mazenovi_time_linable_behavior.listener.action.propel" class="Mazenovi\TimeLinableBehaviorBundle\EventListener\PropelActionListener">
<tag name="propel.event_subscriber" class="Cerdi\KeepContactBundle\Model\Offer"/>
<argument type="service" id="security.context"/>
</service>
....
// no equivalent
....
<?php
namespace Mazenovi\TimeLinableBehaviorBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Mazenovi\TimeLinableBehaviorBundle\DependencyInjection\Compiler\RegisterPropelModelsPass;
class MazenoviTimeLinableBehaviorBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
parent::build($container);
$container->addCompilerPass(new RegisterPropelModelsPass());
}
}
<?php
namespace Mazenovi\TimeLinableBehaviorBundle\EventListener;
use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\SecurityContextInterface;
class PropelActionListener implements EventSubscriberInterface
{
/**
* @var SecurityContextInterface $context
*/
protected $context;
/**
* Constructs a new instance of ActionListener.
*
* @param SecurityContextInterface $context The Security Context.
*/
public function __construct(SecurityContextInterface $context)
{
$this->context = $context;
}
/**
* The events the listener is subscribed to.
*
* @return array The array of events.
*/
public static function getSubscribedEvents()
{
return array(
'propel.pre_insert' => 'onCreate',
'propel.pre_update' => 'onUpdate',
'propel.post_delete' => 'onDelete',
);
}
/**
* Update the time line
*
* @param GenericEvent $event The event.
*/
public function onCreate(GenericEvent $event)
{
$object = $this->getObjectFromArgs($event);
$timeLineEntry = new TimeLine();
$timeLineEntry->setObjectClass("");
$timeLineEntry->setObjectId("");
$timeLineEntry->setActionName("");
$timeLineEntry->setPersonId(1977);
$timeLineEntry->setUsername($this->context->getToken()->getUser()->getUsername());
$timeLineEntry->setActionDate("");
$timeLineEntry->save();
}
/**
* Update the time line if necessary.
*
* @param GenericEvent $event The event.
*/
public function onUpdate(GenericEvent $event)
{
$object = $this->getObjectFromArgs($event);
if($object->isModified())
{
$timeLineEntry = new TimeLine();
$timeLineEntry->setObjectClass("");
$timeLineEntry->setObjectId("");
$timeLineEntry->setActionName("");
$timeLineEntry->setPersonId(1977);
$timeLineEntry->setUsername($this->context->getToken()->getUser()->getUsername());
$timeLineEntry->setActionDate("");
$timeLineEntry->save();
}
}
/**
* Clean the time line.
*
* @param GenericEvent $event The event.
*/
public function onDelete(GenericEvent $event)
{
$object = $this->getObjectFromArgs($event);
}
public function getObjectFromArgs($event)
{
return $event->getSubject();
}
}
<?php
namespace Mazenovi\TimeLinableBehaviorBundle\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class RegisterPropelModelsPass implements CompilerPassInterface
{
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
$listener = $container->getDefinition('mazenovi_time_linable_behavior.listener.action.propel');
$config = $container->getParameter('mazenovi_time_linable_behavior');
foreach ($config['models'] as $class) {
$listener->addTag('propel.event_subscriber', array(
'class' => $class
));
}
}
}
parameters:
mazenovi_time_linable_behavior.listener.action.propel.class: Mazenovi\TimeLinableBehaviorBundle\EventListener\PropelActionListener
services:
mazenovi_time_linable_behavior.listener.action.propel:
class: %mazenovi_time_linable_behavior.listener.action.propel.class%
arguments: [@security.context]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment