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

Commit 34dfd55

Browse files
committed
Merge pull request #17 from dantleech/refresh_command
Refresh command
2 parents 8ed8208 + 7d93486 commit 34dfd55

File tree

5 files changed

+168
-1
lines changed

5 files changed

+168
-1
lines changed

AutoRoute/Factory.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ public function hasMapping($classFqn)
116116
return isset($this->mapping[$classFqn]);
117117
}
118118

119+
/**
120+
* Return all the mapping data
121+
*
122+
* @return array
123+
*/
124+
public function getMappings()
125+
{
126+
return $this->mapping;
127+
}
128+
119129
protected function generateRouteStackChain($classFqn)
120130
{
121131
$mapping = $this->getMapping($classFqn);

AutoRoute/RouteMaker/AutoRouteMaker.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function make(RouteStack $routeStack)
2929

3030
$autoRoute = $this->getAutoRouteForDocument($content);
3131

32-
if (null === $autoRoute) {
32+
if (!$autoRoute) {
3333
$autoRoute = new AutoRoute;
3434
$autoRoute->setParent($context->getTopRoute());
3535
$autoRoute->setRouteContent($content);

Command/RefreshCommand.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\Command;
4+
5+
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6+
use Symfony\Component\Console\Input\InputInterface;
7+
use Symfony\Component\Console\Output\OutputInterface;
8+
use Symfony\Component\Console\Input\InputOption;
9+
use Doctrine\Bundle\PHPCRBundle\Command\DoctrineCommandHelper;
10+
11+
class RefreshCommand extends ContainerAwareCommand
12+
{
13+
public function configure()
14+
{
15+
$this
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');
39+
}
40+
41+
/**
42+
* {@inheritDoc}
43+
*/
44+
public function execute(InputInterface $input, OutputInterface $output)
45+
{
46+
$container = $this->getContainer();
47+
$dm = $container->get('doctrine_phpcr.odm.default_document_manager');
48+
$factory = $container->get('symfony_cmf_routing_auto.factory');
49+
$arm = $container->get('symfony_cmf_routing_auto.auto_route_manager');
50+
$uow = $dm->getUnitOfWork();
51+
52+
$session = $input->getOption('session');
53+
$dryRun = $input->getOption('dry-run');
54+
$class = $input->getOption('class');
55+
$verbose = $input->getOption('verbose');
56+
57+
DoctrineCommandHelper::setApplicationPHPCRSession(
58+
$this->getApplication(),
59+
$session
60+
);
61+
62+
if ($class) {
63+
$mapping = array($class => $class);
64+
} else {
65+
$mapping = $factory->getMappings();
66+
}
67+
68+
foreach (array_keys($mapping) as $classFqn) {
69+
70+
$output->writeln(sprintf('<info>Processing class: </info> %s', $classFqn));
71+
72+
$qb = $dm->createQueryBuilder();
73+
$qb->from($classFqn);
74+
$q = $qb->getQuery();
75+
$result = $q->getResult();
76+
77+
foreach ($result as $autoRouteableDocument) {
78+
$id = $uow->getDocumentId($autoRouteableDocument);
79+
$output->writeln(' <info>Refreshing: </info>'.$id);
80+
$context = $arm->updateAutoRouteForDocument($autoRouteableDocument);
81+
82+
foreach ($context->getRoutes() as $route) {
83+
$dm->persist($route);
84+
$routeId = $uow->getDocumentId($route);
85+
86+
if ($verbose) {
87+
$output->writeln(sprintf(
88+
'<comment> - %sPersisting: </comment> %s <comment>%s</comment>',
89+
$dryRun ? '(dry run) ' : '',
90+
$routeId,
91+
'[...]'.substr(get_class($route), -10)
92+
));
93+
}
94+
95+
if (true !== $dryRun) {
96+
$dm->flush();
97+
}
98+
}
99+
}
100+
}
101+
}
102+
}

Tests/Functional/BaseTestCase.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
require __DIR__.'/app/AppKernel.php';
66

77
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
8+
use Symfony\Bundle\FrameworkBundle\Console\Application;
89

910
class BaseTestCase extends WebTestCase
1011
{
@@ -43,5 +44,11 @@ public function setUp(array $options = array(), $routebase = null)
4344

4445
$session->save();
4546
}
47+
48+
public function getApplication()
49+
{
50+
$application = new Application(self::$kernel);
51+
return $application;
52+
}
4653
}
4754

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Functional\Command;
4+
5+
use Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Functional\app\Document\Blog;
6+
use Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Functional\BaseTestCase;
7+
use Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Functional\app\Document\Post;
8+
use Symfony\Component\Console\Input\ArrayInput;
9+
use Symfony\Component\Console\Output\NullOutput;
10+
use Symfony\Cmf\Bundle\RoutingAutoBundle\Command\RefreshCommand;
11+
use Symfony\Component\Console\Output\StreamOutput;
12+
13+
class RefreshCommandTest extends BaseTestCase
14+
{
15+
protected function createBlog($withPosts = false)
16+
{
17+
$blog = new Blog;
18+
$blog->path = '/test/test-blog';
19+
$blog->title = 'Unit testing blog';
20+
21+
$this->getDm()->persist($blog);
22+
23+
if ($withPosts) {
24+
$post = new Post;
25+
$post->title = 'This is a post title';
26+
$post->blog = $blog;
27+
$this->getDm()->persist($post);
28+
}
29+
30+
$this->getDm()->flush();
31+
$this->getDm()->clear();
32+
}
33+
34+
public function testCommand()
35+
{
36+
$this->createBlog(true);
37+
38+
$application = $this->getApplication();
39+
$input = new ArrayInput(array(
40+
'foo:bar'
41+
));
42+
$output = new NullOutput();
43+
//$output = new StreamOutput(fopen('php://stdout', 'w'));
44+
$command = new RefreshCommand();
45+
$command->setApplication($application);
46+
$command->run($input, $output);
47+
}
48+
}

0 commit comments

Comments
 (0)