src/ApiV3Bundle/Listener/ApiV3KernelRequestListener.php line 26

Open in your IDE?
  1. <?php
  2. namespace ApiV3Bundle\Listener;
  3. use ApiV3Bundle\Security\Firewall\ListenerInterface;
  4. use Symfony\Component\DependencyInjection\ContainerInterface;
  5. use Symfony\Component\HttpKernel\Event\RequestEvent;
  6. use Symfony\Component\HttpKernel\HttpKernelInterface;
  7. use Topxia\Service\Common\ServiceKernel;
  8. class ApiV3KernelRequestListener
  9. {
  10.     /**
  11.      * @var ContainerInterface
  12.      */
  13.     private $container;
  14.     private $firewall;
  15.     public function __construct(ContainerInterface $containerListenerInterface $firewallListener)
  16.     {
  17.         $this->container $container;
  18.         $this->firewall $firewallListener;
  19.     }
  20.     public function onKernelRequest(RequestEvent $event)
  21.     {
  22.         $request $event->getRequest();
  23.         if (HttpKernelInterface::MAIN_REQUEST != $event->getRequestType()) {
  24.             return;
  25.         }
  26.         if (=== stripos($request->getPathInfo(), '/api_v3') && $this->isWhiteRouting($request)) {
  27.             $this->firewall->handle($request);
  28.         }
  29.         if (str_starts_with((string) $request->headers->get('Content-Type'), 'application/json')) {
  30.             $data json_decode($request->getContent(), true);
  31.             $request->request->replace(is_array($data) ? $data : []);
  32.         }
  33.     }
  34.     private function isWhiteRouting($request)
  35.     {
  36.         $whiteRouters = [
  37.             '/api_v3/user/login',
  38.         ];
  39.         foreach ($whiteRouters as $whiteRouter) {
  40.             if (!== stripos($request->getPathInfo(), $whiteRouter)) {
  41.                 return true;
  42.             }
  43.         }
  44.         return false;
  45.     }
  46.     protected function getServiceKernel()
  47.     {
  48.         return ServiceKernel::instance();
  49.     }
  50.     protected function getSettingService()
  51.     {
  52.         return ServiceKernel::instance()->createService('System:SettingService');
  53.     }
  54. }