PHP code example of loremipsum / action-logger-bundle

1. Go to this page and download the library: Download loremipsum/action-logger-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

loremipsum / action-logger-bundle example snippets


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



namespace App\Action\User;

use App\Entity\User;
use Doctrine\Common\Persistence\ObjectManager;
use LoremIpsum\ActionLoggerBundle\Action\EntityAction;
use LoremIpsum\ActionLoggerBundle\Entity\LogAction;
use LoremIpsum\RouteGeneratorBundle\Model\RouteGeneratorInterface;

class UserAddAction extends EntityAction
{
    /**
     * @var User
     */
    protected $entity;

    public function __construct(User $entity = null)
    {
        parent::__construct($entity);
    }
    
    public function getIcon()
    {
        return 'fa fa-plus';
    }

    public function getLevel(): int
    {
        return LogAction::LEVEL_INFO;
    }

    protected function load(ObjectManager $em)
    {
        $this->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 ["<a href=\"{$router->generate($this->entity)}\">%entity%</a>", ['%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];
    }
}



namespace App\EventListener;

use LoremIpsum\ActionLoggerBundle\Event\ActionEvent;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class ActionLogListener implements EventSubscriberInterface
{
    private $logger;

    public function __construct(LoggerInterface $logger)
    {
        $this->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',
        ];
    }
}