72 lines
1.9 KiB
PHP
72 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\EventSubscriber;
|
|
|
|
use App\Controller\LoggedController;
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
|
use Symfony\Component\HttpKernel\Event\ControllerEvent;
|
|
use Symfony\Component\HttpKernel\Event\ResponseEvent;
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
|
use Symfony\Component\HttpKernel\KernelEvents;
|
|
|
|
class LogSubscriber implements EventSubscriberInterface
|
|
{
|
|
protected $allow;
|
|
protected $api_log_flag;
|
|
|
|
public function __construct($api_log_flag)
|
|
{
|
|
$this->api_log_flag = $api_log_flag;
|
|
}
|
|
|
|
public function onKernelController(ControllerEvent $event)
|
|
{
|
|
if ($this->api_log_flag == 'false')
|
|
return;
|
|
|
|
$controller = $event->getController();
|
|
$this->allow = false;
|
|
|
|
// when a controller class defines multiple action methods, the controller
|
|
// is returned as [$controllerInstance, 'methodName']
|
|
if (is_array($controller))
|
|
$controller = $controller[0];
|
|
|
|
if (!($controller instanceof LoggedController))
|
|
return;
|
|
|
|
$this->allow = true;
|
|
|
|
$req = $event->getRequest();
|
|
$reqdata = [
|
|
$req->getPathInfo(),
|
|
$req->getMethod(),
|
|
$req->query->all(),
|
|
$req->request->all(),
|
|
];
|
|
error_log(print_r($reqdata, true));
|
|
}
|
|
|
|
public function onKernelResponse(ResponseEvent $event)
|
|
{
|
|
if ($this->api_log_flag == 'false')
|
|
return;
|
|
|
|
if (!$this->allow)
|
|
return;
|
|
|
|
$resp = $event->getResponse();
|
|
|
|
$content = $resp->getContent();
|
|
|
|
error_log(print_r($content, true));
|
|
}
|
|
|
|
public static function getSubscribedEvents()
|
|
{
|
|
return [
|
|
KernelEvents::CONTROLLER => 'onKernelController',
|
|
KernelEvents::RESPONSE => 'onKernelResponse',
|
|
];
|
|
}
|
|
}
|