/**
  * Reads a Yaml file and process the keys and returns as a associative indexed array
  *
  * @param string $file
  *
  * @return array
  */
 protected function getYamlAsArray($file)
 {
     if (file_exists($file)) {
         return ArrayTools::YamlToKeyedArray(file_get_contents($file));
     }
     return array();
 }
Esempio n. 2
0
 public function getTranslations($locale, $criteria, $hierarchicalArray = true)
 {
     $sql = vsprintf('SELECT `%s` AS `key`, `%s` AS `message` FROM `%s` WHERE `%s` = :locale AND `%s` = :domain', array($this->getColumnname('key'), $this->getColumnname('message'), $this->getTablename(), $this->getColumnname('locale'), strpos('Bundle', $criteria) !== false ? $this->getColumnname('domain') : $this->getColumnname('bundle')));
     $stmt = $this->getConnection()->prepare($sql);
     $stmt->bindParam('locale', $locale);
     if (strpos('Bundle', $criteria) !== false) {
         $stmt->bindParam('bundle', $criteria);
     } else {
         $stmt->bindParam('domain', $criteria);
     }
     if (false === $stmt->execute()) {
         throw new \RuntimeException('Could not fetch translation data from database.');
     }
     $result = array();
     while ($row = $stmt->fetch()) {
         $result[$row['key']] = $row['message'];
     }
     if ($hierarchicalArray) {
         return ArrayTools::keyedAssocToHierarchical($result);
     }
     return $result;
 }
 /**
  * {@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('');
 }