phplibrary/src/Filter/ActionLogger.php

36 lines
1.1 KiB
PHP

<?php
namespace ctiso\Filter;
use ctiso\Role\UserInterface,
ctiso\HttpRequest;
/* Переделать формат Логов на список json */
class ActionLogger
{
public $before = array();
public $file;
public $user;
public $action;
public $processor;
function __construct($processor/*: Filter*/, $logPath, $user/*: UserInterface*/) {
$this->processor = $processor;
$this->user = $user;
$file = fopen($logPath, "a");
if (is_resource($file)) {
$this->file = $file;
} else {
throw new \Exception('Ошибка открытия файла ' . $logPath);
}
}
function execute(HttpRequest $request) {
$action = $request->getAction();
if(in_array($action, $this->before)) {
$line = ['time' => time(), 'user' => $this->user->getName(), 'sid' => session_id(), 'query' => array_merge($_POST, $_GET)];
fwrite($this->file, json_encode($line) . "\n");
}
return $this->processor->execute($request);
}
}