<?php
namespace ApiV3Bundle\Listener;
use ApiV3Bundle\Security\Firewall\ListenerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Topxia\Service\Common\ServiceKernel;
class ApiV3KernelRequestListener
{
/**
* @var ContainerInterface
*/
private $container;
private $firewall;
public function __construct(ContainerInterface $container, ListenerInterface $firewallListener)
{
$this->container = $container;
$this->firewall = $firewallListener;
}
public function onKernelRequest(RequestEvent $event)
{
$request = $event->getRequest();
if (HttpKernelInterface::MAIN_REQUEST != $event->getRequestType()) {
return;
}
if (0 === stripos($request->getPathInfo(), '/api_v3') && $this->isWhiteRouting($request)) {
$this->firewall->handle($request);
}
if (str_starts_with((string) $request->headers->get('Content-Type'), 'application/json')) {
$data = json_decode($request->getContent(), true);
$request->request->replace(is_array($data) ? $data : []);
}
}
private function isWhiteRouting($request)
{
$whiteRouters = [
'/api_v3/user/login',
];
foreach ($whiteRouters as $whiteRouter) {
if (0 !== stripos($request->getPathInfo(), $whiteRouter)) {
return true;
}
}
return false;
}
protected function getServiceKernel()
{
return ServiceKernel::instance();
}
protected function getSettingService()
{
return ServiceKernel::instance()->createService('System:SettingService');
}
}