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

Commit 428ca3a

Browse files
committed
Added refresh command
1 parent 3030eea commit 428ca3a

File tree

5 files changed

+113
-1
lines changed

5 files changed

+113
-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: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
9+
class RefreshCommand extends ContainerAwareCommand
10+
{
11+
public function configure()
12+
{
13+
$this
14+
->setName('cmf:routing:auto:refresh')
15+
->setDescription('Refresh all auto routes')
16+
;
17+
}
18+
19+
/**
20+
* {@inheritDoc}
21+
*/
22+
public function execute(InputInterface $input, OutputInterface $output)
23+
{
24+
$container = $this->getContainer();
25+
$dm = $container->get('doctrine_phpcr.odm.default_document_manager');
26+
$factory = $container->get('symfony_cmf_routing_auto.factory');
27+
$arm = $container->get('symfony_cmf_routing_auto.auto_route_manager');
28+
$uow = $dm->getUnitOfWork();
29+
30+
$mapping = $factory->getMappings();
31+
32+
foreach (array_keys($mapping) as $classFqn) {
33+
34+
$qb = $dm->createQueryBuilder();
35+
$qb->from($classFqn);
36+
$q = $qb->getQuery();
37+
$result = $q->getResult();
38+
39+
foreach ($result as $autoRouteableDocument) {
40+
$id = $uow->getDocumentId($autoRouteableDocument);
41+
$output->writeln('<info>Refreshing: </info>'.$id);
42+
$context = $arm->updateAutoRouteForDocument($autoRouteableDocument);
43+
foreach ($context->getRoutes() as $route) {
44+
$dm->persist($route);
45+
$routeId = $uow->getDocumentId($route);
46+
$output->writeln('<comment> - Persisting: </comment>'.$routeId);
47+
}
48+
$dm->flush();
49+
}
50+
}
51+
}
52+
}

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\Component\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: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
10+
class RefreshCommandTest extends BaseTestCase
11+
{
12+
public function setUp()
13+
{
14+
$this->createBlog(true);
15+
}
16+
17+
protected function createBlog($withPosts = false)
18+
{
19+
$blog = new Blog;
20+
$blog->path = '/test/test-blog';
21+
$blog->title = 'Unit testing blog';
22+
23+
$this->getDm()->persist($blog);
24+
25+
if ($withPosts) {
26+
$post = new Post;
27+
$post->title = 'This is a post title';
28+
$post->blog = $blog;
29+
$this->getDm()->persist($post);
30+
}
31+
32+
$this->getDm()->flush();
33+
$this->getDm()->clear();
34+
}
35+
36+
public function testCommand()
37+
{
38+
$application = $this->getApplication();
39+
$input = new ArrayInput;
40+
$application->run($input);
41+
}
42+
}
43+

0 commit comments

Comments
 (0)