/**
  * {@inheritdoc}
  */
 public function getResult(array $params)
 {
     if (isset($params['filter']) && !is_array($params['filter'])) {
         $params['filter'] = array();
     }
     $total = $this->getCount($params);
     $entities = array();
     if ($total > 0) {
         $entities = $this->getPersistenceHandler()->query(TranslationToken::clazz(), $params);
     }
     return array('success' => true, 'items' => $entities, 'total' => $total);
 }
 public function testImport()
 {
     $tokens = self::$em->getRepository(TranslationToken::clazz())->findAll();
     $this->assertEquals(0, count($tokens));
     $this->launchImportCommand();
     $tokens = self::$em->getRepository(TranslationToken::clazz())->findAll();
     $this->assertEquals(2, count($tokens));
     $token = self::$em->getRepository(TranslationToken::clazz())->findOneBy(array('source' => 'template'));
     $this->assertToken($token);
     $token = self::$em->getRepository(TranslationToken::clazz())->findOneBy(array('source' => 'php-classes'));
     $this->assertToken($token);
     $token = self::$em->getRepository(TranslationToken::clazz())->findOneBy(array('source' => 'undefined'));
     $this->assertFalse($token instanceof TranslationToken);
 }
 public static function doTearDownAfterClass()
 {
     self::$st->dropSchema([self::$em->getClassMetadata(Language::clazz())]);
     self::$st->dropSchema([self::$em->getClassMetadata(TranslationToken::clazz())]);
     self::$st->dropSchema([self::$em->getClassMetadata(LanguageTranslationToken::clazz())]);
 }
 public static function dropDatabase()
 {
     self::$st->dropSchema([self::$em->getClassMetadata(Language::clazz())]);
     self::$st->dropSchema([self::$em->getClassMetadata(TranslationToken::clazz())]);
     self::$st->dropSchema([self::$em->getClassMetadata(LanguageTranslationToken::clazz())]);
 }
 /**
  * @param $source
  * @param $bundleName
  * @param $domain
  * @param $tokenName
  *
  * @return TranslationToken
  */
 private function findOrCreateTranslationToken($source, $bundleName, $domain, $tokenName)
 {
     /* @var EntityManager $em */
     $em = $this->getContainer()->get('doctrine.orm.entity_manager');
     $token = $em->getRepository(TranslationToken::clazz())->findOneBy(array('source' => $source, 'bundleName' => $bundleName, 'domain' => $domain, 'tokenName' => $tokenName));
     if (!$token) {
         $token = new TranslationToken();
         $token->setSource($source);
         $token->setBundleName($bundleName);
         $token->setDomain($domain);
         $token->setTokenName($tokenName);
         $em->persist($token);
         $em->flush();
     }
     return $token;
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $outputFormat = 'yml';
     /* @var EntityManager $em */
     $em = $this->getContainer()->get('doctrine.orm.entity_manager');
     /* @var TranslationWriter $writer */
     $writer = $this->getContainer()->get('translation.writer');
     // check format
     $supportedFormats = $writer->getFormats();
     if (!in_array($outputFormat, $supportedFormats)) {
         $output->writeln('<error>Wrong output format</error>');
         $output->writeln('>>> Supported formats are ' . implode(', ', $supportedFormats) . '.');
         return 1;
     }
     $tokens = $em->getRepository(TranslationToken::clazz())->findBy(array('isObsolete' => false));
     $bundles = array();
     /* @var TranslationToken $token */
     foreach ($tokens as $token) {
         $bundleName = $token->getBundleName();
         if (!isset($bundles[$bundleName])) {
             $bundles[$bundleName] = array();
         }
         $ltts = $token->getLanguageTranslationTokens();
         /* @var LanguageTranslationToken $ltt */
         foreach ($ltts as $ltt) {
             if (!$ltt->getLanguage()->getEnabled()) {
                 continue;
             }
             $locale = $ltt->getLanguage()->getLocale();
             if (!isset($bundles[$bundleName][$locale])) {
                 $bundles[$bundleName][$locale] = new MessageCatalogue($locale);
             }
             $catalogue = $bundles[$bundleName][$locale];
             $catalogue->set($token->getTokenName(), $ltt->getTranslation(), $token->getDomain());
         }
     }
     if (count($bundles)) {
         $fs = new Filesystem();
         $resourcesDir = 'app/Resources';
         $basePath = dirname($this->getContainer()->get('kernel')->getRootdir());
         foreach ($bundles as $bundleName => $catalogues) {
             if (!count($catalogues)) {
                 continue;
             }
             $bundleTransDir = $resourcesDir . '/translations' . '/' . $bundleName;
             $bundleTransPath = $basePath . '/' . $bundleTransDir;
             $output->writeln('>>> ' . $bundleName . ': ' . $bundleTransDir);
             if ($fs->exists($bundleTransPath)) {
                 $output->writeln('    <fg=red>Removing old files</>');
                 $fs->remove($bundleTransPath);
             }
             try {
                 if (!$fs->exists(dirname($bundleTransPath))) {
                     $fs->mkdir(dirname($bundleTransPath));
                     $fs->chmod(dirname($bundleTransPath), 0777);
                 }
                 $fs->mkdir($bundleTransPath);
             } catch (IOExceptionInterface $e) {
                 echo 'An error occurred while creating your directory at ' . $e->getPath();
             }
             $output->writeln('    <fg=green>Creating new files</>');
             foreach ($catalogues as $locale => $catalogue) {
                 $writer->writeTranslations($catalogue, $outputFormat, array('path' => $bundleTransPath));
             }
             $fs->chmod($bundleTransPath, 0777, 00, true);
         }
         $output->writeln('>>> Translations have been successfully compiled');
     } else {
         $output->writeln('>>> Nothing to compile');
     }
 }
Пример #7
0
 /**
  * @return array
  */
 public function getConfig()
 {
     return array('entity' => TranslationToken::clazz(), 'hydration' => array('groups' => array('list' => ['id', 'source', 'bundleName', 'domain', 'tokenName', 'isObsolete', 'translations']), 'profiles' => array('list')));
 }