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

Commit dfee57b

Browse files
committed
Added AutoRouteMaker logic
1 parent a9b8af9 commit dfee57b

File tree

2 files changed

+136
-23
lines changed

2 files changed

+136
-23
lines changed

AutoRoute/AutoRouteMaker.php

Lines changed: 66 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,83 @@
22

33
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute;
44

5-
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteStack\Builder;
6-
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteStack\BuilderUnitInterface;
5+
use Doctrine\ODM\PHPCR\DocumentManager;
6+
use Symfony\Cmf\Bundle\RoutingAutoBundle\Document\AutoRoute;
77

88
/**
99
* @author Daniel Leech <[email protected]>
1010
*/
1111
class AutoRouteMaker
1212
{
13-
protected $builder;
14-
protected $builderUnit;
13+
protected $dm;
1514

16-
public function __construct(Builder $builder, BuilderUnitInterface $builderUnit)
15+
public function __construct(DocumentManager $dm)
1716
{
18-
$this->builder = $builder;
19-
$this->builderUnit = $builderUnit;
17+
$this->dm = $dm;
2018
}
2119

2220
public function createOrUpdateAutoRoute(AutoRouteStack $autoRouteStack)
2321
{
24-
$this->builder->build($autoRouteStack, $this->builderUnit);
22+
$content = $autoRouteStack->getContext()->getContent();
23+
24+
$autoRoute = $this->getAutoRouteForDocument($content);
25+
26+
if (null === $autoRoute) {
27+
$autoRoute = new AutoRoute;
28+
$autoRoute->setRouteContent($content);
29+
}
30+
31+
$autoRouteStack->addRoute($autoRoute);
32+
}
33+
34+
protected function getAutoRouteForDocument($document)
35+
{
36+
$dm = $this->dm;
37+
$uow = $dm->getUnitOfWork();
38+
39+
$referrers = $this->dm->getReferrers($document);
40+
41+
if ($referrers->count() == 0) {
42+
return null;
43+
}
44+
45+
// Filter non auto-routes
46+
$referrers = $referrers->filter(function ($referrer) {
47+
if ($referrer instanceof AutoRoute) {
48+
return true;
49+
}
50+
51+
return false;
52+
});
53+
54+
$metadata = $dm->getClassMetadata(get_class($document));
55+
56+
$locale = null; // $uow->getLocale($document, $locale);
57+
58+
// If the document is translated, filter locales
59+
if (null !== $locale) {
60+
throw new \Exception(
61+
'Translations not yet supported for Auto Routes - '.
62+
'Should be easy.'
63+
);
64+
65+
// array_filter($referrers, function ($referrer) use ($dm, $uow, $locale) {
66+
// $metadata = $dm->getClassMetadata($refferer);
67+
// if ($locale == $uow->getLocaleFor($referrer, $referrer)) {
68+
// return true;
69+
// }
70+
71+
// return false;
72+
// });
73+
}
74+
75+
if ($referrers->count() > 1) {
76+
throw new \RuntimeException(sprintf(
77+
'More than one auto route for document "%s"',
78+
get_class($document)
79+
));
80+
}
81+
82+
return $referrers->first();
2583
}
2684
}

Tests/AutoRoute/AutoRouteMakerTest.php

Lines changed: 70 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,94 @@
33
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\AutoRoute;
44

55
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\AutoRouteMaker;
6+
use Doctrine\Common\Collections\ArrayCollection;
67

78
class AutoRouteMakerTest extends \PHPUnit_Framework_TestCase
89
{
910
public function setUp()
1011
{
11-
$this->builder = $this->getMockBuilder(
12-
'Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteStack\Builder'
12+
$this->dm = $this->getMockBuilder(
13+
'Doctrine\ODM\PHPCR\DocumentManager'
1314
)->disableOriginalConstructor()->getMock();
1415

15-
$this->builderUnit = $this->getMock(
16-
'Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteStack\BuilderUnitInterface'
16+
$this->uow = $this->getMockBuilder(
17+
'Doctrine\ODM\PHPCR\UnitOfWork'
18+
)->disableOriginalConstructor()->getMock();
19+
20+
$this->metadata = $this->getMockBuilder(
21+
'Doctrine\ODM\PHPCR\Mapping\ClassMetadata'
22+
)->disableOriginalConstructor()->getMock();
23+
24+
$this->arm = new AutoRouteMaker($this->dm);
25+
$this->doc = new \stdClass;
26+
27+
$this->autoRoute1 = $this->getMock(
28+
'Symfony\Cmf\Bundle\RoutingAutoBundle\Document\AutoRoute'
1729
);
30+
$this->autoRoute2 = $this->getMock(
31+
'Symfony\Cmf\Bundle\RoutingAutoBundle\Document\AutoRoute'
32+
);
33+
$this->nonAutoRoute = new \stdClass;
1834

19-
$this->autoRouteStack = $this->getMockBuilder(
35+
$this->autoRouteStack = $this->getMockBuilder(
2036
'Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\AutoRouteStack'
2137
)->disableOriginalConstructor()->getMock();
2238

23-
$this->autoRouteMaker = new AutoRouteMaker($this->builder, $this->builderUnit);
39+
$this->builderContext = $this->getMock(
40+
'Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\BuilderContext'
41+
);
2442
}
2543

26-
public function testCreateOrUpdateAutoRoute()
44+
public function testCreateOrUpdateAutoRouteForExisting()
45+
{
46+
$this->dm->expects($this->once())
47+
->method('getReferrers')
48+
->will($this->returnValue(new ArrayCollection(array(
49+
$this->autoRoute1,
50+
$this->nonAutoRoute
51+
))));
52+
53+
$this->autoRouteStack->expects($this->once())
54+
->method('getContext')
55+
->will($this->returnValue($this->builderContext));
56+
57+
$this->builderContext->expects($this->once())
58+
->method('getContent')
59+
->will($this->returnValue($this->doc));
60+
61+
$this->dm->expects($this->once())
62+
->method('getUnitOfWork')
63+
->will($this->returnValue($this->uow));
64+
65+
$this->autoRouteStack->expects($this->once())
66+
->method('addRoute')
67+
->with($this->autoRoute1);
68+
69+
$this->arm->createOrUpdateAutoRoute($this->autoRouteStack);
70+
}
71+
72+
public function testCreateOrUpdateAutoRouteForNew()
2773
{
2874
$testCase = $this;
75+
$this->dm->expects($this->once())
76+
->method('getReferrers')
77+
->will($this->returnValue(new ArrayCollection(array(
78+
))));
79+
80+
$this->autoRouteStack->expects($this->once())
81+
->method('getContext')
82+
->will($this->returnValue($this->builderContext));
83+
84+
$this->builderContext->expects($this->once())
85+
->method('getContent')
86+
->will($this->returnValue($this->doc));
2987

30-
$this->builder->expects($this->once())
31-
->method('build')
32-
->will($this->returnCallback(function ($stack, $builderUnit) use ($testCase) {
33-
$testCase->assertInstanceOf(
34-
'Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteStack',
35-
$stack
36-
);
88+
$this->autoRouteStack->expects($this->once())
89+
->method('addRoute')
90+
->will($this->returnCallback(function ($route) use ($testCase) {
91+
$testCase->assertInstanceOf('Symfony\Cmf\Bundle\RoutingAutoBundle\Document\AutoRoute', $route);
3792
}));
3893

39-
$stack = $this->autoRouteMaker->createOrUpdateAutoRoute($this->autoRouteStack);
94+
$this->arm->createOrUpdateAutoRoute($this->autoRouteStack);
4095
}
4196
}

0 commit comments

Comments
 (0)