An open API service indexing awesome lists of open source software.

https://github.com/loremipsum/action-logger-bundle

Symfony bundle to log custom actions and events with doctrine.
https://github.com/loremipsum/action-logger-bundle

Last synced: over 1 year ago
JSON representation

Symfony bundle to log custom actions and events with doctrine.

Awesome Lists containing this project

README

          

# ActionLogger bundle

Symfony bundle to log custom actions and events with doctrine.

## Configuration

```yaml
# config/packages/lorem_ipsum_action_logger.yaml

lorem_ipsum_action_logger:
# mapping is used to store actions in the database without using the class name
mapping:
user.add: { class: App\Action\User\UserAddAction }
user.edit: { class: App\Action\User\UserEditAction, alias: 'user_edit' }
settings.edit: { class: App\Action\SettingsEditAction, alias: ['settings_edit', 'settings_update'] }
# entity_mapping is used to store action relations to entities in the database using the class name
entity_mapping:
user: App\Entity\User
settings: App\Entity\Settings
```

## Action example

Usage example:
A new user has been created. All you have to do is call `log` or `flashLog`
on our ActionLogger with the `UserAddAction` and the new `$user` entity.

```php
/** @var LoremIpsum\ActionLoggerBundle\ActionLogger $actionLogger **/
$actionLogger->log(new UserAddAction($user));
// Display action message as flash message
$actionLogger->flashLog(new UserAddAction($user), 'success');
```

And here is our `UserAddAction`:

```php
entity = $em->getRepository(User::class)->find($this->meta['id']);
}

private function getLink(RouteGeneratorInterface $router = null): array
{
if (! $router || ! $this->entity instanceof User) {
return ['%entity%', ['%entity%' => $this->meta['name']]];
}
return ["generate($this->entity)}\">%entity%", ['%entity%' => $this->entity]];
}

public function getMessage(RouteGeneratorInterface $router = null)
{
list($entity, $entities) = $this->getLink($router);
return ["User $entity added.", $entities];
}

public function getUserMessage(RouteGeneratorInterface $router = null)
{
list($user, $users) = $this->getUserLink($router);
list($entity, $entities) = $this->getLink($router);
return ["$user added user $entity.", $users, $entities];
}
}
```

## Event subscriber example

Log all action events with Psr\Log\LoggerInterface.

```php
logger = $logger;
}

public function onLog(ActionEvent $event)
{
$action = $event->getAction();
$this->logger->debug('[ActionLogListener] ' . $event->getActionFactory()->getAction(get_class($action)), $action->getUserMessage());
}

public static function getSubscribedEvents()
{
return [
ActionEvent::class => 'onLog',
];
}
}
```