/**
  * @param string    $bundleName
  * @param string    $file
  * @param string    $key
  * @param string    $locale
  * @param string    $content
  * @param string    $catalog
  * @param \DateTime $updatedAt
  */
 protected function updateOrInsertEntry($bundleName, $file, $key, $locale, $content, $catalog, \DateTime $updatedAt = null)
 {
     $shortFile = str_replace($this->srcDir, '', $file);
     $shortFile = str_replace('\\/', '/', $shortFile);
     $mod = $updatedAt ?: new \DateTime(date('c', filemtime($file)));
     /** @var Translation $entry */
     $entry = $this->translationRepository->findOneBy(array('domain' => $bundleName, 'key' => $key, 'locale' => $locale));
     if (!$entry) {
         $entry = new Translation();
     }
     $entry->setDomain($catalog);
     $entry->setBundle($bundleName);
     $entry->setFile($shortFile);
     $entry->setKey($key);
     $entry->setMessage($content);
     $entry->setUpdatedAt($mod);
     $entry->setLocale($locale);
     $this->em->persist($entry);
 }
 /**
  * {@inheritdoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $this->input = $input;
     $this->output = $output;
     $this->init();
     $this->output->writeln('<info>*** Extracting translating info from php and twig files ***</info>');
     $fileNames = array();
     $keys = array();
     $idx = 0;
     $numKeys = 0;
     $patterns = array('*.twig' => '/(["\'])(?<trans>(?:\\\\1|(?!\\1).)*?)\\1\\s*\\|\\s*trans/i', '*.php' => '/trans\\s*\\(\\s*(["\'])(?<trans>(?:\\\\1|(?!\\1).)*?)\\1\\s*\\)/i', '*.js' => '/trans.getTranslation\\s*\\(\\s*(["\'])(?<trans>(?:\\\\1|(?!\\1).)*?)\\1\\s*\\)/i');
     $folders = array($this->srcDir . '/app', $this->srcDir . '/src');
     $keyInfo = array();
     foreach ($patterns as $filePattern => $exrPattern) {
         foreach ($folders as $folder) {
             $output->writeln($folder);
             $finder = new Finder();
             $files = $finder->in($folder)->name($filePattern)->files();
             /** @var SplFileInfo[] $files */
             foreach ($files as $file) {
                 $fileName = $folder . '/' . $file->getRelativePathname();
                 if (strpos($fileName, $folder . "/cache") === 0) {
                     //$output->writeln(sprintf("ignored <comment>%s</comment>", $file));
                     continue;
                 }
                 if (preg_match("/\\/(?P<bundle>.*Bundle)\\//U", $file->getRelativePathname(), $match)) {
                     $bundleName = $match['bundle'];
                 } else {
                     $bundleName = "*app";
                 }
                 $fileContents = file_get_contents($fileName);
                 if (preg_match_all($exrPattern, $fileContents, $matches)) {
                     $output->writeln(sprintf("<info>%s</info>", $file));
                     $fileNames[$bundleName] = $file->getRelativePathname();
                     if (!isset($keys[$bundleName])) {
                         $output->writeln($bundleName);
                         $keys[$bundleName] = array();
                     }
                     $keys[$bundleName] = array_merge_recursive($keys[$bundleName], $matches["trans"]);
                     foreach ($matches['trans'] as $currentKey) {
                         $keyInfo[$bundleName][$currentKey] = $file->getRelativePathname();
                     }
                     $numKeys += count($matches["trans"]);
                 }
                 $idx++;
             }
         }
     }
     $output->writeln(sprintf("Total %d files examined, and found translations in %d files", $idx, count($fileNames)));
     $output->writeln(sprintf("Total %d keys extracted (%d)", $numKeys, count($keys, COUNT_RECURSIVE)));
     //        var_dump($fileNames);
     //        var_dump($keys);
     $sortedKeys = array();
     /** @var Translation[] $localKeys */
     $localKeys = $this->translationRepository->findAll();
     foreach ($localKeys as $localKey) {
         $bundle = $localKey->getBundle();
         $keyName = $localKey->getKey();
         $sortedKeys[$bundle][$keyName] = true;
     }
     $localSortedKeys = array();
     // find keys thar are in files but not in db
     foreach ($keys as $bundle => $keyArray) {
         foreach ($keyArray as $key) {
             if (!isset($sortedKeys[$bundle][$key])) {
                 $output->writeln(sprintf("file local key <info>%s</info>[<comment>%s</comment>] not found in DB, file %s", $bundle, $key, $keyInfo[$bundle][$key]));
             }
             $localSortedKeys[$bundle][$key] = true;
         }
     }
     // find keys that are in db but not used in files
     foreach ($sortedKeys as $bundle => $keyArray) {
         //var_dump($keyArray);
         foreach ($keyArray as $key => $void) {
             if (!isset($localSortedKeys[$bundle][$key])) {
                 $output->writeln(sprintf("db key <info>%s</info>[<comment>%s</comment>] not used in local files", $bundle, $key));
                 //var_dump($localSortedKeys[$bundle]);
                 //die;
             }
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $this->input = $input;
     $this->output = $output;
     $ymlOptions = array('regenerate' => false, 'backup' => false, 'blank' => false);
     $yml = $this->input->getOption('yml');
     if ($yml) {
         $aux = explode(",", $yml);
         if (count($aux)) {
             foreach ($aux as $option) {
                 $ymlOptions[$option] = true;
             }
         }
     }
     if (count($ymlOptions) != 3) {
         var_dump($ymlOptions);
         die('Sorry, but you can use only regenerate,blank and backup with --yml option');
     }
     $this->init($input->getOption('address'), $input->getOption('port'));
     $config = $this->getContainer()->getParameter('translations_api');
     $managedLocales = $config['managed_locales'];
     $this->output->writeln(PHP_EOL . '<info>*** Syncing translations ***</info>');
     /**
      * uploading local catalog keys (from local table) to remote server
      */
     if ($input->getOption('upload-first') == 'yes') {
         $catalogs = $this->translationsRepository->getCatalogs();
         foreach ($catalogs as $catalog) {
             // data para enviar al servidor
             $data = array();
             $this->output->writeln(PHP_EOL . sprintf('<info>Processing catalog %s ...</info>', $catalog));
             /** @var Translation[] $messages */
             $messages = $this->translationsRepository->findBy(array('domain' => $catalog));
             foreach ($messages as $message) {
                 $key = $message->getKey();
                 $locale = $message->getLocale();
                 $bundle = $message->getBundle();
                 $fileName = $message->getFile();
                 $data[$key][$locale] = array('message' => $message->getMessage(), 'updatedAt' => $message->getUpdatedAt()->format('c'), 'fileName' => $message->getFile(), 'bundle' => $message->getBundle());
             }
             $this->output->writeln('uploadKeys("' . $catalog . '", $data)');
             $result = $this->clientApiService->uploadKeys($catalog, $data);
         }
     }
     /**
      * download the remote catalogs and integrate into local table (previously truncate local table)
      */
     // truncate local translations table
     $this->translationsRepository->truncateTranslations();
     $result = $this->clientApiService->getCatalogIndex();
     if ($result['result']) {
         $catalogs = $result['catalogs'];
     } else {
         die('error getting catalogs');
     }
     foreach ($catalogs as $catalog) {
         $this->output->writeln(PHP_EOL . sprintf('<info>Processing catalog %s ...</info>', $catalog));
         $result = $this->clientApiService->downloadKeys($catalog);
         file_put_contents(sys_get_temp_dir() . DIRECTORY_SEPARATOR . $catalog . '.json', json_encode($result));
         $bundles = $result['bundles'];
         foreach ($result['data'] as $key => $data) {
             foreach ($data as $locale => $messageData) {
                 echo '.';
                 $fileName = isset($messageData['fileName']) ? $messageData['fileName'] : '';
                 $trans = Translation::newFromArray($catalog, $key, $locale, $messageData, $bundles[$key], $fileName);
                 $this->em->persist($trans);
             }
         }
     }
     $this->output->writeln(PHP_EOL . '<info>Flushing to DB ...</info>');
     $this->em->flush();
     /**
      * regeneration of local .yml files if user wants
      */
     /** @var DialogHelper $dialog */
     $dialog = $this->getHelper('dialog');
     if ($ymlOptions['regenerate']) {
         $bundles = $this->translationsRepository->getBundles();
         foreach ($bundles as $bundle) {
             if (!$bundle) {
                 continue;
             }
             $keys = array();
             $filenames = array();
             $scheme = "";
             // in order to deduce filename from other keys
             $translations = $this->translationsRepository->getKeysByBundle($bundle);
             foreach ($translations as $translation) {
                 $locale = $translation->getLocale();
                 $file = $translation->getFile();
                 if ($file && $locale && !$scheme) {
                     $scheme = str_replace(".{$locale}.", ".%s.", $file);
                     break;
                 }
             }
             foreach ($translations as $translation) {
                 $locale = $translation->getLocale();
                 $file = $translation->getFile();
                 if ($locale && !$file && $scheme) {
                     $file = sprintf($scheme, $locale);
                 }
                 if ($file && $locale) {
                     if (!isset($filenames[$locale])) {
                         $filenames[$locale] = $file;
                     }
                     $keys[$locale][$translation->getKey()] = $translation->getMessage();
                 }
             }
             foreach ($filenames as $locale => $file) {
                 $this->output->writeln(sprintf('Generating <info>"%s"</info> ...', $file));
                 $subKeys = $keys[$locale];
                 $file = dirname($this->rootDir) . '/src/' . $file;
                 if ($ymlOptions['blank']) {
                     foreach ($subKeys as $key => $value) {
                         if (!$value) {
                             $subKeys[$key] = $key;
                         }
                     }
                 }
                 if ($ymlOptions['backup'] && file_exists($file)) {
                     copy($file, $file . '.' . date('U'));
                 }
                 @mkdir(dirname($file), 0777, true);
                 file_put_contents($file, ArrayTools::prettyYamlDump($subKeys));
             }
         }
     }
     /**
      * erasing cached translations files
      */
     $this->output->writeln(PHP_EOL . '<info>Clearing SF cache ...</info>');
     $finder = new Finder();
     $finder->files()->in($this->rootDir . "/cache")->name('*');
     foreach ($finder as $file) {
         $fileFull = $file->getRealpath();
         //$relativePath = $file->getRelativePath();
         $fileName = $file->getRelativePathname();
         if (preg_match('!/translations/.+$!i', $fileName)) {
             $this->output->writeln('removing ' . $fileName);
             unlink($file);
         }
     }
     $this->output->writeln('');
 }
 /**
  * {@inheritdoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $this->input = $input;
     $this->output = $output;
     $this->init();
     $verbose = (bool) $input->getOption('verbose');
     $bundleToExtract = $input->getOption('bundle');
     $minWords = intval($input->getOption('min-words') ?: 0);
     $this->output->writeln('<info>*** Discovering new keys in php, twig and js files ***</info>');
     $fileNames = array();
     $keys = array();
     $idx = 0;
     $numKeys = 0;
     $patterns = array('*.twig' => '/(["\'])(?<trans>(?:\\\\1|(?!\\1).)+?)\\1\\s*\\|\\s*trans/i', '*.html.twig' => '/\\.trans\\s*\\(\\s*(["\'])(?<trans>(?:\\\\1|(?!\\1).)+?)\\1/i', '*.php' => '/trans\\s*\\(\\s*(["\'])(?<trans>(?:\\\\1|(?!\\1).)+?)\\1\\s*\\)/i', '*Type.php' => '/([\'"])label\\1\\s*=>\\s*([\'"])(?<trans>(?:\\\\2|(?!\\2).)+?)\\2/', '*.js' => '/trans.getTranslation\\s*\\(\\s*(["\'])(?<trans>(?:\\\\1|(?!\\1).)+?)\\1\\s*\\)/i');
     $folders = array($this->srcDir . '/app', $this->srcDir . '/src');
     $keyInfo = array();
     foreach ($patterns as $filePattern => $exrPattern) {
         foreach ($folders as $folder) {
             if ($verbose) {
                 $output->writeln($folder);
             }
             $finder = new Finder();
             $files = $finder->in($folder)->name($filePattern)->files();
             /** @var SplFileInfo[] $files */
             foreach ($files as $file) {
                 $fileName = $folder . '/' . $file->getRelativePathname();
                 if (strpos($fileName, $folder . "/cache") === 0) {
                     //$output->writeln(sprintf("ignored <comment>%s</comment>", $file));
                     continue;
                 }
                 if (preg_match("/\\/(?P<bundle>.*Bundle)\\//U", $file->getRelativePathname(), $match)) {
                     $bundleName = $match['bundle'];
                 } else {
                     $bundleName = "*app";
                 }
                 if (!$bundleToExtract || $bundleToExtract == $bundleName) {
                     $fileContents = file_get_contents($fileName);
                     if (preg_match_all($exrPattern, $fileContents, $matches)) {
                         if ($verbose) {
                             $output->writeln(sprintf("<info>%s</info>", $file));
                         }
                         $fileNames[$bundleName] = $file->getRelativePathname();
                         if (!isset($keys[$bundleName])) {
                             if ($verbose) {
                                 $output->writeln($bundleName);
                             }
                             $keys[$bundleName] = array();
                         }
                         $keys[$bundleName] = array_merge_recursive($keys[$bundleName], $matches["trans"]);
                         foreach ($matches['trans'] as $currentKey) {
                             $keyInfo[$bundleName][$currentKey] = $file->getRelativePathname();
                         }
                         $numKeys += count($matches['trans']);
                     }
                     $idx++;
                 }
             }
         }
     }
     $output->writeln(sprintf("Total %d files examined, and found key translations in %d files", $idx, count($fileNames)));
     $output->writeln(sprintf("Total %d keys extracted (%d)", $numKeys, count($keys, COUNT_RECURSIVE)));
     // get all the translations from the local DB
     $sortedKeys = array();
     /** @var Translation[] $localKeys */
     $localKeys = $this->translationRepository->findAll();
     foreach ($localKeys as $localKey) {
         $bundle = $localKey->getBundle();
         $keyName = $localKey->getKey();
         $sortedKeys[$keyName][$bundle] = true;
     }
     unset($localKeys);
     $localSortedKeys = array();
     /** @var CandidateKey[] $candidates */
     $candidates = array();
     // find keys thar are in files but not in db
     foreach ($keys as $bundle => $keyArray) {
         foreach ($keyArray as $key) {
             if (!isset($sortedKeys[$key])) {
                 if (!$minWords || $minWords - 1 <= substr_count($key, ".")) {
                     $file = $keyInfo[$bundle][$key];
                     $candidates[] = new CandidateKey($bundle, $file, $key);
                     //$output->writeln(sprintf("file local key <info>%s</info>[<comment>%s</comment>] not found in DB, file %s", $bundle, $key, $file));
                 }
             }
             $localSortedKeys[$bundle][$key] = true;
         }
     }
     //        // find keys that are in db but not used in files
     //        foreach($sortedKeys as $bundle=>$keyArray){
     //
     //            //var_dump($keyArray);
     //            foreach($keyArray as $key=>$void){
     //
     //                if(!isset($localSortedKeys[$bundle][$key])){
     //
     //                    $output->writeln(sprintf("db key <info>%s</info>[<comment>%s</comment>] not used in local files", $bundle, $key));
     //                    //var_dump($localSortedKeys[$bundle]);
     //                    //die;
     //                }
     //
     //            }
     //
     //        }
     if (!count($candidates)) {
         $output->writeln("Congratulations!, you have all the keys synchronized");
         exit;
     }
     $output->writeln("Check the possible candidate keys to upload to server");
     foreach ($candidates as $candidate) {
         $output->writeln(sprintf("key <info>%s</info>[<comment>%s</comment>] not found in local DB, file %s", $candidate->getKey(), $candidate->getBundle(), $candidate->getFile()));
     }
     $output->writeln(PHP_EOL . '<info>Have you been synchronized your translations with the sync command?</info>' . PHP_EOL);
     /** @var DialogHelper $dialog */
     $dialog = $this->getHelper('dialog');
     if ($dialog->askConfirmation($output, '<question>Confirm that you want to upload the previous keys to server ?</question>', false)) {
         $catalog = $dialog->ask($output, 'Name of catalog in which to integrate new keys [messages]:', 'messages');
         $data = array();
         $date = new \DateTime();
         $config = $this->getContainer()->getParameter('translations_api');
         $managedLocales = $config['managed_locales'];
         foreach ($candidates as $candidate) {
             $key = $candidate->getKey();
             $bundle = $candidate->getBundle();
             $fileName = $candidate->getFile();
             if (preg_match('|^(?<prefix>\\w+)/' . $bundle . '/|', $fileName, $matches)) {
                 if ($matches['prefix']) {
                     $prefix = $matches['prefix'];
                     foreach ($managedLocales as $locale) {
                         $data[$key][$locale] = array('message' => '', 'updatedAt' => $date->format('c'), 'fileName' => $prefix . '/' . $this->genFileFromBundleAndLocale($bundle, $catalog, $locale), 'bundle' => $bundle);
                     }
                 }
             }
         }
         //$this->output->writeln('uploadKeys("' . $catalog . '", $data)');
         //var_dump($data); //die;
         $this->clientApiService->init();
         $result = $this->clientApiService->uploadKeys($catalog, $data);
     }
     $output->writeln('You must now to synchronize your translations with the sync command!');
 }