protected function execute(InputInterface $input, OutputInterface $output)
 {
     $em = $this->getContainer()->get('doctrine')->getManager();
     $srcFinder = new Finder();
     $cmsFinder = new Finder();
     $cachedFile = $this->getContainer()->getParameter('debug.container.dump');
     $container = new ContainerBuilder();
     $loader = new XmlFileLoader($container, new FileLocator());
     $loader->load($cachedFile);
     $services = $container->getServiceIds();
     $sites = $em->getRepository('SymbioOrangeGatePageBundle:Site')->findAll();
     $tokens = [];
     // find tokens in src files
     $srcFinder->files()->in($this->getContainer()->get('kernel')->getRootDir() . '/../src')->name('*.php')->name('*.html.twig');
     foreach ($srcFinder as $file) {
         $fileContent = file_get_contents($file->getRealPath());
         foreach ($sites as $site_key => $site) {
             $add = false;
             $tokenSite = null;
             $tokenName = null;
             if (preg_match('/\'' . strtolower($site->getSlug()) . '\\.(.*)\'/', $fileContent, $matches) || preg_match('/\\"' . strtolower($site->getSlug()) . '\\.(.*)\\"/', $fileContent, $matches)) {
                 $tokenSite = $site;
                 $tokenName = str_replace("\"", '', str_replace('\'', '', $matches[0]));
                 $add = true;
             } elseif (preg_match('/\'orangegate\\.(.*)\'/', $fileContent, $matches) || preg_match('/\\"orangegate\\.(.*)\\"/', $fileContent, $matches)) {
                 $tokenName = str_replace("\"", '', str_replace('\'', '', $matches[0]));
                 $add = true;
             }
             if ($add && !in_array($tokenName, $services) && !in_array($tokenName, $tokens)) {
                 $catalogueName = "";
                 if (strpos($tokenName, '|trans') !== false) {
                     $catalogueName = substr($tokenName, strrpos($tokenName, '(') + 1, strlen($tokenName));
                     if (strpos($catalogueName, ',') !== false) {
                         $catalogueName = substr($catalogueName, strrpos($catalogueName, ',') + 1, strlen($catalogueName));
                     }
                     $tokenName = substr($tokenName, 0, strpos($tokenName, '|trans'));
                 }
                 if (strpos($tokenName, ',') !== false) {
                     $tokenName = str_replace(' ', '', $tokenName);
                     $catalogueName = substr($tokenName, strrpos($tokenName, ',') + 1, strlen($tokenName));
                     $tokenName = substr($tokenName, 0, strpos($tokenName, ','));
                 }
                 $catalogueName = $catalogueName == $tokenName || !$catalogueName ? self::DEFAULT_CATALOGUE : $catalogueName;
                 if (strpos($tokenName, '%') === false) {
                     $catalogue = $em->getRepository("SymbioOrangeGateTranslationBundle:LanguageCatalogue")->findOneByName($catalogueName);
                     if (!$catalogue) {
                         $catalogue = new LanguageCatalogue();
                         $catalogue->setName($catalogueName);
                         $em->persist($catalogue);
                     }
                     if (!$em->getRepository('SymbioOrangeGateTranslationBundle:LanguageToken')->findOneBy(['token' => $tokenName])) {
                         $tokens[] = $tokenName;
                         $token = new \Symbio\OrangeGate\TranslationBundle\Entity\LanguageToken();
                         $token->setToken($tokenName);
                         $token->setCatalogue($catalogue);
                         $token->setSite($tokenSite);
                         $em->persist($token);
                     }
                 }
             }
         }
     }
     //find tokens in cms bundles
     $cmsFinder->files()->in($this->getContainer()->get('kernel')->getRootDir() . '/../vendor/symbio')->notName('*.json');
     foreach ($cmsFinder as $file) {
         $fileContent = file_get_contents($file->getRealPath());
         if (preg_match('/\'orangegate\\.(.*)\'/', $fileContent, $matches) || preg_match('/\\"orangegate\\.(.*)\\"/', $fileContent, $matches)) {
             $tokenName = str_replace("\"", '', str_replace('\'', '', $matches[0]));
             if (!in_array($tokenName, $services) && !in_array($tokenName, $tokens)) {
                 $catalogueName = "";
                 if (strpos($tokenName, '|trans') !== false) {
                     $tokenName = str_replace(' ', '', $tokenName);
                     $catalogueName = substr($tokenName, strrpos($tokenName, '(') + 1, strlen($tokenName));
                     if (strpos($catalogueName, ',') !== false) {
                         $catalogueName = substr($catalogueName, strrpos($catalogueName, ',') + 1, strlen($catalogueName));
                     }
                     $tokenName = substr($tokenName, 0, strpos($tokenName, '|trans'));
                 }
                 if (strpos($tokenName, ',') !== false) {
                     $tokenName = str_replace(' ', '', $tokenName);
                     $catalogueName = substr($tokenName, strrpos($tokenName, ',') + 1, strlen($tokenName));
                     $tokenName = substr($tokenName, 0, strpos($tokenName, ','));
                 }
                 $catalogueName = $catalogueName == $tokenName || !$catalogueName ? self::DEFAULT_CATALOGUE : $catalogueName;
                 if (strpos($tokenName, '%') === false) {
                     $catalogue = $em->getRepository("SymbioOrangeGateTranslationBundle:LanguageCatalogue")->findOneByName($catalogueName);
                     if (!$catalogue) {
                         $catalogue = new LanguageCatalogue();
                         $catalogue->setName($catalogueName);
                         $em->persist($catalogue);
                     }
                     if (!$em->getRepository('SymbioOrangeGateTranslationBundle:LanguageToken')->findOneBy(['catalogue' => $catalogue, 'token' => $tokenName])) {
                         $tokens[] = $tokenName;
                         $token = new \Symbio\OrangeGate\TranslationBundle\Entity\LanguageToken();
                         $token->setToken($tokenName);
                         $token->setCatalogue($catalogue);
                         $token->setSite(null);
                         $em->persist($token);
                     }
                 }
             }
         }
     }
     $em->flush();
     foreach ($tokens as $token) {
         $output->writeln('---> Token "' . $token . '" successfully inserted.');
     }
     $output->writeln('Operation complete!');
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $em = $this->getContainer()->get('doctrine')->getManager();
     if (!$input->getArgument('path') && !$input->getOption('default_location')) {
         throw new \Exception("Please provide absolute path to source file (JSON) or run command with [-d] option to load file located in 'dumps' folder.");
     }
     $inPath = $input->getArgument('path') ? $input->getArgument('path') : __DIR__ . "/../Resources/dumps/translation_dump.json";
     $start = strrpos($inPath, '/');
     $fileName = substr($inPath, $start + 1, strlen($inPath) - $start);
     $path = substr($inPath, 0, $start);
     $locator = new FileLocator();
     try {
         $file = $locator->locate($fileName, $path);
     } catch (\Exception $ex) {
         throw new \Exception("File " . $fileName . " doesn't exist in path " . $path);
     }
     $data = json_decode(file_get_contents($file), true);
     if (!$data) {
         throw new \Exception("File " . $fileName . "is empty or it's not a valid JSON file.");
     }
     $catalogues = [];
     $insertedC = 0;
     //$data[0] - LanguageCatalogue table
     foreach ($data[0] as $catalogue) {
         $cat = $em->getRepository('SymbioOrangeGateTranslationBundle:LanguageCatalogue')->findOneBy(['name' => $catalogue['name']]);
         if (!$cat) {
             $cat = new LanguageCatalogue();
             $cat->setName($catalogue['name']);
             $em->persist($cat);
             $insertedC++;
         }
         $catalogues[$catalogue['id']]['cat'] = $cat;
     }
     $tokens = [];
     //$data[1] - LanguageToken table
     foreach ($data[1] as $token) {
         if ($token['site_id'] === null) {
             $tokens[$token['id']]['token'] = $token;
         }
     }
     //$data[2] - LanguageTranslation table
     foreach ($data[2] as $trans) {
         if (isset($tokens[$trans['languageToken_id']])) {
             $tokens[$trans['languageToken_id']]['trans'][] = $trans;
         }
     }
     foreach ($catalogues as $key => $cat) {
         foreach ($tokens as $t_key => $token) {
             if ($token['token']['catalogue_id'] == $key) {
                 $catalogues[$key]['tokens'][$t_key] = $token;
             }
         }
     }
     $insertedT = 0;
     foreach ($catalogues as $cat) {
         foreach ($cat['tokens'] as $record) {
             $token = $em->getRepository('SymbioOrangeGateTranslationBundle:LanguageToken')->findOneBy(['catalogue' => $cat['cat'], 'token' => $record['token']['token']]);
             if (!$token) {
                 $token = new LanguageToken();
                 $token->setToken($record['token']['token']);
                 $token->setCatalogue($cat['cat']);
                 $insertedT++;
                 $em->persist($token);
             }
             foreach ($record['trans'] as $tr) {
                 $trans = $em->getRepository('SymbioOrangeGateTranslationBundle:LanguageTranslation')->findOneBy(['languageToken' => $token, 'language' => $tr['language']]);
                 if (!$trans) {
                     $trans = new LanguageTranslation();
                     $trans->setLanguage($tr['language']);
                     $trans->setLanguageToken($token);
                 }
                 $trans->setTranslation($tr['translation']);
                 $em->persist($trans);
             }
         }
     }
     $em->flush();
     $output->writeln("==========CATALOGUES==========");
     $output->writeln("Inserted: " . $insertedC);
     $output->writeln("============TOKENS============");
     $output->writeln("Inserted: " . $insertedT);
     $output->writeln("Clearing the translation cache.");
     //clear translations cache
     $cacheDir = $this->getContainer()->get('kernel')->getCacheDir();
     $fs = new Filesystem();
     if ($fs->exists($cacheDir . '/../*/translations')) {
         $finder = new Finder();
         $finder->in([$cacheDir . '/../*/translations'])->files();
         foreach ($finder as $file) {
             unlink($file->getRealPath());
         }
         if (is_dir($cacheDir . '/translations')) {
             rmdir($cacheDir . '/translations');
         }
     }
     $output->writeln("Operation complete!");
 }