Skip to content

Commit

Permalink
[close #35] Implement ODM Support
Browse files Browse the repository at this point in the history
[#35] Implement ODM Support
  • Loading branch information
ronisaha committed Dec 6, 2018
2 parents e754134 + bacc571 commit 39c1940
Show file tree
Hide file tree
Showing 108 changed files with 1,921 additions and 1,187 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,5 +1,6 @@
# IDE
.nbproject
.idea

vendor
composer.lock
Expand Down
21 changes: 5 additions & 16 deletions Annotation/ORMSubscribedEvents.php
Expand Up @@ -11,29 +11,18 @@

namespace Xiidea\EasyAuditBundle\Annotation;

@trigger_error(sprintf('The "%s" annotation is deprecated since version 1.4.10. Use "%s" instead.', ORMSubscribedEvents::class, SubscribeDoctrineEvents::class), E_USER_DEPRECATED);

/**
* Annotation for ORM Subscribed Event.
*
* @Annotation
* @Target({"CLASS"})
*
* @author Roni Saha <roni@xiidea.net>
*
* @deprecated since version 1.4.10
*/
final class ORMSubscribedEvents
final class ORMSubscribedEvents extends SubscribeDoctrineEvents
{
public $events = array();

public function __construct(array $values)
{
if (isset($values['value'])) {
$values['events'] = $values['value'];
}
if (!isset($values['events'])) {
return;
}

$this->events = is_array($values['events']) ? $values['events'] : array_map('trim', explode(',', $values['events']));

$this->events = array_filter($this->events);
}
}
39 changes: 39 additions & 0 deletions Annotation/SubscribeDoctrineEvents.php
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of the XiideaEasyAuditBundle package.
*
* (c) Xiidea <http://www.xiidea.net>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Xiidea\EasyAuditBundle\Annotation;

/**
* Annotation for ORM Subscribed Event.
*
* @Annotation
* @Target({"CLASS"})
*
* @author Roni Saha <roni@xiidea.net>
*/
/* @final */ class SubscribeDoctrineEvents
{
public $events = array();

public function __construct(array $values)
{
if (isset($values['value'])) {
$values['events'] = $values['value'];
}
if (!isset($values['events'])) {
return;
}

$this->events = is_array($values['events']) ? $values['events'] : array_map('trim', explode(',', $values['events']));

$this->events = array_filter($this->events);
}
}
18 changes: 9 additions & 9 deletions Common/UserAwareComponent.php
Expand Up @@ -15,7 +15,7 @@
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Role\SwitchUserRole;
use \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

class UserAwareComponent
{
Expand All @@ -42,16 +42,15 @@ public function setTokenStorage($tokenStorage)
$this->tokenStorage = $tokenStorage;
}


/**
* Get a user from the Security Context
* Get a user from the Security Context.
*
* @return mixed
*
* @throws \LogicException If SecurityBundle is not available
*/
public function getUser()
{

if (null === $token = $this->tokenStorage->getToken()) {
return null;
}
Expand Down Expand Up @@ -102,7 +101,7 @@ public function getUsername()
{
$user = $this->getUser();

if(empty($user)) {
if (empty($user)) {
return $this->getAnonymousUserName();
}

Expand All @@ -117,15 +116,16 @@ protected function getAnonymousUserName()
$request = $this->getRequest();

if ($request && $request->getClientIp()) {
return "Anonymous";
return 'Anonymous';
}

return "By Command";
return 'By Command';
}

/**
* @param TokenInterface $token
* @param null $user
* @param null $user
*
* @return mixed
*/
protected function getImpersonatingUserFromRole($token, $user = null)
Expand All @@ -142,7 +142,7 @@ protected function getImpersonatingUserFromRole($token, $user = null)

protected function getRequest()
{
if($this->requestStack === null) {
if (null === $this->requestStack) {
return false;
}

Expand Down
1 change: 0 additions & 1 deletion DependencyInjection/Compiler/LoggerFactoryPass.php
Expand Up @@ -19,7 +19,6 @@ class LoggerFactoryPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{

if (false === $container->hasDefinition('xiidea.easy_audit.logger_factory')) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/Compiler/MonologLoggerPass.php
Expand Up @@ -25,6 +25,6 @@ public function process(ContainerBuilder $container)

$definition = $container->getDefinition('xiidea.easy_audit.mono_logger.service');

$definition->setPublic(TRUE);
$definition->setPublic(true);
}
}
12 changes: 6 additions & 6 deletions DependencyInjection/Compiler/ResolverFactoryPass.php
Expand Up @@ -19,7 +19,6 @@ class ResolverFactoryPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{

if (false === $container->hasDefinition('xiidea.easy_audit.event_resolver_factory')) {
return;
}
Expand All @@ -36,12 +35,12 @@ public function process(ContainerBuilder $container)
}

$definition->addMethodCall('setCommonResolver', array(
$this->getServiceReferenceByConfigName($container, 'resolver'))
$this->getServiceReferenceByConfigName($container, 'resolver'), )
);

if ($container->getParameter('xiidea.easy_audit.entity_event_resolver') !== null) {
if (null !== $container->getParameter('xiidea.easy_audit.doctrine_event_resolver')) {
$definition->addMethodCall('setEntityEventResolver', array(
$this->getServiceReferenceByConfigName($container, 'entity_event_resolver'))
$this->getServiceReferenceByConfigName($container, 'doctrine_event_resolver'), )
);
}

Expand All @@ -51,10 +50,11 @@ public function process(ContainerBuilder $container)
/**
* @param ContainerBuilder $container
* @param $configName
*
* @return Reference
*/
protected function getServiceReferenceByConfigName(ContainerBuilder $container, $configName)
{
return new Reference($container->getParameter('xiidea.easy_audit.' . $configName));
return new Reference($container->getParameter('xiidea.easy_audit.'.$configName));
}
}
}
21 changes: 8 additions & 13 deletions DependencyInjection/Compiler/SubscriberPass.php
Expand Up @@ -19,7 +19,6 @@ class SubscriberPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{

if (false === $container->hasDefinition('xiidea.easy_audit.event_listener')) {
return;
}
Expand All @@ -29,21 +28,17 @@ public function process(ContainerBuilder $container)

private function initializeSubscriberEvents(ContainerBuilder $container)
{

$eventsList = $container->getParameter('xiidea.easy_audit.events');

$this->appendDoctrineEventsToList($container, $eventsList);
$this->appendSubscribedEventsToList($container, $eventsList);

$this->registerEventsToListener($eventsList, $container);

}

private function appendDoctrineEventsToList(ContainerBuilder $container, &$events = array())
{
$doctrine_entities = $container->getParameter('xiidea.easy_audit.doctrine_entities');

if ($doctrine_entities === false) {
if (false === $container->getParameter('xiidea.easy_audit.doctrine_objects')) {
return;
}

Expand All @@ -54,7 +49,7 @@ private function appendDoctrineEventsToList(ContainerBuilder $container, &$event

/**
* @param ContainerBuilder $container
* @param array $events
* @param array $events
*/
private function appendSubscribedEventsToList(ContainerBuilder $container, &$events = array())
{
Expand All @@ -73,7 +68,6 @@ private function appendSubscribedEventsToList(ContainerBuilder $container, &$eve

/**
* @param $attributes
* @return null
*/
private function getResolverFromConfigurationAttributes($attributes)
{
Expand All @@ -93,7 +87,6 @@ private function appendEventsFromSubscriber(ContainerBuilder $container, &$event
$subscribedEvents = $subscriber->getSubscribedEvents();

foreach ($subscribedEvents as $key => $item) {

$resolver = !empty($defaultResolver) && !is_string($key) ? $defaultResolver : $key;
$this->addEventFromSubscriber($events, $item, $resolver);
}
Expand Down Expand Up @@ -129,6 +122,7 @@ private function appendEventArray(&$events, $resolver, $item)

/**
* @param $resolver
*
* @return bool
*/
private function isEventWithResolver($resolver)
Expand All @@ -140,13 +134,14 @@ private function isEventWithResolver($resolver)
* @param $events
* @param $items
* @param $key
*
* @internal param $event
*/
private function appendEventWithResolver(&$events, $items, $key)
{
$items = (array) $items;

foreach($items as $item) {
foreach ($items as $item) {
array_push($events, array($item => $key));
}
}
Expand All @@ -157,7 +152,7 @@ private function appendEventWithResolver(&$events, $items, $key)
*/
private function registerEventsToListener($events, ContainerBuilder $container)
{
if(empty($events)) {
if (empty($events)) {
return;
}

Expand All @@ -173,19 +168,19 @@ private function registerEventsToListener($events, ContainerBuilder $container)

/**
* @param $events
*
* @return array
*/
private function getListenableEventList($events)
{
$eventList = array();

foreach ($events as $item) {

$event = is_array($item) ? key($item) : $item;

$eventList[$event] = array(
'event' => $event,
'method' => 'resolveEventHandler'
'method' => 'resolveEventHandler',
);
}

Expand Down
26 changes: 20 additions & 6 deletions DependencyInjection/Configuration.php
Expand Up @@ -17,14 +17,14 @@
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;

/**
* This is the class that validates and merges configuration from your app/config files
* This is the class that validates and merges configuration from your app/config files.
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
*/
class Configuration implements ConfigurationInterface
{
/**
* {@inheritDoc}
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
{
Expand All @@ -48,7 +48,10 @@ private function addRequiredConfigs(ArrayNodeDefinition $rootNode)
$rootNode
->children()
->scalarNode('user_property')->isRequired()->end()
->scalarNode('entity_class')->cannotBeOverwritten()->isRequired()->cannotBeEmpty()->end()
->scalarNode('audit_log_class')->cannotBeOverwritten()->isRequired()->cannotBeEmpty()->end()
->scalarNode('entity_class')->cannotBeOverwritten()
// ->setDeprecated('The "%node%" option is deprecated since XiideaEasyAuditBundle 1.4.10. and will not be supported anymore in 2.0. Use "audit_log_class" instead.')
->end()
->end();
}

Expand All @@ -60,8 +63,12 @@ private function addDefaultServices(ArrayNodeDefinition $rootNode)
$rootNode
->children()
->scalarNode('resolver')->defaultValue('xiidea.easy_audit.default_event_resolver')->end()
->scalarNode('doctrine_event_resolver')
->defaultValue(null)
->end()
->scalarNode('entity_event_resolver')
->defaultValue(null)
// ->setDeprecated('The "%node%" option is deprecated since XiideaEasyAuditBundle 1.4.10. and will not be supported anymore in 2.0. Use "doctrine_event_resolver" instead.')
->end()
->booleanNode('default_logger')->defaultValue(true)->end()
->end();
Expand All @@ -74,7 +81,13 @@ private function addOptionalConfigs(ArrayNodeDefinition $rootNode)
{
$rootNode
->children()
->variableNode('doctrine_entities')->defaultValue(array())->end()
->variableNode('doctrine_objects')
->defaultValue(array())
->end()
->variableNode('doctrine_entities')
->defaultValue(array())
// ->setDeprecated('The "%node%" option is deprecated since XiideaEasyAuditBundle 1.4.10. and will not be supported anymore in 2.0. Use "doctrine_objects" instead.')
->end()
->variableNode('events')->defaultValue(array())->end()
->variableNode('custom_resolvers')->defaultValue(array())->end()
->end();
Expand Down Expand Up @@ -127,12 +140,13 @@ private function getChannelTypeValidator()
}

/**
* @param boolean $invalid
* @param bool $invalid
*
* @throws InvalidConfigurationException
*/
public static function throwExceptionOnInvalid($invalid)
{
if(!$invalid) {
if (!$invalid) {
return;
}

Expand Down

0 comments on commit 39c1940

Please sign in to comment.