This repository was archived by the owner on Sep 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathRefreshCommand.php
119 lines (98 loc) · 4.09 KB
/
RefreshCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
/*
* This file is part of the Symfony CMF package.
*
* (c) 2011-2015 Symfony CMF
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;
use Doctrine\Bundle\PHPCRBundle\Command\DoctrineCommandHelper;
use Symfony\Cmf\Component\RoutingAuto\UriContextCollection;
class RefreshCommand extends ContainerAwareCommand
{
public function configure()
{
$this
->setName('cmf:routing:auto:refresh')
->setDescription('Refresh auto-routeable documents')
->setHelp(<<<'HERE'
WARNING: Experimental!
This command iterates over all Documents that are mapped by the auto
routing system and re-applys the auto routing logic.
You can specify the "--verbose" option to output detail for each created
route.
Specify the "--dry-run" option to not write any changes to the database.
Use "--class" to only apply the changes to a single class - although beware this
may cause an error if you persist a class whose auto routing configuration
relies on the auto routing of another class.
HERE
);
$this->addOption('dry-run', null, InputOption::VALUE_NONE,
'Do not write any change to the database.'
);
$this->addOption('class', null, InputOption::VALUE_REQUIRED,
'Only update the given class FQN'
);
$this->addOption('session', null, InputOption::VALUE_OPTIONAL, 'The session to use for this command');
}
/**
* {@inheritdoc}
*/
public function execute(InputInterface $input, OutputInterface $output)
{
$container = $this->getContainer();
$manager = $container->get('doctrine_phpcr');
$factory = $container->get('cmf_routing_auto.metadata.factory');
$arm = $container->get('cmf_routing_auto.auto_route_manager');
$dm = $manager->getManager();
$uow = $dm->getUnitOfWork();
$session = $input->getOption('session');
$dryRun = $input->getOption('dry-run');
$class = $input->getOption('class');
$verbose = $input->getOption('verbose');
DoctrineCommandHelper::setApplicationPHPCRSession(
$this->getApplication(),
$session
);
if ($class) {
$mapping = array($class => $class);
} else {
$mapping = iterator_to_array($factory->getIterator());
}
foreach (array_keys($mapping) as $classFqn) {
$output->writeln(sprintf('<info>Processing class: </info> %s', $classFqn));
$qb = $dm->createQueryBuilder();
$qb->from()->document($classFqn, 'a');
$q = $qb->getQuery();
$result = $q->getResult();
foreach ($result as $autoRouteableDocument) {
$id = $uow->getDocumentId($autoRouteableDocument);
$output->writeln(' <info>Refreshing: </info>'.$id);
$uriContextCollection = new UriContextCollection($autoRouteableDocument);
$arm->buildUriContextCollection($uriContextCollection);
foreach ($uriContextCollection->getUriContexts() as $uriContext) {
$autoRoute = $uriContext->getAutoRoute();
$dm->persist($autoRoute);
$autoRouteId = $uow->getDocumentId($autoRoute);
if ($verbose) {
$output->writeln(sprintf(
'<comment> - %sPersisting: </comment> %s <comment>%s</comment>',
$dryRun ? '(dry run) ' : '',
$autoRouteId,
'[...]'.substr(get_class($autoRoute), -10)
));
}
if (true !== $dryRun) {
$dm->flush();
}
}
}
}
}
}