src/Events/NGrokAwareRequestContext.php line 44

Open in your IDE?
  1. <?php
  2. namespace App\Events;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  5. use Symfony\Component\HttpKernel\KernelEvents;
  6. use Symfony\Component\Routing\RequestContextAwareInterface;
  7. use Symfony\Component\HttpFoundation;
  8. /**
  9.  * Set host when Ngrok is used as reverse proxy
  10.  */
  11. class NGrokAwareRequestContext implements EventSubscriberInterface
  12. {
  13.     const SCHEME_HTTP  'http';
  14.     const SCHEME_HTTPS 'https';
  15.     const NGROK_DOMAIN '.ngrok.io';
  16.     /**
  17.      * @var RequestContextAwareInterface
  18.      */
  19.     private $router;
  20.     /**
  21.      * @param RequestContextAwareInterface $router
  22.      */
  23.     public function __construct(RequestContextAwareInterface $router)
  24.     {
  25.         $this->router $router;
  26.     }
  27.     /**
  28.      * {@inheritdoc}
  29.      */
  30.     public static function getSubscribedEvents()
  31.     {
  32.         return [KernelEvents::REQUEST => 'onRequest'];
  33.     }
  34.     /**
  35.      * @param GetResponseEvent $event
  36.      */
  37.     public function onRequest(GetResponseEvent $event)
  38.     {
  39.         if (!$event->isMasterRequest()) {
  40.             return;
  41.         }
  42.         $request $event->getRequest();
  43.         $host $this->getHost($request);
  44.         $scheme $this->getScheme($request);
  45.         $router_context $this->router->getContext();
  46.         $router_context->setHost($host);
  47.         $router_context->setScheme($scheme);
  48.     }
  49.     /**
  50.      * Get Ngrok Domain/Hostname or fallback to the default Host
  51.      *
  52.      * @param Request $request
  53.      * @return string
  54.      */
  55.     public function getHost($request)
  56.     {
  57.         // if using ngrok, rewrite the host to be the original host, and not ngrok.io
  58.         // so that the request can be mapped to a virtual server on apache
  59.         $original_host $this->getServer($request'HTTP_X_ORIGINAL_HOST');
  60.         $http_host $this->getServer($request'HTTP_HOST');
  61.         $ngrokDomain $original_host ?: $http_host;
  62.         $useHost stripos($ngrokDomainself::NGROK_DOMAIN) == false $http_host $ngrokDomain;
  63.         return $useHost;
  64.     }
  65.     /**
  66.      * Get the forwarded scheme or fallback to default scheme
  67.      *
  68.      * @param Request $request
  69.      * @return string
  70.      */
  71.     public function getScheme($request)
  72.     {
  73.         $forwarded_proto $this->getServer($request'HTTP_X_FORWARDED_PROTO');
  74.         $default_scheme $this->getServer($request'REQUEST_SCHEME');
  75.         return $forwarded_proto ?: $default_scheme;
  76.     }
  77.     /**
  78.      * Get server parameter value by key
  79.      *
  80.      * @param string $key
  81.      * @return string
  82.      */
  83.     protected function getServer($request$key)
  84.     {
  85.         return $request->server->get($key'');
  86.     }
  87. }