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

Commit 4323bfc

Browse files
committed
Subscriber now poerted over from RoutingExtra PR
1 parent 709ca61 commit 4323bfc

File tree

16 files changed

+206
-1054
lines changed

16 files changed

+206
-1054
lines changed

AutoRoute/AutoRouteManager.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Metadata\MetadataFactoryInterface;
77
use Symfony\Cmf\Bundle\RoutingAutoRouteBundle\Document\AutoRoute;
88
use Symfony\Cmf\Bundle\CoreBundle\Slugifier\SlugifierInterface;
9+
use PHPCR\Util\NodeHelper;
910

1011
/**
1112
* This class is concerned with the automatic creation of route objects.
@@ -16,12 +17,11 @@
1617
class AutoRouteManager
1718
{
1819
protected $dm;
20+
protected $phpcrSession;
1921
protected $mapping;
2022
protected $defaultPath;
2123
protected $slugifier;
2224

23-
protected $metadata;
24-
2525
/**
2626
* @TODO: Should defaultPath be contained in a service to
2727
* enable this property to be modified at runtime?
@@ -43,6 +43,7 @@ public function __construct(
4343
$this->mapping = $mapping;
4444
$this->slugifier = $slugifier;
4545
$this->defaultPath = $defaultPath;
46+
$this->phpcrSession = $dm->getPhpcrSession();
4647
}
4748

4849
/**
@@ -99,9 +100,13 @@ public function removeAutoRoutesForDocument($document)
99100
*/
100101
public function isAutoRouteable($document)
101102
{
102-
$metadata = $this->metadataFactory->getMetadataForClass(get_class($document));
103+
foreach ($this->mapping as $classFqn => $metadata) {
104+
if ($document instanceof $classFqn) {
105+
return true;
106+
}
107+
}
103108

104-
return $metadata->autoRouteable == 1 ? true : false;
109+
return false;
105110
}
106111

107112
/**
@@ -142,6 +147,13 @@ protected function getParentRoute($document)
142147
{
143148
$metadata = $this->getMetadata($document);
144149
$defaultPath = $metadata['base_path'] ? : $this->defaultPath;
150+
151+
if ($metadata['base_path_auto_create']) {
152+
if (!$this->phpcrSession->nodeExists($defaultPath)) {
153+
NodeHelper::createPath($this->phpcrSession, $defaultPath);
154+
}
155+
}
156+
145157
$parent = $this->dm->find(null, $defaultPath);
146158

147159
if (!$parent) {
@@ -183,7 +195,6 @@ protected function getAutoRouteForDocument($document)
183195
$autoRoutes = $this->fetchAutoRoutesForDocument($document);
184196
}
185197

186-
// @TODO: get locale from ODM
187198
$locale = null;
188199

189200
if ($locale) {
@@ -241,6 +252,6 @@ protected function isDocumentPersisted($document)
241252
$metadata = $this->dm->getClassMetadata(get_class($document));
242253
$id = $metadata->getIdentifierValue($document);
243254

244-
return $this->dm->getPhpcrSession()->nodeExists($id);
255+
return $this->phpcrSession->nodeExists($id);
245256
}
246257
}

DependencyInjection/Configuration.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ public function getConfigTreeBuilder()
1717
$treeBuilder = new TreeBuilder();
1818
$treeBuilder->root('symfony_cmf_routing_auto_route')
1919
->children()
20-
->scalarNode('basepath')
20+
->scalarNode('base_path')
2121
->defaultValue('/cms/auto-routes')
2222
->end()
2323
->arrayNode('auto_route_by_class')
2424
->useAttributeAsKey('class')
2525
->prototype('array')
2626
->children()
2727
->scalarNode('base_path')->end()
28+
->booleanNode('base_path_auto_create')->defaultValue(true)->end()
2829
->scalarNode('route_method_name')->end()
2930
->end()
3031
->end();

DependencyInjection/SymfonyCmfRoutingAutoRouteExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function load(array $configs, ContainerBuilder $container)
2525
$config = $processor->processConfiguration($configuration, $configs);
2626

2727
$keys = array(
28-
'basepath',
28+
'base_path',
2929
'auto_route_by_class'
3030
);
3131

Document/AutoRoute.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Symfony\Cmf\Bundle\RoutingAutoRouteBundle\Document;
44

5-
use Doctrine\Common\Collections\Collection;
65
use Symfony\Cmf\Bundle\RoutingExtraBundle\Document\Route;
76

87
/**

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
This bundle automatically creates and manages routes for given persisted document classes.
44

5-
User stories:
5+
Use cases:
66

77
* Blog posts: To be able to view a blog posts, blog posts must have a route which
88
is appended to that of its parent, the blog.

Resources/config/auto_route.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
id="symfony_cmf_routing_auto_route.slugifier"
1616
class="Symfony\Cmf\Bundle\CoreBundle\Slugifier\CallbackSlugifier"
1717
>
18-
<argument>URLify::filter</argument>
18+
<argument>Ferrandini\Urlizer::urlize</argument>
1919
</service>
2020

2121
<service
@@ -27,6 +27,11 @@
2727
<argument type="service" id="symfony_cmf_routing_auto_route.slugifier"/>
2828
<argument>%symfony_cmf_routing_auto_route.basepath%</argument>
2929
</service>
30+
31+
<service id="symfony_cmf_routing_auto_route.phpcrodm_auto_route_subscriber" class="%symfony_cmf_routing_auto_route.phpcrodm_auto_route_subscriber_class%">
32+
<argument type="service" id="service_container"/>
33+
<tag name="doctrine_phpcr.event_subscriber"/>
34+
</service>
3035
</services>
3136
</container>
3237

Tests/Functional/BaseTestCase.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@
88

99
class BaseTestCase extends WebTestCase
1010
{
11-
/**
12-
* @var \Doctrine\ODM\PHPCR\DocumentManager
13-
*/
14-
protected $dm;
15-
1611
static protected function createKernel(array $options = array())
1712
{
1813
return new AppKernel(
@@ -25,25 +20,24 @@ public function getContainer()
2520
return self::$kernel->getContainer();
2621
}
2722

23+
public function getDm()
24+
{
25+
return $this->getContainer()->get('doctrine_phpcr.odm.document_manager');
26+
}
27+
2828
public function setUp(array $options = array(), $routebase = null)
2929
{
3030
self::$kernel = self::createKernel($options);
3131
self::$kernel->init();
3232
self::$kernel->boot();
3333

34-
$this->dm = $this->getContainer()->get('doctrine_phpcr.odm.document_manager');
35-
36-
if (null == $routebase) {
37-
return;
38-
}
39-
4034
$session = $this->getContainer()->get('doctrine_phpcr.session');
4135

42-
if ($session->nodeExists("/test/$routebase")) {
43-
$session->getNode("/test/$routebase")->remove();
36+
if ($session->nodeExists('/test')) {
37+
$session->getNode('/test')->remove();
4438
}
4539

46-
if (! $session->nodeExists('/test')) {
40+
if (!$session->nodeExists('/test')) {
4741
$session->getRootNode()->addNode('test', 'nt:unstructured');
4842
}
4943

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\RoutingAutoRouteBundle\Tests\Functional\Subscriber;
4+
5+
use Symfony\Cmf\Bundle\RoutingAutoRouteBundle\Tests\Functional\app\Document\Post;
6+
use Symfony\Cmf\Bundle\RoutingAutoRouteBundle\Tests\Functional\BaseTestCase;
7+
8+
class AutoRouteSubscriberTest extends BaseTestCase
9+
{
10+
public function setUp()
11+
{
12+
parent::setUp();
13+
$this->manager = $this->getContainer()->get('symfony_cmf_routing_auto_route.auto_route_manager');
14+
}
15+
16+
protected function createPost()
17+
{
18+
$post = new Post;
19+
$post->path = '/test/test-post';
20+
$post->title = 'Unit testing blog post';
21+
22+
$this->getDm()->persist($post);
23+
$this->getDm()->flush();
24+
$this->getDm()->clear();
25+
}
26+
27+
public function testPersist()
28+
{
29+
$this->createPost();
30+
31+
$route = $this->getDm()->find(null, '/test/auto-route/posts/unit-testing-blog-post');
32+
33+
$this->assertNotNull($route);
34+
35+
// make sure auto-route has been persisted
36+
$post = $this->getDm()->find(null, '/test/test-post');
37+
$routes = $this->getDm()->getReferrers($post);
38+
39+
$this->assertCount(1, $routes);
40+
$this->assertInstanceOf('Symfony\Cmf\Bundle\RoutingAutoRouteBundle\Document\AutoRoute', $routes[0]);
41+
$this->assertEquals('unit-testing-blog-post', $routes[0]->getName());
42+
}
43+
44+
public function testUpdate()
45+
{
46+
$this->createPost();
47+
48+
$post = $this->getDm()->find(null, '/test/test-post');
49+
// test update
50+
$post->title = 'Foobar';
51+
$this->getDm()->persist($post);
52+
$this->getDm()->flush();
53+
54+
// make sure auto-route has been persisted
55+
$post = $this->getDm()->find(null, '/test/test-post');
56+
$routes = $post->routes;
57+
58+
$this->assertCount(1, $routes);
59+
$this->assertInstanceOf('Symfony\Cmf\Bundle\RoutingAutoRouteBundle\Document\AutoRoute', $routes[0]);
60+
61+
$this->getDm()->refresh($routes[0]);
62+
63+
$this->assertEquals('foobar', $routes[0]->getName());
64+
$this->assertEquals('/test/auto-route/posts/foobar', $routes[0]->getId());
65+
66+
}
67+
68+
public function testRemove()
69+
{
70+
$this->createPost();
71+
$post = $this->getDm()->find(null, '/test/test-post');
72+
73+
// test removing
74+
$this->getDm()->remove($post);
75+
76+
$this->getDm()->flush();
77+
78+
$baseRoute = $this->getDm()->find(null, '/test/auto-route/posts');
79+
$routes = $this->getDm()->getChildren($baseRoute);
80+
$this->assertCount(0, $routes);
81+
}
82+
}
83+

Tests/Functional/app/AppKernel.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function registerBundles()
3434
new \Symfony\Bundle\MonologBundle\MonologBundle(),
3535
new \Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
3636
new \Doctrine\Bundle\PHPCRBundle\DoctrinePHPCRBundle(),
37+
new \Symfony\Cmf\Bundle\RoutingExtraBundle\SymfonyCmfRoutingExtraBundle(),
3738
new \Symfony\Cmf\Bundle\RoutingAutoRouteBundle\SymfonyCmfRoutingAutoRouteBundle(),
3839
);
3940
}

Tests/Functional/app/Document/Post.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Symfony\Cmf\Bundle\RoutingExtraBundle\Tests\Functional\Testdoc;
3+
namespace Symfony\Cmf\Bundle\RoutingAutoRouteBundle\Tests\Functional\app\Document;
44

55
use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCR;
66
use Symfony\Cmf\Bundle\RoutingExtraBundle\Mapping\Annotations as CMFRouting;
@@ -18,7 +18,10 @@ class Post
1818
public $path;
1919

2020
/**
21-
* @PHPCR\Referrers(filter="routeContent")
21+
* @PHPCR\Referrers(
22+
* referringDocument="Symfony\Cmf\Bundle\RoutingExtraBundle\Document\Route",
23+
* referencedBy="routeContent"
24+
* )
2225
*/
2326
public $routes;
2427

0 commit comments

Comments
 (0)