/**
  * Add a file to the current import buffer.
  *
  * @param string $filePath The path to the file
  * @param string $fileName Optional, the filename to parse to extract resources data
  *
  * @return array
  *
  * @throws ImportException
  */
 protected function importFile($filePath, $fileName = null)
 {
     // Filename parsing
     if ($fileName == null) {
         $fileName = basename($filePath);
     }
     if (!preg_match('/\\w+\\.\\w+\\.\\w+/', $fileName)) {
         throw new ImportException("Invalid filename [{$fileName}], all translation files must be named: domain.locale.format (ex: messages.en.yml)");
     }
     list($domain, $locale, $format) = explode('.', $fileName, 3);
     $catalogue = $this->translator->loadResource(array('format' => $format, 'locale' => $locale, 'domain' => $domain, 'path' => $filePath));
     // Merge with existing entries
     $translations = $this->getTranslationsFromSession();
     $counters = array('new' => 0, 'updated' => 0);
     if (!array_key_exists($locale, $translations)) {
         $translations[$locale] = array('new' => array(), 'updated' => array());
     }
     foreach ($catalogue->all() as $domain => $messages) {
         foreach ($messages as $key => $value) {
             if ($trans = $this->repository->findTranslation($domain, $key, $locale, true)) {
                 if ($trans->getValue() !== $value) {
                     $translations[$locale]['updated'][$domain][$key] = array('old' => $trans->getValue(), 'new' => $value);
                     $counters['updated'] += 1;
                 }
             } else {
                 $translations[$locale]['new'][$domain][$key] = $value;
                 $counters['new'] += 1;
             }
         }
     }
     $this->updateSession($translations);
     return $counters;
 }
 public function processImportOfStandardResources($options)
 {
     $options = array_merge(array('locale_list' => null, 'domain_list' => null, 'output' => null, 'import-translations' => false, 'override' => false, 'metadata_locale' => 'en', 'prune' => false), $options);
     if (array_key_exists('output', $options)) {
         $this->logger = $options['output'];
     }
     $locales = $options['locale_list'] !== null ? $options['locale_list'] : $this->getLocaleList();
     // Create all catalogues
     /** @var MessageCatalogue[] $catalogues */
     $catalogues = array();
     foreach ($locales as $locale) {
         $catalogues[$locale] = new MessageCatalogue($locale);
     }
     // Import resources one by one
     $this->log("<info>Evaluation of the file list</info>\n");
     foreach ($this->getStandardResources() as $resource) {
         $this->log("    Resource <fg=cyan>{$resource['path']}</fg=cyan>");
         if ($this->checkIfResourceIsIgnored($resource)) {
             $this->log("  >> <comment>Skipped</comment> (due to ignore settings from the config)\n");
             continue;
         }
         if (!in_array($resource['locale'], $locales)) {
             $this->log("  >> <comment>Skipped</comment> (unwanted locales)\n");
             continue;
         }
         if ($options['domain_list'] !== null && !in_array($resource['domain'], $options['domain_list'])) {
             $this->log("  >> <comment>Skipped</comment> (unwanted domain)\n");
             continue;
         }
         $catalogues[$resource['locale']]->addCatalogue($this->translator->loadResource($resource));
         $this->log("  >> <info>OK</info>\n");
     }
     // Update all units from the catalog
     $this->log("\n<info>Update or create units and associated translations</info>\n");
     $existingUnits = $this->repository->getAllByDomainAndKey();
     $allFileUnits = array();
     foreach ($locales as $locale) {
         foreach ($catalogues[$locale]->all() as $domain => $translations) {
             foreach ($translations as $key => $value) {
                 // Retrieved or create a unit
                 if (!isset($existingUnits[$domain][$key])) {
                     $this->log("\t>> Creation of a new Unit [{$domain}, {$key}]\n");
                     $existingUnits[$domain][$key] = $this->repository->createUnit($domain, $key);
                 }
                 $unit = $existingUnits[$domain][$key];
                 // Update it's metadata
                 if ($locale == $options['metadata_locale']) {
                     $catalogMetadata = $catalogues[$locale]->getMetadata($key, $domain);
                     if ($catalogMetadata !== $unit->getMetadata()) {
                         $unit->setMetadata(is_null($catalogMetadata) ? array() : $catalogMetadata);
                         $this->log("\t>> Metadata of the Unit [{$domain}, {$key}] updated\n");
                     }
                 }
                 // Update translation
                 if ($options['import-translations']) {
                     if ($unit->hasTranslation($locale) && $options['override']) {
                         $this->log("\t>> Translation [{$domain}, {$key}] for [{$locale}] overridden by '{$value}'\n");
                         $unit->setTranslation($locale, $value);
                     }
                     if (!$unit->hasTranslation($locale)) {
                         $this->log("\t>> Translation [{$domain}, {$key}] for [{$locale}] imported\n");
                         $unit->setTranslation($locale, $value);
                     }
                 }
                 // Key a trace of all units from the file for the prune process
                 $allFileUnits[$domain][$key] = true;
             }
         }
     }
     // Potentially remove no more existing units
     if ($options['prune'] === true) {
         $this->log("\n<info>Remove units that are no more present in translation files</info>\n");
         $domains = $options['domain_list'] !== null ? $options['domain_list'] : array_keys($existingUnits);
         foreach ($domains as $domain) {
             if (!isset($allFileUnits[$domain])) {
                 continue;
             }
             $removed = array_diff(array_keys($existingUnits[$domain]), array_keys($allFileUnits[$domain]));
             foreach ($removed as $key) {
                 $this->log("\t>> Translation unit [{$domain}, {$key}] removed\n");
                 $existingUnits[$domain][$key]->delete();
             }
         }
     }
     $this->log("<info>Save the changes</info>\n");
     $this->log("<comment>Persisting</comment> ... ");
     $stat = $this->repository->persist();
     $this->log("<info>Success</info>\n");
     return $stat;
 }