em = $em; $this->userManager = $userManager; $this->tokenStorage = $tokenStorage; $this->projectDir = $kernel->getProjectDir(); } protected function configure() { $this // the name of the command (the part after "app/console") ->setName('zitec:normalize-translations') // the short description shown while running "php app/console list" ->setDescription('Normalizes the translations.') // the full command description shown when running the command with // the "--help" option ->setHelp('This command allows you to normalize the translations...') ; } /** * @param InputInterface $input * @param OutputInterface $output */ protected function execute(InputInterface $input, OutputInterface $output) { // all the translatable classes $classes = [ \App\Entity\BussinessStage::class, \App\Entity\Indicador::class, \App\Entity\Contador::class, \App\Entity\Formulario::class, \App\Entity\Preguntas::class, \App\Entity\PreguntasFormulario::class, \App\Entity\Recursos::class, \App\Entity\Sector::class, \App\Entity\Tarea::class, \App\Entity\TiposRecursos::class, ]; $defaultLanguages = ['fr','ar']; // fake language foreach ($classes as $class) { foreach($defaultLanguages as $defaultLanguage) $this->processClass($class, $output,$defaultLanguage); } } /** * @param $class * @param $output * @param $defaultLanguage * @throws \ReflectionException */ private function processClass($class, $output,$defaultLanguage) { $output->writeln(sprintf('Processing class %s', $class)); // gets all the properties $properties = $this->getProperties($class); // gets the translatable properties $translatableProperties = $this->getTranslatableProperties($properties, $class); $output->writeln(sprintf('Found %d translatable properties: %s', count($translatableProperties), implode(', ', $translatableProperties))); $em = $this->em; //$repository = $em->getRepository('Gedmo\\Translatable\\Entity\\Translation'); $items = $em->getRepository($class)->findAll(); $propertyAccessor = PropertyAccess::createPropertyAccessor(); $annotationReader = new AnnotationReader(); //Get class annotation $reflectionClass = new \ReflectionClass($class); $classAnnotations = $annotationReader->getClassAnnotations($reflectionClass); $translationclass=null; foreach($classAnnotations as $entity){ $entity=(array)$entity; if(!empty($entity['class']) && strpos($entity['class'], 'Translation')!==false){ $translationclass=$entity['class']; } } $output->writeln(sprintf('Translation class: %s', $translationclass)); foreach ($items as $item) { foreach ($translatableProperties as $translatableProperty) { $value = $propertyAccessor->getValue($item, $translatableProperty); $translations = $item->getTranslations(); $exists=false; foreach($translations as $translation) { if($translation->getLocale()==$defaultLanguage && !empty($translation->getContent())) $exists=true; } if (!$exists) { $item->addTranslation(new $translationclass($defaultLanguage,$translatableProperty, $value)); } } } $em->flush(); } private function getProperties($class) { $phpDocExtractor = new PhpDocExtractor(); $reflectionExtractor = new ReflectionExtractor(); // array of PropertyListExtractorInterface $listExtractors = array($reflectionExtractor); // array of PropertyTypeExtractorInterface $typeExtractors = array($phpDocExtractor, $reflectionExtractor); // array of PropertyDescriptionExtractorInterface $descriptionExtractors = array($phpDocExtractor); // array of PropertyAccessExtractorInterface $accessExtractors = array($reflectionExtractor); $propertyInfo = new PropertyInfoExtractor( $listExtractors, $typeExtractors, $descriptionExtractors, $accessExtractors ); return $propertyInfo->getProperties($class); } private function getTranslatableProperties($properties, $class) { $translatableProperties = []; // https://gist.github.com/Swop/5990316 $annotationReader = new AnnotationReader(); foreach ($properties as $property) { try { $reflectionProperty = new \ReflectionProperty($class, $property); $propertyAnnotations = $annotationReader->getPropertyAnnotations($reflectionProperty); foreach ($propertyAnnotations as $propertyAnnotation) { if ($propertyAnnotation instanceof Gedmo\Translatable) { // this property is translatable $translatableProperties[] = $property; } } } catch (\ReflectionException $e) { // missing property continue; } } return $translatableProperties; } }