|
5 | 5 | use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
|
6 | 6 | use Symfony\Component\Console\Input\InputInterface;
|
7 | 7 | use Symfony\Component\Console\Output\OutputInterface;
|
| 8 | +use Symfony\Component\Console\Input\InputOption; |
| 9 | +use Doctrine\Bundle\PHPCRBundle\Command\DoctrineCommandHelper; |
8 | 10 |
|
9 | 11 | class RefreshCommand extends ContainerAwareCommand
|
10 | 12 | {
|
11 | 13 | public function configure()
|
12 | 14 | {
|
13 | 15 | $this
|
14 |
| - ->setName('cmf:routing:auto:refresh') |
15 |
| - ->setDescription('Refresh all auto routes') |
16 |
| - ; |
| 16 | + ->setName('cmf:routing-auto:refresh') |
| 17 | + ->setDescription(<<<HERE |
| 18 | +This command iterates over all Documents that are mapped by the auto |
| 19 | +routing system and re-applys the auto routing logic. |
| 20 | +
|
| 21 | +You can specify the "--verbose" option to output detail for each created |
| 22 | +route. |
| 23 | +
|
| 24 | +Specify the "--dry-run" option to not write any changes to the database. |
| 25 | +
|
| 26 | +Use "--class" to only apply the changes to a single class - although beware this |
| 27 | +may cause an error if you persist a class whose auto routing configuration |
| 28 | +relies on the auto routing of another class. |
| 29 | +HERE |
| 30 | + ); |
| 31 | + |
| 32 | + $this->addOption('dry-run', null, InputOption::VALUE_NONE, |
| 33 | + 'Do not write any change to the database.' |
| 34 | + ); |
| 35 | + $this->addOption('class', null, InputOption::VALUE_REQUIRED, |
| 36 | + 'Only update the given class FQN' |
| 37 | + ); |
| 38 | + $this->addOption('session', null, InputOption::VALUE_OPTIONAL, 'The session to use for this command'); |
17 | 39 | }
|
18 | 40 |
|
19 | 41 | /**
|
20 | 42 | * {@inheritDoc}
|
21 | 43 | */
|
22 | 44 | public function execute(InputInterface $input, OutputInterface $output)
|
23 | 45 | {
|
| 46 | + |
24 | 47 | $container = $this->getContainer();
|
25 | 48 | $dm = $container->get('doctrine_phpcr.odm.default_document_manager');
|
26 | 49 | $factory = $container->get('symfony_cmf_routing_auto.factory');
|
27 | 50 | $arm = $container->get('symfony_cmf_routing_auto.auto_route_manager');
|
28 | 51 | $uow = $dm->getUnitOfWork();
|
| 52 | + $session = $input->getOption('session'); |
29 | 53 |
|
30 |
| - $mapping = $factory->getMappings(); |
| 54 | + DoctrineCommandHelper::setApplicationPHPCRSession( |
| 55 | + $this->getApplication(), |
| 56 | + $session |
| 57 | + ); |
| 58 | + |
| 59 | + $dryRun = $input->getOption('dry-run'); |
| 60 | + $class = $input->getOption('class'); |
| 61 | + $verbose = $input->getOption('verbose'); |
| 62 | + |
| 63 | + if ($class) { |
| 64 | + $mapping = array($class => $class); |
| 65 | + } else { |
| 66 | + $mapping = $factory->getMappings(); |
| 67 | + } |
31 | 68 |
|
32 | 69 | foreach (array_keys($mapping) as $classFqn) {
|
33 | 70 |
|
| 71 | + $output->writeln(sprintf('<info>Processing class: </info> %s', $classFqn)); |
34 | 72 | $qb = $dm->createQueryBuilder();
|
35 | 73 | $qb->from($classFqn);
|
36 | 74 | $q = $qb->getQuery();
|
37 | 75 | $result = $q->getResult();
|
38 | 76 |
|
39 | 77 | foreach ($result as $autoRouteableDocument) {
|
40 | 78 | $id = $uow->getDocumentId($autoRouteableDocument);
|
41 |
| - $output->writeln('<info>Refreshing: </info>'.$id); |
| 79 | + $output->writeln(' <info>Refreshing: </info>'.$id); |
42 | 80 | $context = $arm->updateAutoRouteForDocument($autoRouteableDocument);
|
43 | 81 | foreach ($context->getRoutes() as $route) {
|
44 | 82 | $dm->persist($route);
|
45 | 83 | $routeId = $uow->getDocumentId($route);
|
46 |
| - $output->writeln(sprintf( |
47 |
| - '<comment> - Persisting: </comment> %s <comment>%s</comment>', |
48 |
| - $routeId, |
49 |
| - '[...]'.substr(get_class($route), -10) |
50 |
| - )); |
51 |
| - $dm->flush(); |
| 84 | + |
| 85 | + if ($verbose) { |
| 86 | + $output->writeln(sprintf( |
| 87 | + '<comment> - %sPersisting: </comment> %s <comment>%s</comment>', |
| 88 | + $dryRun ? '(dry run) ' : '', |
| 89 | + $routeId, |
| 90 | + '[...]'.substr(get_class($route), -10) |
| 91 | + )); |
| 92 | + } |
| 93 | + |
| 94 | + if (true !== $dryRun) { |
| 95 | + $dm->flush(); |
| 96 | + } |
52 | 97 | }
|
53 | 98 | }
|
54 | 99 | }
|
|
0 commit comments