Get translations to export and export translations into a file.

@param FileInterface $file
169261
protected function exportFile(FileInterface $file)
    {
        $rootDir = $this->input->getOption('export-path') ? $this->input->getOption('export-path') . '/' : $this->getContainer()->getParameter('kernel.root_dir');

        $this->output->writeln(sprintf('<info># Exporting "%s/%s":</info>', $file->getPath(), $file->getName()));
        $override = $this->input->getOption('override');

        if (!$this->input->getOption('export-path')) {
            // we only export updated translations in case of the file is located in vendor/
            if ($override) {
                $onlyUpdated = ('Resources/translations' !== $file->getPath());
            } else {
                $onlyUpdated = (false !== strpos($file->getPath(), 'vendor/'));
            }
        } else {
            $onlyUpdated = !$override;
        }

        $translations = $this->getContainer()
            ->get('lexik_translation.translation_storage')
            ->getTranslationsFromFile($file, $onlyUpdated);

        if (count($translations) < 1) {
            $this->output->writeln('<comment>No translations to export.</comment>');

            return;
        }

        $format = $this->input->getOption('format') ? $this->input->getOption('format') : $file->getExtention();

        // we don't write vendors file, translations will be exported in %kernel.root_dir%/Resources/translations
        if (false !== strpos($file->getPath(), 'vendor/') || $override) {
            $outputPath = sprintf('%s/Resources/translations', $rootDir);
        } else {
            $outputPath = sprintf('%s/%s', $rootDir, $file->getPath());
        }

        $this->output->writeln(sprintf('<info># OutputPath "%s":</info>', $outputPath));

        // ensure the path exists
        if ($this->input->getOption('export-path')) {
            /** @var Filesystem $fs */
            $fs = $this->getContainer()->get('filesystem');
            if (!$fs->exists($outputPath)) {
                $fs->mkdir($outputPath);
            }
        }

        $outputFile = sprintf('%s/%s.%s.%s', $outputPath, $file->getDomain(), $file->getLocale(), $format);
        $this->output->writeln(sprintf('<info># OutputFile "%s":</info>', $outputFile));

        $translations = $this->mergeExistingTranslations($file, $outputFile, $translations);
        $this->doExport($outputFile, $translations, $format);
    }
212765
public function getLanguage($file = '')
    {
        $path = $this->path . DIRECTORY_SEPARATOR . $file . '.php';
        if (file_exists($path)) {
            $variables = require $path;
            return $variables;
        } else {
            return false;
        }
    }
1181339
public function export($file, $translations)
    {
        $bytes = false;

        if (pathinfo($file, PATHINFO_EXTENSION) === 'yml') {
            $ymlDumper = new Dumper();
            $ymlContent = '';
            $ymlContent .= $ymlDumper->dump($translations, 10);
            $bytes = file_put_contents($file, $ymlContent);
        }

        return ($bytes !== false);
    }
1056535
protected function evaluateFile(FileInterface $file) {
    $uri = $file->getFileUri();
    $scheme = $this->fileSystem->uriScheme($uri);
    return !empty($this->configuration['filesystems'][$scheme]);
  }
229445
protected function getFileContents($file)
    {
        if ($file instanceof ModuleResource) {
            $path = $file->getPath();
        } else {
            $path = Director::baseFolder() . '/' . $file;
        }
        if (!file_exists($path)) {
            return null;
        }
        $content = file_get_contents($path);

        // Remove UTF-8 BOM
        if (substr($content, 0, 3) === pack("CCC", 0xef, 0xbb, 0xbf)) {
            $content = substr($content, 3);
        }

        return $content;
    }
182862
public function getFileTranslationMessages(SplFileInfo $file, $domain)
    {
        $locale = explode('.', $file->getFilename())[1];

        if (!in_array($locale, $this->getLocales())) {
            return [];
        }

        $translations = [];

        try {
            $domainMessages = $this->parser->parse(file_get_contents($file->getPath().DIRECTORY_SEPARATOR.$file->getFilename()));
        } catch (ParseException $e) {
            throw new InvalidResourceException(sprintf('Error parsing YAML, invalid file "%s"', $file->getPath()), 0, $e);
        }

        $path = substr(pathinfo($file->getPathname(), PATHINFO_DIRNAME), strlen(getcwd()) + 1);

        $domainMessages = $this->flatten($domainMessages);

        foreach ($domainMessages as $key => $content) {
            $translations[$domain][$key]['messages'][$locale] = $content;
            $translations[$domain][$key]['path'] = $path;
            $translations[$domain][$key]['format'] = $file->getExtension();
        }

        return $translations;
    }
34265
private function getData(string $file): array
  {
    static $RESULT_STOP_WORDS_CACHE = array();

    if (isset($RESULT_STOP_WORDS_CACHE[$file])) {
      return $RESULT_STOP_WORDS_CACHE[$file];
    }

    $file = __DIR__ . '/stopwords/' . $file . '.php';
    if (file_exists($file)) {
      /** @noinspection PhpIncludeInspection */
      $RESULT_STOP_WORDS_CACHE[$file] = require $file;
    } else {
      $RESULT_STOP_WORDS_CACHE[$file] = array();
    }

    return $RESULT_STOP_WORDS_CACHE[$file];
  }
105220
private function _getTranslators($file)
    {
        $subject = file_get_contents($file);
        preg_match_all($this->module->patternArrayTranslator, $subject, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
        $translators = [];
        foreach ($matches as $data) {
            if (isset($data['translator'][0])) {
                $translators[$data['translator'][0]] = true;
            }
        }

        return array_keys($translators);
    }
894547
public function save(FileInterface $file)
    {
        $file = $this->ensureEloquentFile($file);

        $this->callBeforeListeners('save', [$file]);

        $path = $file->getPath();
        $parentDir = null;

        if ($path != '/') {
            $parentDir = $this->ensureParentIsInDb($file);
            $path = '/' .  $this->normalizeFilePath($file, $parentDir);
            $this->applyChangesInFs($file, $path);
        }

        // Save the title
        $title = $file->hasTitle() ? $file->getTitle() : '';
        $this->fillByPath($file, $path);

        if ($title) {
            $file->setTitle($title);
        }

        if ($parentDir) {
            $file->setParent($parentDir);
        }

        if (!$this->nodeRepository->save($file)) {
            return false;
        }

        $this->callAfterListeners('save', [$file]);

        return true;
    }
717833
protected function _handleFiles(EntityInterface $entity) {
		$files = $this->getConfig('file');
		if (is_string($files)) {
			$files = [$files => $this->getConfig('defaults')];
		}

		$results = [];
		foreach ($files as $key => $file) {
			if (is_string($key)) {
				$field = $key;
				$options = $this->getConfig('defaults');
				$options += $file;
			}
			if (is_string($file)) {
				$field = $file;
				$options = $this->getConfig('defaults');
			}
			$results[$field] = $this->saveFile($entity->{$field}, $options);
		}

		return $results;
	}
412388
public function getTranslationsForFile(ModelFile $file, $onlyUpdated)
    {
        $builder = $this->createQueryBuilder()
            ->hydrate(false)
            ->select('key', 'translations')
            ->field('translations.file.$id')->equals(new \MongoId($file->getId()))
            ->sort('translations.created_at', 'asc');

        $results = $builder->getQuery()->execute();

        $translations = array();
        foreach ($results as $result) {
            $content = null;
            $i = 0;
            while ($i < count($result['translations']) && null === $content) {
                if ($file->getLocale() == $result['translations'][$i]['locale']) {
                    if ($onlyUpdated) {
                        $updated = ($result['translations'][$i]['created_at']->sec < $result['translations'][$i]['updated_at']->sec);
                        $content = $updated ? $result['translations'][$i]['content'] : null;
                    } else {
                        $content = $result['translations'][$i]['content'];
                    }
                }
                $i++;
            }

            if (null !== $content) {
                $translations[$result['key']] = $content;
            }
        }

        return $translations;
    }
373501
public static function exportToFile(
        string $file,
        OutputInterface $output,
        callable $queryItems
    ) {
        $resource = fopen($file, 'w');

        $i = 1;
        $count = 0;
        while (true) {
            $items = $queryItems(Query::create('', $i, 500))->getItems();

            if (empty($items)) {
                break;
            }

            self::writeItemsToResource(
                $resource,
                $items
            );
            self::printPartialCountSaved($output, count($items));

            $count += count($items);
            ++$i;
        }

        fclose($resource);

        return $count;
    }
381723
private function checkFileMigration(string $file, OutputInterface $output): int
    {
        $queries = Parser::extractSql(file_get_contents($file));
        if (!empty($queries)) {
            $output->writeln(sprintf('<comment>Migration %s contains</comment>', $file));
            $this->printQueries($queries, $output);
        } else {
            $output->writeln(sprintf('<comment>Migration %s is empty</comment>', $file));
        }
        return 0;
    }
1259484
protected function moveImports($file)
    {
        $imports = [];

        $file = (string)preg_replace_callback(self::CSS_IMPORT_REGEX, function ($matches) use (&$imports) {
            $imports[] = $matches[0];

            return '';
        }, $file);

        return implode("\n", $imports) . "\n\n" . $file;
    }
251673
private function getFileContents($file) {
		$content = file_get_contents($file);

		// Remove UTF-8 BOM
		if (substr($content, 0, 3) === pack("CCC", 0xef, 0xbb, 0xbf))
			$content = substr($content, 3);

		return $content;
	}
706377
public function generateDownloadUrl(FileInterface $file)
    {
        return $this->container->get('router')->generate('thrace_media_file_download', array(
            'filepath' => $file->getFilePath(), 'filename' => $file->getOriginalName()
        ), true);
    }
1160363
public function persist(File $file, FileLocator $fileLocator = null): string {
		$this->ensureNotReadOnly('persist');
	
		$fileEngine = $this->getFileEngine();
		$qualifiedName = $fileEngine->persist($file, $fileLocator);
		if (!$this->tm->hasOpenTransaction()) {
			$fileEngine->flush();
		}
	
		return $qualifiedName;
	}