|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the Doctrine Behavioral Extensions package. |
| 7 | + * (c) Gediminas Morkevicius <[email protected]> http://www.gediminasm.org |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace App\Command; |
| 13 | + |
| 14 | +use App\Entity\Category; |
| 15 | +use App\Entity\CategoryTranslation; |
| 16 | +use App\Entity\Repository\CategoryRepository; |
| 17 | +use Doctrine\ORM\Query; |
| 18 | +use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper; |
| 19 | +use Gedmo\Translatable\Query\TreeWalker\TranslationWalker; |
| 20 | +use Gedmo\Translatable\TranslatableListener; |
| 21 | +use Symfony\Component\Console\Command\Command; |
| 22 | +use Symfony\Component\Console\Input\InputInterface; |
| 23 | +use Symfony\Component\Console\Output\OutputInterface; |
| 24 | + |
| 25 | +final class PrintCategoryTranslationTreeCommand extends Command |
| 26 | +{ |
| 27 | + protected static $defaultName = 'app:print-category-translation-tree'; |
| 28 | + protected static $defaultDescription = 'Seeds an example category tree with translations and prints the tree.'; |
| 29 | + |
| 30 | + protected function configure(): void |
| 31 | + { |
| 32 | + // Kept for compatibility with Symfony 5.2 and older, which do not support lazy descriptions |
| 33 | + $this->setDescription(self::$defaultDescription); |
| 34 | + } |
| 35 | + |
| 36 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 37 | + { |
| 38 | + /** @var EntityManagerHelper $helper */ |
| 39 | + $helper = $this->getHelper('em'); |
| 40 | + |
| 41 | + $em = $helper->getEntityManager(); |
| 42 | + |
| 43 | + /** @var CategoryRepository $repository */ |
| 44 | + $repository = $em->getRepository(Category::class); |
| 45 | + |
| 46 | + /** @var Category|null $food */ |
| 47 | + $food = $repository->findOneByTitle('Food'); |
| 48 | + |
| 49 | + // If we don't have our examples in the database already, seed them |
| 50 | + if (null === $food) { |
| 51 | + $food = new Category(); |
| 52 | + $food->setTitle('Food'); |
| 53 | + $food->addTranslation(new CategoryTranslation('lt', 'title', 'Maistas')); |
| 54 | + |
| 55 | + $fruits = new Category(); |
| 56 | + $fruits->setParent($food); |
| 57 | + $fruits->setTitle('Fruits'); |
| 58 | + $fruits->addTranslation(new CategoryTranslation('lt', 'title', 'Vaisiai')); |
| 59 | + |
| 60 | + $apple = new Category(); |
| 61 | + $apple->setParent($fruits); |
| 62 | + $apple->setTitle('Apple'); |
| 63 | + $apple->addTranslation(new CategoryTranslation('lt', 'title', 'Obuolys')); |
| 64 | + |
| 65 | + $milk = new Category(); |
| 66 | + $milk->setParent($food); |
| 67 | + $milk->setTitle('Milk'); |
| 68 | + $milk->addTranslation(new CategoryTranslation('lt', 'title', 'Pienas')); |
| 69 | + |
| 70 | + $em->persist($food); |
| 71 | + $em->persist($milk); |
| 72 | + $em->persist($fruits); |
| 73 | + $em->persist($apple); |
| 74 | + $em->flush(); |
| 75 | + } |
| 76 | + |
| 77 | + // Create a query to fetch the tree nodes |
| 78 | + $query = $em->createQueryBuilder() |
| 79 | + ->select('node') |
| 80 | + ->from(Category::class, 'node') |
| 81 | + ->orderBy('node.root') |
| 82 | + ->addOrderBy('node.lft') |
| 83 | + ->getQuery() |
| 84 | + ; |
| 85 | + |
| 86 | + // Set the hint to translate nodes |
| 87 | + $query->setHint( |
| 88 | + Query::HINT_CUSTOM_OUTPUT_WALKER, |
| 89 | + TranslationWalker::class |
| 90 | + ); |
| 91 | + |
| 92 | + $treeDecorationOptions = [ |
| 93 | + 'decorate' => true, |
| 94 | + 'rootOpen' => '', |
| 95 | + 'rootClose' => '', |
| 96 | + 'childOpen' => '', |
| 97 | + 'childClose' => '', |
| 98 | + 'nodeDecorator' => static function ($node): string { |
| 99 | + return str_repeat('-', $node['level']).$node['title'].PHP_EOL; |
| 100 | + }, |
| 101 | + ]; |
| 102 | + |
| 103 | + // Build the tree in English |
| 104 | + $output->writeln('English:'); |
| 105 | + $output->writeln($repository->buildTree($query->getArrayResult(), $treeDecorationOptions)); |
| 106 | + |
| 107 | + // Change the locale and build the tree in Lithuanian |
| 108 | + $query->setHint(TranslatableListener::HINT_TRANSLATABLE_LOCALE, 'lt'); |
| 109 | + $output->writeln('Lithuanian:'); |
| 110 | + $output->writeln($repository->buildTree($query->getArrayResult(), $treeDecorationOptions)); |
| 111 | + |
| 112 | + return 0; |
| 113 | + } |
| 114 | +} |
0 commit comments