Skip to content

Commit 3cf1b67

Browse files
mbabkerphansys
authored andcommitted
Refresh the example app code
1 parent daebbcf commit 3cf1b67

File tree

9 files changed

+216
-205
lines changed

9 files changed

+216
-205
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ To set up and run example, follow these steps:
111111
- download composer: `wget https://getcomposer.org/composer.phar`
112112
- install dev libraries: `php composer.phar install`
113113
- edit `example/em.php` and configure your database on top of the file
114-
- run: `./example/bin/console` or `php example/bin/console` for console commands
115-
- run: `./example/bin/console orm:schema-tool:create` to create schema
116-
- run: `php example/run.php` to run example
114+
- run: `php example/bin/console` or `php example/bin/console` for console commands
115+
- run: `php example/bin/console orm:schema-tool:create` to create the schema
116+
- run: `php example/bin/console app:print-category-translation-tree` to run the example to print the category translation tree
117117

118118
### Contributors
119119

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@
6060
"phpstan/phpstan-doctrine": "^1.0",
6161
"phpstan/phpstan-phpunit": "^1.0",
6262
"phpunit/phpunit": "^8.5 || ^9.5",
63-
"symfony/cache": "^4.4 || ^5.3",
64-
"symfony/yaml": "^4.4 || ^5.3"
63+
"symfony/cache": "^4.4 || ^5.3 || ^6.0",
64+
"symfony/console": "^4.4 || ^5.3 || ^6.0",
65+
"symfony/yaml": "^4.4 || ^5.3 || ^6.0"
6566
},
6667
"suggest": {
6768
"doctrine/mongodb-odm": "to use the extensions with the MongoDB ODM",
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
}

example/app/Entity/Category.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Entity;
12+
namespace App\Entity;
1313

1414
use Doctrine\Common\Collections\ArrayCollection;
1515
use Doctrine\ORM\Mapping as ORM;
@@ -18,8 +18,8 @@
1818
/**
1919
* @Gedmo\Tree(type="nested")
2020
* @ORM\Table(name="ext_categories")
21-
* @ORM\Entity(repositoryClass="Entity\Repository\CategoryRepository")
22-
* @Gedmo\TranslationEntity(class="Entity\CategoryTranslation")
21+
* @ORM\Entity(repositoryClass="App\Entity\Repository\CategoryRepository")
22+
* @Gedmo\TranslationEntity(class="App\Entity\CategoryTranslation")
2323
*/
2424
class Category
2525
{

example/app/Entity/CategoryTranslation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Entity;
12+
namespace App\Entity;
1313

1414
use Doctrine\ORM\Mapping as ORM;
1515
use Gedmo\Translatable\Entity\MappedSuperclass\AbstractPersonalTranslation;

example/app/Entity/Repository/CategoryRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Entity\Repository;
12+
namespace App\Entity\Repository;
1313

1414
use Gedmo\Tree\Entity\Repository\NestedTreeRepository;
1515

example/bin/console.php

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,21 @@
99
* file that was distributed with this source code.
1010
*/
1111

12+
/** @var \Doctrine\ORM\EntityManager $em */
1213
$em = include __DIR__.'/../em.php';
1314

14-
$cli = new Symfony\Component\Console\Application('My CLI interface', '1.0.0');
15+
$entityManagerProvider = new \Doctrine\ORM\Tools\Console\EntityManagerProvider\SingleManagerProvider($em);
16+
17+
$cli = new \Symfony\Component\Console\Application('Doctrine Extensions Example Application', \Gedmo\DoctrineExtensions::VERSION);
1518
$cli->setCatchExceptions(true);
16-
// commands
19+
$cli->setHelperSet(\Doctrine\ORM\Tools\Console\ConsoleRunner::createHelperSet($em));
20+
21+
// Use the ORM's console runner to register the default commands available from the DBAL and ORM for the environment
22+
\Doctrine\ORM\Tools\Console\ConsoleRunner::addCommands($cli, $entityManagerProvider);
23+
24+
// Register our example app commands
1725
$cli->addCommands([
18-
// DBAL Commands
19-
new Doctrine\DBAL\Tools\Console\Command\RunSqlCommand(),
20-
new Doctrine\DBAL\Tools\Console\Command\ImportCommand(),
21-
22-
// ORM Commands
23-
new Doctrine\ORM\Tools\Console\Command\ClearCache\MetadataCommand(),
24-
new Doctrine\ORM\Tools\Console\Command\ClearCache\ResultCommand(),
25-
new Doctrine\ORM\Tools\Console\Command\ClearCache\QueryCommand(),
26-
new Doctrine\ORM\Tools\Console\Command\SchemaTool\CreateCommand(),
27-
new Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand(),
28-
new Doctrine\ORM\Tools\Console\Command\SchemaTool\DropCommand(),
29-
new Doctrine\ORM\Tools\Console\Command\EnsureProductionSettingsCommand(),
30-
new Doctrine\ORM\Tools\Console\Command\ConvertDoctrine1SchemaCommand(),
31-
new Doctrine\ORM\Tools\Console\Command\GenerateRepositoriesCommand(),
32-
new Doctrine\ORM\Tools\Console\Command\GenerateEntitiesCommand(),
33-
new Doctrine\ORM\Tools\Console\Command\GenerateProxiesCommand(),
34-
new Doctrine\ORM\Tools\Console\Command\ConvertMappingCommand(),
35-
new Doctrine\ORM\Tools\Console\Command\RunDqlCommand(),
36-
new Doctrine\ORM\Tools\Console\Command\ValidateSchemaCommand(),
26+
new \App\Command\PrintCategoryTranslationTreeCommand(),
3727
]);
38-
// helpers
39-
$helpers = [
40-
'db' => new Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()),
41-
'em' => new Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em),
42-
];
43-
foreach ($helpers as $name => $helper) {
44-
$cli->getHelperSet()->set($helper, $name);
45-
}
4628

4729
return $cli;

0 commit comments

Comments
 (0)