Skip to content
This repository was archived by the owner on Sep 16, 2021. It is now read-only.

Commit d851a55

Browse files
committed
Improved command
1 parent c5fdcfa commit d851a55

File tree

1 file changed

+56
-11
lines changed

1 file changed

+56
-11
lines changed

Command/RefreshCommand.php

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,50 +5,95 @@
55
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
66
use Symfony\Component\Console\Input\InputInterface;
77
use Symfony\Component\Console\Output\OutputInterface;
8+
use Symfony\Component\Console\Input\InputOption;
9+
use Doctrine\Bundle\PHPCRBundle\Command\DoctrineCommandHelper;
810

911
class RefreshCommand extends ContainerAwareCommand
1012
{
1113
public function configure()
1214
{
1315
$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');
1739
}
1840

1941
/**
2042
* {@inheritDoc}
2143
*/
2244
public function execute(InputInterface $input, OutputInterface $output)
2345
{
46+
2447
$container = $this->getContainer();
2548
$dm = $container->get('doctrine_phpcr.odm.default_document_manager');
2649
$factory = $container->get('symfony_cmf_routing_auto.factory');
2750
$arm = $container->get('symfony_cmf_routing_auto.auto_route_manager');
2851
$uow = $dm->getUnitOfWork();
52+
$session = $input->getOption('session');
2953

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+
}
3168

3269
foreach (array_keys($mapping) as $classFqn) {
3370

71+
$output->writeln(sprintf('<info>Processing class: </info> %s', $classFqn));
3472
$qb = $dm->createQueryBuilder();
3573
$qb->from($classFqn);
3674
$q = $qb->getQuery();
3775
$result = $q->getResult();
3876

3977
foreach ($result as $autoRouteableDocument) {
4078
$id = $uow->getDocumentId($autoRouteableDocument);
41-
$output->writeln('<info>Refreshing: </info>'.$id);
79+
$output->writeln(' <info>Refreshing: </info>'.$id);
4280
$context = $arm->updateAutoRouteForDocument($autoRouteableDocument);
4381
foreach ($context->getRoutes() as $route) {
4482
$dm->persist($route);
4583
$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+
}
5297
}
5398
}
5499
}

0 commit comments

Comments
 (0)