PHP code example of lyrasoft / action-log

1. Go to this page and download the library: Download lyrasoft/action-log 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/ */

    

lyrasoft / action-log example snippets


use Lyrasoft\ActionLog\Middleware\ActionLogMiddleware;

// ...

$router->group('admin')
    // ...
    ->middleware(ActionLogMiddleware::class)
    // ...

use Lyrasoft\ActionLog\ActionLogPackage;

// ...

    $this->lang->loadAllFromVendor(ActionLogPackage::class, 'ini');

// Action Log
$menu->link('操作記錄')
    ->to($nav->to('action_log_list'))
    ->icon('fal fa-shoe-prints');

return [
    'action_log' => [
        'reserve_max_time' => env('ACTION_LOG_MAX_TIME') ?: '3months',
        'auth_clear' => [
            'chance' => env('ACTION_LOG_CLEAR_CHANCE', 1),
            'chance_base' => env('ACTION_LOG_CLEAR_CHANCE_BASE', 100)
        ],

        // ...
    ]
];

$router->group('admin')
    // ...
    ->middleware(
        ActionLogMiddleware::class,
        options: [
            'methods' => ['POST', 'DELETE'],
            'enabled' => (bool) env('ACTION_LOG_ENABLE'),
            'max_time' => '7days',
            // ...
        ]
    )
    // ...

use Lyrasoft\ActionLog\Entity\ActionLog;
use Lyrasoft\Luna\User\UserService;

$router->group('admin')
    // ...
    ->middleware(
        ActionLogMiddleware::class,
        options: [
            'prepare_log' => function (ActionLog $log, UserService $userService) {
                $user = $userService->getUser();
                
                $log->setName($user->getFirstName() . ' ' . $user->getLastName());
            }
        ]
    )
    // ...

    'prepare_log' => function (ActionLog $log, UserService $userService) {
        // ...
        
        if ($log->getTask() === '...') {
            // This log will not save
            return false;
        }
    }

/** @var \Lyrasoft\ActionLog\Service\ActionLogService $actionLogService */
$actionLogService = $app->retrieve(\Lyrasoft\ActionLog\Service\ActionLogService::class);
$appRequest = $app->retrieve(\Windwalker\Core\Http\AppRequest::class);

$actionLogService->clearExpiredIfTriggered();

$actionLogService->log($appRequest, $response ?? null);

// Or just create entity item.
$log = $actionLogService->createLogItem($appRequest, $response ?? null);

namespace App\Subscriber;

use Lyrasoft\ActionLog\Event\FormatEntityEvent;
use Lyrasoft\ActionLog\Event\FormatTaskEvent;
use Windwalker\Event\Attributes\EventSubscriber;
use Windwalker\Event\Attributes\ListenTo;

#[EventSubscriber]
class ActionLogSubscriber
{
    #[ListenTo(FormatTaskEvent::class)]
    public function formatTask(FormatTaskEvent $event): void
    {
        $text = &$event->getTaskText();
        $log = $event->getLog();
        $task = $log->getTask();

        // Custom you task text
        
        // Same examples
        if ($task === 'relativeContracts') {
            $text = '相關合約操作';
        }

        if ($task === 'relativeRentals') {
            $text = '相關委託操作';
        }

        if ($task === 'addToCart') {
            $text = '加入購物車';
        }
    }

    #[ListenTo(FormatEntityEvent::class)]
    public function formatEntity(FormatEntityEvent $event): void
    {
        $text = &$event->getEntityText();
        $log = $event->getLog();

        // Custom you entity text
    }
}

// ...

        'listeners' => [
            \Lyrasoft\ActionLog\Service\ActionLogService::class => [
                \App\Subscriber\ActionLogSubscriber::class
            ]
        ],