src/EventListener/CommandListener.php line 41

Open in your IDE?
  1. <?php
  2. // src/AppBundle/EventListener/CommandListener.php
  3. namespace App\EventListener;
  4. use Doctrine\Persistence\ManagerRegistry;
  5. use Symfony\Component\Console\Event\ConsoleCommandEvent;
  6. use Symfony\Component\Console\ConsoleEvents;
  7. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  8. use Symfony\Component\HttpKernel\Event\RequestEvent;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. use AppBundle\Service\FlexiBee\Utils;
  11. use Symfony\Component\HttpKernel\KernelInterface;
  12. use App\Entity\User;
  13. use App\Entity\UserConnector;
  14. class CommandListener
  15. {
  16.     
  17.     private $container;
  18.     private $utils;
  19.     protected $doctrine;
  20.     protected $appKernel;
  21.     
  22.     public function __construct(KernelInterface $appKernel,
  23.                                 ManagerRegistry $doctrine,
  24.                                                 $defaultLocale 'cs')
  25.     {
  26.         $this->appKernel $appKernel;
  27.         $this->doctrine $doctrine;
  28.         $this->defaultLocale $defaultLocale;
  29.     }
  30.     
  31.     //public function onConsoleCommand(ConsoleCommandEvent $event) {
  32.     /**
  33.      * @throws \ReflectionException
  34.      */
  35.     //public function __invoke(RequestEvent $event): void {
  36.     public function onConsoleCommand(ConsoleCommandEvent $event) {
  37.         // get the output instance
  38.         $output $event->getOutput();
  39.         // get the output instance
  40.         $input $event->getInput();
  41.         $args $input->getArguments();
  42. //        print("\n\n qqw onConsoleCommand getInput: ".$args['connectorCode']);
  43. //        \Doctrine\Common\Util\Debug::dump($args);
  44.         // get the command to be executed
  45.         $command $event->getCommand();
  46.         
  47.         // write something about the command
  48.         if(!empty($args['connectorCode']) && $args['connectorCode'] != '') {
  49.             $output->writeln(sprintf("\n ================= Before running command <info>%s</info> ================="$command->getName()));
  50.             $output->writeln(sprintf("\n User Connector Code: <info>%s</info>\n"$args['connectorCode']));
  51.             //$this->container->setParameter('locale', 'de');
  52.             //$request = $this->container->get('request_stack')->getCurrentRequest();
  53.             //$request = $event->getRequest();
  54.             //$request->setLocale('de');
  55.             
  56.             $em $this->doctrine->getManager();
  57.             
  58.             /* we get user connector */
  59.             $userConnector $em->getRepository(UserConnector::class)->getUserConnectorByCode($args['connectorCode']);
  60.             $userId $userConnector->getUserId();
  61.             $user $em->getRepository(User::class)->getUser($userId);
  62.             //test - setting current locale
  63.             //$this->container->get('gedmo.listener.translatable')->setTranslatableLocale('cs');
  64.             //$this->container->get('gedmo.listener.translatable')->setDefaultLocale('cs');
  65.             
  66.             
  67.             /* we setup entity manager for user erp database*/
  68. //            $connection = $this->container->get(sprintf('doctrine.dbal.%s_connection', 'dynamic_conn'));
  69.             //$connection->setLocale('en');
  70.             $connection $this->appKernel->getContainer()->get(sprintf('doctrine.dbal.%s_connection''dynamic_conn'));
  71.             $connection->close();
  72.             
  73.             $refConn = new \ReflectionObject($connection);
  74.             $refParams $refConn->getProperty('params');
  75.             $refParams->setAccessible('public'); //we have to change it for a moment
  76.             
  77.             $params $refParams->getValue($connection);
  78.             $params['dbname'] = $user->getDatabaseName();
  79.             $params['user'] = $user->getDatabaseUser();
  80.             $params['password'] = $user->getDatabasePassword();
  81.             
  82.             $refParams->setAccessible('private');
  83.             $refParams->setValue($connection$params);
  84.             $this->doctrine->resetManager('dynamic_em');
  85.             
  86.             /* we setup entity manager for target e-shop */
  87. //            $connectionTarget = $this->container->get(sprintf('doctrine.dbal.%s_connection', 'eshop_conn'));
  88.             $connectionTarget $this->appKernel->getContainer()->get(sprintf('doctrine.dbal.%s_connection''eshop_conn'));
  89.             $connectionTarget->close();
  90.             $refConn = new \ReflectionObject($connectionTarget);
  91.             $refParams $refConn->getProperty('params');
  92.             $refParams->setAccessible('public'); //we have to change it for a moment
  93.             $params $refParams->getValue($connectionTarget);
  94.             $params['host'] = $userConnector->getTargetDatabaseHost();
  95.             $params['dbname'] = $userConnector->getTargetDatabaseName();
  96.             $params['user'] = $userConnector->getTargetDatabaseUser();
  97.             $params['password'] = $userConnector->getTargetDatabasePassword();
  98.             $refParams->setAccessible('private');
  99.             $refParams->setValue($connectionTarget$params);
  100.             $this->doctrine->resetManager('eshop_em');
  101.             
  102.         }
  103.     }
  104. }