Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto add error logging to all forms #3

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
7f8f605
added entity and changed the repository name to be prefixed with lend…
Jan 13, 2015
0a1085d
fixed parameter value
Jan 13, 2015
34553e6
renamed entity name
Jan 13, 2015
0c6d64d
used a form extension to subscribe the listener instead of injecting …
Jan 13, 2015
bdf262d
added a created at value to the entity
Jan 13, 2015
c6fc807
renamed field createdAt to just created
Feb 5, 2015
c38063e
Support for Symfony3
titolendable Jul 15, 2016
1ada491
updated service definitions
titolendable Jul 18, 2016
f0f068b
Set dependency on RequestStack
titolendable Jul 18, 2016
134bb91
Update services.yml
titolendable Jul 18, 2016
ec71eb9
Update ErrorLogSubscriber.php
titolendable Jul 19, 2016
23d0f90
added event before storing entity
Sep 20, 2016
0832e87
changed entity to a mappedsuperclass so we can redifine it as an enti…
Sep 20, 2016
db7346c
Removed composer.lock, depend on symfony/symfony for now (should be e…
ben-challis Feb 9, 2017
fa98466
Serialize the errored value as we'd like to catch arrays and other un…
martin-georgiev Oct 25, 2017
fee656a
Serialize the errored value as we'd like to catch arrays and other un…
martin-georgiev Oct 25, 2017
2144335
Alias namespace import to avoid conflict with current class name
martin-georgiev Oct 25, 2017
444bd29
PHP resources cannot be serialized so default in this case to empty s…
martin-georgiev Oct 31, 2017
ce64696
Remove unused trait import
martin-georgiev Oct 31, 2017
eb0f16f
Merge pull request #1 from Lendable/bugfix/php-resources-cant-be-seri…
ppamment Nov 17, 2017
8c56784
Prepare for sf 4
martin-georgiev Nov 17, 2017
f461510
Fix travis script path
martin-georgiev Nov 18, 2017
8eb76bc
Bump min supported php version to 5.5
martin-georgiev Nov 18, 2017
7238353
Change autoloading strategy to PSR-4
martin-georgiev Nov 18, 2017
8870791
Merge pull request #2 from Lendable/feature/symfony4-ready
martin-georgiev Nov 24, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor
/.idea
10 changes: 7 additions & 3 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ public function getConfigTreeBuilder()
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('oh_form_error_log');

// Here you should define the parameters that are allowed to
// configure your bundle. See the documentation linked above for
// more information on that topic.
$rootNode
->children()
->scalarNode('logger')->isRequired()->cannotBeEmpty()->end()
->end()
->children()
->scalarNode('db_entity_class')->defaultValue('Oh\FormErrorLogBundle\Entity\FormErrorLog')->cannotBeEmpty()->end()
->end();

return $treeBuilder;
}
Expand Down
3 changes: 3 additions & 0 deletions DependencyInjection/OhFormErrorLogExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public function load(array $configs, ContainerBuilder $container)
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$container->setAlias('oh_form_error_log.logger.manager', $config['logger']);
$container->setParameter('oh_form_error_log.db.entity.class', $config['db_entity_class']);

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
}
Expand Down
133 changes: 133 additions & 0 deletions Entity/FormErrorLog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

namespace Oh\FormErrorLogBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Oh\FormErrorLogBundle\Entity\FormErrorLogEntityInterface;

/**
* @ORM\MappedSuperclass()
* @ORM\HasLifecycleCallbacks()
*/
class FormErrorLog implements FormErrorLogEntityInterface
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;

/**
* @ORM\Column(name="form_name", type="string", length=255)
* @var string
*/
private $form_name;

/**
* @var string $field
*
* @ORM\Column(name="field", type="string", length=255)
*/
private $field;

/**
* @var string $error
*
* @ORM\Column(name="error", type="string", length=2000)
*/
private $error;

/**
* @var string $error
*
* @ORM\Column(name="value", type="string", length=2000)
*/
private $value;

/**
* @var string $uri
*
* @ORM\Column(type="string", length=512)
*/
private $uri;

/**
* @ORM\Column(type="datetime")
*/
private $created;

public function getFormName()
{
return $this->form_name;
}

public function setFormName($formName)
{
$this->form_name = $formName;
}

public function getField()
{
return $this->field;
}

public function setField($field)
{
$this->field = $field;
}

public function getError()
{
return $this->error;
}

public function setError($error)
{
$this->error = $error;
}

public function getValue()
{
return $this->value;
}

public function setValue($value)
{
$this->value = $value;
}

public function setUri($uri)
{
$this->uri = $uri;

return $this;
}

public function getUri()
{
return $this->uri;
}

public function getCreated()
{
return $this->created;
}

public function setCreated(\DateTime $created)
{
$this->created = $created;

return $this;
}

/**
* @ORM\PrePersist
*/
public function setCreatedValue()
{
$this->created = new \DateTime();
}
}
3 changes: 2 additions & 1 deletion Entity/FormErrorLogEntityInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ public function setFormName($formName);
public function setField($field);

public function setError($error);


public function setValue($value);
}
8 changes: 8 additions & 0 deletions Event/Events.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Oh\FormErrorLogBundle\Event;

final class Events
{
const PRE_PERSIST = 'oh_form_error_log.entity.pre_persist';
}
28 changes: 28 additions & 0 deletions Event/PrePersistEntityEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Oh\FormErrorLogBundle\Event;

use Oh\FormErrorLogBundle\Entity\FormErrorLogEntityInterface;
use Symfony\Component\EventDispatcher\Event;

class PrePersistEntityEvent extends Event
{
/** @var FormErrorLogEntityInterface */
private $entity;

/**
* @param FormErrorLogEntityInterface $entity
*/
public function __construct(FormErrorLogEntityInterface $entity)
{
$this->entity = $entity;
}

/**
* @return FormErrorLogEntityInterface
*/
public function getEntity()
{
return $this->entity;
}
}
17 changes: 6 additions & 11 deletions EventListener/ErrorLogSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@
use Symfony\Component\Form\FormEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\HttpFoundation\Request;

//use Symfony\Component\Serializer\Serializer;
//use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
//use Symfony\Component\Serializer\Encoder\JsonEncoder;

use Symfony\Component\HttpFoundation\RequestStack;

class ErrorLogSubscriber implements EventSubscriberInterface
{
Expand All @@ -27,23 +22,23 @@ class ErrorLogSubscriber implements EventSubscriberInterface
*/
private $request;

public function __construct(ErrorLogInterface $logger, Request $request)
public function __construct(ErrorLogInterface $logger, RequestStack $request)
{
$this->logger = $logger;
$this->request = $request;
$this->request = $request->getMasterRequest();
}

public static function getSubscribedEvents()
{
return array(FormEvents::POST_BIND => 'postBind');
return array(FormEvents::POST_SUBMIT => 'postSubmit');
}

/**
*
* @param \Symfony\Component\Form\FormEvent $event
* @return null
*/
public function postBind(FormEvent $event)
public function postSubmit(FormEvent $event)
{
$form = $event->getForm();

Expand Down Expand Up @@ -132,4 +127,4 @@ private function getErrorMessages(\Symfony\Component\Form\Form $form) {

return $errors;
}
}
}
28 changes: 28 additions & 0 deletions Form/Extension/FormLogTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Oh\FormErrorLogBundle\Form\Extension;

use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class FormLogTypeExtension extends AbstractTypeExtension
{

private $eventSubscriber;

public function __construct(EventSubscriberInterface $eventSubscriber)
{
$this->eventSubscriber = $eventSubscriber;
}

public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addEventSubscriber($this->eventSubscriber);
}

public function getExtendedType()
{
return 'form';
}
}
37 changes: 26 additions & 11 deletions Logger/DatabaseLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,55 @@

namespace Oh\FormErrorLogBundle\Logger;

use Oh\FormErrorLogBundle\Logger\ErrorLogInterface;
use Doctrine\ORM\EntityManagerInterface;
use Oh\FormErrorLogBundle\Entity\FormErrorLogEntityInterface;
use Oh\FormErrorLogBundle\Event\Events;
use Oh\FormErrorLogBundle\Event\PrePersistEntityEvent;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class DatabaseLogger implements ErrorLogInterface
{
private $em;

private $entityClass;

public function __construct($em, $entityClass)

private $eventDispatcher;

/**
* @param EntityManagerInterface $em
* @param $entityClass
* @param EventDispatcherInterface $eventDispatcher
*/
public function __construct(EntityManagerInterface $em, $entityClass, EventDispatcherInterface $eventDispatcher)
{
$this->em = $em;
$this->entityClass = $entityClass;
$this->eventDispatcher = $eventDispatcher;
}

public function log($formName, $key, $error, $value = '', $uri = '')
{
if($this->entityClass == 'Oh\FormErrorLogBundle\Entity\FormErrorLogEntityInterface') {
if ($this->entityClass == 'Oh\FormErrorLogBundle\Entity\FormErrorLogEntityInterface') {
throw new InvalidArgumentException('You need to update your %oh_form_error_log.db.entity.class% parameter to your own class. See the README for help.');
}

/** @var FormErrorLogEntityInterface $entity */
$entity = new $this->entityClass;

$entity->setFormName($formName);
$entity->setField($key);
$entity->setError($error);
$entity->setValue($value);
// for BC
if(method_exists($entity, 'setUri')) {
if (method_exists($entity, 'setUri')) {
$entity->setUri($uri);
}


$this->eventDispatcher->dispatch(Events::PRE_PERSIST, new PrePersistEntityEvent($entity));

$this->em->persist($entity);

$this->em->flush($entity);

}

}
Loading