src/EventListener/RequestListener.php line 92

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Doctrine\Persistence\ManagerRegistry;
  4. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  5. use Symfony\Component\HttpKernel\HttpKernel;
  6. use Symfony\Component\HttpKernel\HttpKernelInterface;
  7. use Doctrine\ORM\EntityManager;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\HttpKernel\Event\RequestEvent;
  10. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  11. use Symfony\Component\HttpKernel\KernelInterface;
  12. use Symfony\Contracts\Translation\TranslatorInterface;
  13. use App\Entity\Currency;
  14. use App\Entity\Eshop;
  15. use App\Entity\User;
  16. use App\Entity\Website;
  17. class RequestListener
  18. {
  19.     protected $container;
  20.     protected $doctrine;
  21.     protected $appKernel;
  22.     public function __construct(KernelInterface $appKernel,
  23.                                 ManagerRegistry $doctrine,
  24.                                 $defaultLocale 'cs')
  25.     {
  26.         $this->appKernel $appKernel;
  27.         $this->doctrine $doctrine;
  28.         if($_SERVER['HTTP_HOST'] == 'www.easycommerc.cz' OR $_SERVER['HTTP_HOST'] == 'easycommerc.cz') {
  29.             $defaultLocale 'cs';
  30.         } elseif($_SERVER['HTTP_HOST'] == 'www.easycommerc.com' OR $_SERVER['HTTP_HOST'] == 'easycommerc.com') {
  31.             $defaultLocale 'en';
  32.         } elseif($_SERVER['HTTP_HOST'] == 'www.easycommerc.eu' OR $_SERVER['HTTP_HOST'] == 'easycommerc.eu') {
  33.             $defaultLocale 'en';
  34.         }
  35.         $this->defaultLocale $defaultLocale;
  36.     }
  37.     /**
  38.      * @throws \ReflectionException
  39.      */
  40.     public function __invoke(RequestEvent $event): void
  41.     {
  42.         
  43.         $request $event->getRequest();
  44.         if (!$event->isMainRequest()) {
  45.             // don't do anything if it's not the master request
  46.             return;
  47.         }
  48.         $session $request->getSession();
  49.         
  50. //        print('<br> QQW RequestListener');
  51.         //code to get db credentials from master database and stored in varaiables
  52.         $user null;
  53.         /* we setup e-shop and its user */
  54.         if(($request->attributes->get('_route') == 'eshop'
  55.             OR ($request->attributes->get('_route') == 'eshop3d')
  56.             OR ($request->attributes->get('_route') == 'basket3d')            
  57.             OR ($request->attributes->get('_route') == 'eshopCategory')
  58.             OR ($request->attributes->get('_route') == 'eshopProduct')
  59.             OR ($request->attributes->get('_route') == 'eshopBasket')
  60.             OR ($request->attributes->get('_route') == 'eshopLogin')
  61.             OR ($request->attributes->get('_route') == 'eshopRegister')
  62.             OR ($request->attributes->get('_route') == 'eshopHomeContact')
  63.             OR ($request->attributes->get('_route') == 'eshopEditContact')            
  64.             OR ($request->attributes->get('_route') == 'eshopOrderContact')
  65.             OR ($request->attributes->get('_route') == 'payment')
  66.             OR ($request->attributes->get('_route') == 'customer')
  67.             OR ($request->attributes->get('_route') == 'thankyou')
  68.             OR ($request->attributes->get('_route') == 'eshopPage')
  69.             OR ($request->attributes->get('_route') == 'search')
  70.             OR ($request->attributes->get('_route') == 'eshopRequest')    
  71.             OR ($request->attributes->get('_route') == 'getBasket')    
  72.             OR ($request->attributes->get('_route') == 'addBasket')    
  73.             OR ($request->attributes->get('_route') == 'apiEshopProduct')    
  74.             OR ($request->attributes->get('_route') == 'eshopForgottenPassword')    
  75.           ) {  
  76.             //print('<br>qqw setting e-shop');
  77.             //die();
  78.             
  79.             $em $this->doctrine->getManager();
  80.             $eshop $em->getRepository(Eshop::class)->getEshop($request->attributes->get('eshopId'));
  81.             $session->set('eshop'$eshop);
  82.             
  83.             /* we load e-shop currencies into session */
  84.             if(empty($session->get('eshopCurrencyList'))) {
  85.                 //print("<br>QQW e-shop currencies: ");
  86.                 $eshopCurrencyList $eshop->getCurrencies();
  87.                 $session->set('eshopCurrencyList'$eshopCurrencyList);    
  88.             }
  89.             
  90.             /* we load default currency into session */
  91.             if(empty($session->get('eshopCurrency'))) {
  92.                 //print("<br>QQW e-shop default currency: ");
  93.                 $eshopCurrency $em->getRepository(Currency::class)->getCurrency($eshop->getPreferredCurrencyId());
  94.                 $session->set('eshopCurrency'$eshopCurrency);
  95.             }
  96.             $user $em->getRepository(User::class)->getUser($session->get('eshop')->getUserId());
  97.         }
  98.         /* we setup website and its user */
  99.         if($request->attributes->get('_route') == 'web' 
  100.             OR $request->attributes->get('_route') == 'webGalleries'
  101.             OR $request->attributes->get('_route') == 'webGallery'
  102.             OR $request->attributes->get('_route') == 'webLogin'
  103.             OR $request->attributes->get('_route') == 'webRegister'
  104.             OR $request->attributes->get('_route') == 'webHomeContact'
  105.             OR $request->attributes->get('_route') == 'page') {
  106.             //print('<br>qqw we setup website: '.$request->attributes->get('webId'));
  107.             $em $this->doctrine->getManager();
  108.             $website $em->getRepository(Website::class)->getWebsite($request->attributes->get('webId'));
  109.             $session->set('website'$website);
  110.             $user $em->getRepository(User::class)->getUser($session->get('website')->getUserId());
  111.         }    
  112.         /* we set user oriented services */
  113.         if($request->attributes->get('_route') == 'gallery3D' 
  114.             OR $request->attributes->get('_route') == 'freeWorld3D'
  115.             OR $request->attributes->get('_route') == 'form'
  116.             ) {
  117.             //print('<br>qqw we setup user: '.$request->attributes->get('userId'));
  118.             $em $this->doctrine->getManager();
  119.             $user $em->getRepository(User::class)->getUser($request->attributes->get('userId'));
  120.         }    
  121.         
  122.         //print('<br>qqw request listener');
  123.         if(empty($user) && !empty($session->get('user'))) {
  124.             //print('<br> set user from session');
  125.             $user $session->get('user');
  126.         }
  127.         /* we set user entity manager */
  128.         if(!empty($user)) {
  129.             $em $this->doctrine->getManager();
  130.             $connection $this->appKernel->getContainer()->get(sprintf('doctrine.dbal.%s_connection''dynamic_conn'));
  131.             $connection->close();
  132.                 
  133.             $refConn = new \ReflectionObject($connection);
  134.             $refParams $refConn->getProperty('params');
  135.             $refParams->setAccessible('public'); //we have to change it for a moment
  136.             $params $refParams->getValue($connection);
  137.             $params['dbname'] = $user->getDatabaseName();
  138.             $params['user'] = $user->getDatabaseUser();
  139.             $params['password'] = $user->getDatabasePassword();
  140.                 
  141.             $refParams->setAccessible('private');
  142.             $refParams->setValue($connection$params);
  143.             $this->doctrine->resetManager('dynamic_em');
  144.         }
  145.         /* if current user has set db host */
  146.         /*
  147.         if(!empty($session->get('user'))) {
  148.             print('<br>qqw set dem from user');
  149.             $connection = $this->container->get(sprintf('doctrine.dbal.%s_connection', 'dynamic_conn'));
  150.             $connection->close();
  151.             
  152.             $refConn = new \ReflectionObject($connection);
  153.             $refParams = $refConn->getProperty('_params');
  154.             $refParams->setAccessible('public'); //we have to change it for a moment
  155.             
  156.             $params = $refParams->getValue($connection);
  157.             $params['dbname'] = $session->get('user')->getDatabaseName();
  158.             $params['user'] = $session->get('user')->getDatabaseUser();
  159.             $params['password'] = $session->get('user')->getDatabasePassword();
  160.             
  161.             $refParams->setAccessible('private');
  162.             $refParams->setValue($connection, $params);
  163.             $this->container->get('doctrine')->resetEntityManager('dynamic_em');
  164.         } elseif(!empty($session->get('eshop'))) {
  165.             print('<br>qqw set dem from eshop');
  166.             
  167.             $em = $this->container->get('doctrine')->getManager();
  168.             
  169.             //$eshop = $em->getRepository('AppBundle:Eshop')->getCurrentEshop($currentUserId);
  170.             $user = $em->getRepository('AppBundle:User')->getUser($session->get('eshop')->getUserId());
  171.             $connection = $this->container->get(sprintf('doctrine.dbal.%s_connection', 'dynamic_conn'));
  172.             $connection->close();
  173.                 
  174.             $refConn = new \ReflectionObject($connection);
  175.             $refParams = $refConn->getProperty('_params');
  176.             $refParams->setAccessible('public'); //we have to change it for a moment
  177.             $params = $refParams->getValue($connection);
  178.             $params['dbname'] = $user->getDatabaseName();
  179.             $params['user'] = $user->getDatabaseUser();
  180.             $params['password'] = $user->getDatabasePassword();
  181.                 
  182.             $refParams->setAccessible('private');
  183.             $refParams->setValue($connection, $params);
  184.             $this->container->get('doctrine')->resetEntityManager('dynamic_em');
  185.         } elseif(!empty($session->get('website'))) {
  186.             print('<br>qqw set dem from website');
  187.             
  188.             $em = $this->container->get('doctrine')->getManager();
  189.             $user = $em->getRepository('AppBundle:User')->getUser($session->get('website')->getUserId());
  190.             $connection = $this->container->get(sprintf('doctrine.dbal.%s_connection', 'dynamic_conn'));
  191.             $connection->close();
  192.                 
  193.             $refConn = new \ReflectionObject($connection);
  194.             $refParams = $refConn->getProperty('_params');
  195.             $refParams->setAccessible('public'); //we have to change it for a moment
  196.             $params = $refParams->getValue($connection);
  197.             $params['dbname'] = $user->getDatabaseName();
  198.             $params['user'] = $user->getDatabaseUser();
  199.             $params['password'] = $user->getDatabasePassword();
  200.                 
  201.             $refParams->setAccessible('private');
  202.             $refParams->setValue($connection, $params);
  203.             $this->container->get('doctrine')->resetEntityManager('dynamic_em');
  204.         }
  205.         */
  206.         
  207.         //$em = $this->container->get('doctrine')->getManager();
  208.         //print('<br>locale listener:');
  209. //        $request = $event->getRequest();
  210. //        if (!$request->hasPreviousSession()) {
  211. //            return;
  212. //        }
  213. //
  214. //        // try to see if the locale has been set as a _locale routing parameter
  215. //        if ($locale = $request->attributes->get('_locale')) {
  216. //            $request->getSession()->set('_locale', $locale);
  217. //        } else {
  218. //            // if no explicit locale has been set on this request, use one from the session
  219. //            $request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
  220. //        }
  221. //
  222. //        if(!empty($request->getSession()->get('lang'))) {
  223. //            $locale = $request->getSession()->get('lang')->getLangKey();
  224. //        } else {
  225. //            $locale = $this->defaultLocale;
  226. //            //$locale = 'cz';
  227. //        }
  228. //        //$locale = 'cs';
  229. //        print('<br> QQW LocaleListener locale 3: '.$locale);
  230. //
  231. //        $request->setLocale($locale);
  232.     }
  233. }