Created
January 26, 2017 22:35
-
-
Save lenybernard/0ea62e8d8eb30d83e5d6afb9312a79cc to your computer and use it in GitHub Desktop.
Symfony console command to parse xliff files to add or update missing id attribute
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Command; | |
use Doctrine\ORM\EntityManager; | |
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | |
use Symfony\Component\Console\Input\InputArgument; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Input\InputOption; | |
use Symfony\Component\Console\Output\OutputInterface; | |
use Symfony\Component\Finder\Finder; | |
/** | |
* @author [email protected] | |
*/ | |
class XliffFixerCommand extends ContainerAwareCommand | |
{ | |
/** | |
* {@inheritdoc} | |
*/ | |
public function configure() | |
{ | |
parent::configure(); | |
$this | |
->setName('xliff:fixer') | |
->addOption('path', null, InputOption::VALUE_OPTIONAL, 'Where the script will search for xliff files', __DIR__.'/..') | |
->setDescription('Regenerate ids for each trans-unit'); | |
} | |
/** | |
* @param InputInterface $input | |
* @param OutputInterface $output | |
*/ | |
protected function execute(InputInterface $input, OutputInterface $output) | |
{ | |
$finder = new Finder(); | |
$finder->files()->in($input->getOption('path'))->name('*\.xliff'); | |
foreach ($finder as $file) { | |
$output->write(sprintf('<comment>%s</comment>', $file->getFilename())); | |
$xml = simplexml_load_file($file->getRealPath()); | |
foreach ($xml->file->body->{'trans-unit'} as $item) { | |
/** @var \SimpleXMLElement $item */ | |
$item->attributes()->{'id'} = uniqid(); | |
if ((string) $item->attributes()->{'id'} === '') { | |
$item->addAttribute('id', uniqid()); | |
} | |
} | |
$xml->saveXML($file->getRealPath()); | |
$output->writeln(sprintf('<info> OK</info>', $file->getFilename())); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment