parseFile() public static method

Reads and parses a file
public static parseFile ( string $filepath, array $options = [] ) : array.
$filepath string
$options array
return array.
Exemplo n.º 1
0
 private function _getPoParsers($source, $targetLanguage)
 {
     $files = array();
     if ($source == Kwf_Trl::SOURCE_WEB) {
         if (file_exists('trl/' . $targetLanguage . '.po')) {
             $files = array('trl/' . $targetLanguage . '.po');
         }
     } else {
         if ($source == Kwf_Trl::SOURCE_KWF) {
             // check all composer packages
             $this->_checkPackagesForTrlFilesAndTryDownloadFromKoalaWebsiteIfNotExisting($targetLanguage);
             $files = glob(VENDOR_PATH . '/*/*/trl/' . $targetLanguage . '.po');
         }
     }
     require_once VENDOR_PATH . '/autoload.php';
     $poParsers = array();
     foreach ($files as $file) {
         $poParser = \Sepia\PoParser::parseFile($file);
         array_push($poParsers, $poParser);
     }
     return $poParsers;
 }
Exemplo n.º 2
0
 public function parse()
 {
     $kwfTrlElements = array();
     if ($this->_kwfPoFilePath) {
         $this->_output->writeln('<info>Reading kwf-po file</info>');
         $kwfPoFile = \Sepia\PoParser::parseFile($this->_kwfPoFilePath);
         $trlElementsExtractor = new TrlElementsExtractor($kwfPoFile);
         $kwfTrlElements = $trlElementsExtractor->extractTrlElements();
     }
     $initDir = getcwd();
     $this->_output->writeln('<info>Changing into directory</info>');
     $repository = new Repository($this->_directory);
     $wc = $repository->getWorkingCopy();
     if ($wc->getDiffPending()->getRawDiff() != '' || $wc->getDiffStaged()->getRawDiff() != '') {
         $this->_output->writeln('<error>Uncommited changes</error>');
         exit;
     }
     chdir($initDir);
     $initBranch = trim($repository->run('rev-parse', array('--abbrev-ref', 'HEAD')));
     $trlElements = array();
     $errors = array();
     $repository = new Repository($this->_directory);
     $repository->run('fetch', array('origin'));
     if ($this->_kwfPoFilePath) {
         $this->_output->writeln('<info>Iterating over branches matching "^[1-9]+.[0-9]+$"</info>');
     } else {
         $this->_output->writeln('<info>Iterating over branches matching "^[3-9]+.[0-9]+$" and >=3.9</info>');
     }
     foreach ($repository->getReferences()->getBranches() as $branch) {
         $branchName = $branch->getName();
         if (strpos($branchName, 'origin/') === false) {
             continue;
         }
         $splited = explode('/', $branchName);
         $isVersionNumber = preg_match('/^[0-9]+.[0-9]+$/i', $splited[1]);
         if (sizeof($splited) >= 3) {
             continue;
         }
         if (!$isVersionNumber && $splited[1] != 'master' && $splited[1] != 'production') {
             continue;
         }
         if (!$this->_kwfPoFilePath && $isVersionNumber && version_compare($splited[1], '3.9', '<')) {
             continue;
         }
         $this->_output->writeln("<info>Checking out branch: {$branchName}</info>");
         $wc->checkout($branchName);
         // parse package
         $this->_output->writeln('Parsing source directory...');
         $parser = new ParseAll($this->_directory, $this->_output);
         $parser->setIgnoredFiles($this->_ignoredFiles);
         $trlElements = array_merge($parser->parseDirectoryForTrl(), $trlElements);
         $newErrors = $parser->getErrors();
         foreach ($newErrors as $key => $error) {
             $newErrors[$key]['branch'] = $branchName;
         }
         $errors = array_merge($newErrors, $errors);
     }
     $wc->checkout($initBranch);
     $sources = array();
     $filteredTrlElements = array();
     foreach ($trlElements as $trlElement) {
         $sources[$trlElement['source']] = true;
         if ($trlElement['source'] == $this->_source) {
             $filteredTrlElements[] = $trlElement;
         }
     }
     $trlElements = $filteredTrlElements;
     // generate po file
     $this->_output->writeln('Generate Po-File...');
     touch($this->_poFilePath);
     $this->_output->writeln($this->_poFilePath);
     $poFileGenerator = new PoFileGenerator($trlElements, $kwfTrlElements);
     $poFile = $poFileGenerator->generatePoFileObject($this->_poFilePath);
     $poFile->writeFile($this->_poFilePath);
     $this->_output->writeln('');
     $this->_output->writeln('Trl Errors:');
     foreach ($trlElements as $trlElement) {
         if (isset($trlElement['error_short']) && $trlElement['error_short']) {
             var_dump($trlElement);
         }
     }
     if (count($errors)) {
         $this->_output->writeln('');
         $this->_output->writeln('Php Parse-Errors:');
         foreach ($errors as $error) {
             $this->_output->writeln('  Branch: ' . $error['branch'] . ' | File: ' . $error['file']);
             $this->_output->writeln('    Error:' . $error['error']->getMessage());
             $this->_output->writeln('  -------------------------------------');
         }
     }
     $this->_output->writeln('');
     $this->_output->writeln('Every branch has been parsed an data is collected in ' . $this->_poFilePath);
     $this->_output->writeln('');
     $this->_output->writeln('1. Please check the mentioned Parse-Errors');
     $this->_output->writeln('2. Upload and translate file from ' . $this->_poFilePath . ' to lingohub or translate in another way');
     $this->_output->writeln('3. When using lingohub call "./vendor/bin/lingohub downloadTranslations" after translating');
     $this->_output->writeln('');
 }
Exemplo n.º 3
0
 private function _getAllPoParsersExceptKwf($source, $targetLanguage)
 {
     $files = array();
     if ($source == Kwf_Trl::SOURCE_WEB) {
         if (file_exists('trl/' . $targetLanguage . '.po')) {
             $files = array('trl/' . $targetLanguage . '.po');
         }
     } else {
         if ($source == Kwf_Trl::SOURCE_KWF) {
             // check all composer packages
             $files = $this->_getAllPackagesTrlFiles($targetLanguage);
         }
     }
     require_once VENDOR_PATH . '/autoload.php';
     $poParsers = array();
     foreach ($files as $file) {
         $poParsers[$file] = \Sepia\PoParser::parseFile($file);
     }
     return $poParsers;
 }