Skip to content

[FrameworkBundle][Workflow] Add a way to enable the AuditTrail Logger #21933

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

Merged
merged 2 commits into from
Mar 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ private function addWorkflowSection(ArrayNodeDefinition $rootNode)
->fixXmlConfig('place')
->fixXmlConfig('transition')
->children()
->arrayNode('audit_trail')
->canBeEnabled()
->end()
->enumNode('type')
->values(array('workflow', 'state_machine'))
->defaultValue('workflow')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
use Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer;
use Symfony\Component\Workflow;
use Symfony\Component\Workflow\SupportStrategy\ClassInstanceSupportStrategy;
use Symfony\Component\Console\Application;

/**
Expand Down Expand Up @@ -480,13 +479,24 @@ private function registerWorkflowConfiguration(array $workflows, ContainerBuilde
// Add workflow to Registry
if ($workflow['supports']) {
foreach ($workflow['supports'] as $supportedClassName) {
$strategyDefinition = new Definition(ClassInstanceSupportStrategy::class, array($supportedClassName));
$strategyDefinition = new Definition(Workflow\SupportStrategy\ClassInstanceSupportStrategy::class, array($supportedClassName));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this change?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To reduce the number of use statement and to be consistant with other "...\Workflow" usage.

$strategyDefinition->setPublic(false);
$registryDefinition->addMethodCall('add', array(new Reference($workflowId), $strategyDefinition));
}
} elseif (isset($workflow['support_strategy'])) {
$registryDefinition->addMethodCall('add', array(new Reference($workflowId), new Reference($workflow['support_strategy'])));
}

// Enable the AuditTrail
if ($workflow['audit_trail']['enabled']) {
$listener = new Definition(Workflow\EventListener\AuditTrailListener::class);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use a use statement instead? more "SF style" to me

Copy link
Member Author

@lyrixx lyrixx Mar 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now, there are more usage of "this style" than FQCN

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just saw that, in the Workflow namespace in fact. OK then.

$listener->addTag('monolog.logger', array('channel' => 'workflow'));
$listener->addTag('kernel.event_listener', array('event' => sprintf('workflow.%s.leave', $name), 'method' => 'onLeave'));
$listener->addTag('kernel.event_listener', array('event' => sprintf('workflow.%s.transition', $name), 'method' => 'onTransition'));
$listener->addTag('kernel.event_listener', array('event' => sprintf('workflow.%s.enter', $name), 'method' => 'onEnter'));
$listener->addArgument(new Reference('logger'));
$container->setDefinition(sprintf('%s.listener.audit_trail', $workflowId), $listener);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ public function __construct(LoggerInterface $logger)
public function onLeave(Event $event)
{
foreach ($event->getTransition()->getFroms() as $place) {
$this->logger->info(sprintf('Leaving "%s" for subject of class "%s".', $place, get_class($event->getSubject())));
$this->logger->info(sprintf('Leaving "%s" for subject of class "%s" in workflow "%s".', $place, get_class($event->getSubject()), $event->getWorkflowName()));
}
}

public function onTransition(Event $event)
{
$this->logger->info(sprintf('Transition "%s" for subject of class "%s".', $event->getTransition()->getName(), get_class($event->getSubject())));
$this->logger->info(sprintf('Transition "%s" for subject of class "%s" in workflow "%s".', $event->getTransition()->getName(), get_class($event->getSubject()), $event->getWorkflowName()));
}

public function onEnter(Event $event)
{
foreach ($event->getTransition()->getTos() as $place) {
$this->logger->info(sprintf('Entering "%s" for subject of class "%s".', $place, get_class($event->getSubject())));
$this->logger->info(sprintf('Entering "%s" for subject of class "%s" in workflow "%s".', $place, get_class($event->getSubject()), $event->getWorkflowName()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public function testItWorks()
$workflow->apply($object, 't1');

$expected = array(
'Leaving "a" for subject of class "stdClass".',
'Transition "t1" for subject of class "stdClass".',
'Entering "b" for subject of class "stdClass".',
'Leaving "a" for subject of class "stdClass" in workflow "unnamed".',
'Transition "t1" for subject of class "stdClass" in workflow "unnamed".',
'Entering "b" for subject of class "stdClass" in workflow "unnamed".',
);

$this->assertSame($expected, $logger->logs);
Expand Down