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

Commit 631894b

Browse files
committed
Functional test passes :)
1 parent dfee57b commit 631894b

File tree

16 files changed

+185
-50
lines changed

16 files changed

+185
-50
lines changed

AutoRoute/AutoRouteMaker.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,21 @@ public function createOrUpdateAutoRoute(AutoRouteStack $autoRouteStack)
2525

2626
if (null === $autoRoute) {
2727
$autoRoute = new AutoRoute;
28+
$autoRoute->setParent($autoRouteStack->getContext()->getTopRoute());
2829
$autoRoute->setRouteContent($content);
2930
}
3031

32+
$autoRoute->setName($autoRouteStack->getPath());
33+
3134
$autoRouteStack->addRoute($autoRoute);
3235
}
3336

3437
protected function getAutoRouteForDocument($document)
3538
{
39+
if (!$this->documentIsPersisted($document)) {
40+
return null;
41+
}
42+
3643
$dm = $this->dm;
3744
$uow = $dm->getUnitOfWork();
3845

@@ -79,6 +86,14 @@ protected function getAutoRouteForDocument($document)
7986
));
8087
}
8188

82-
return $referrers->first();
89+
return $referrers->current();
90+
}
91+
92+
protected function documentIsPersisted($document)
93+
{
94+
$metadata = $this->dm->getClassMetadata(get_class($document));
95+
$id = $metadata->getIdentifierValue($document);
96+
$phpcrSession = $this->dm->getPhpcrSession();
97+
return $phpcrSession->nodeExists($id);
8398
}
8499
}

AutoRoute/AutoRouteStack.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function close()
2424
parent::close();
2525
}
2626

27-
public function addRoute(AutoRoute $route)
27+
public function addRoute($route)
2828
{
2929
parent::addRoute($route);
3030
}

AutoRoute/BuilderContext.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,25 @@ public function getRouteStacks()
5252
return $this->routeStacks;
5353
}
5454

55+
public function getTopRoute()
56+
{
57+
$routes = $this->getRoutes();
58+
return end($routes);
59+
}
60+
61+
public function getFullPath()
62+
{
63+
$paths = array();
64+
foreach ($this->routeStacks as $routeStack) {
65+
$paths[] = $routeStack->getPath();
66+
}
67+
68+
$path = implode('/', $paths);
69+
70+
return $path;
71+
72+
}
73+
5574
public function setContent($content)
5675
{
5776
$this->content = $content;

AutoRoute/Factory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,10 @@ private function validateMapping($classFqn, $mapping)
138138
return isset($mapping['content_name']['provider']);
139139
});
140140
$exists('content_name/exists', function ($mapping) {
141-
return isset($mapping['content_name']['exists']);
141+
return isset($mapping['content_name']['exists_action']);
142142
});
143143
$exists('content_name/not_exists', function ($mapping) {
144-
return isset($mapping['content_name']['not_exists']);
144+
return isset($mapping['content_name']['not_exists_action']);
145145
});
146146
}
147147

AutoRoute/PathExists/UsePath.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,20 @@ public function init(array $options)
2525

2626
public function execute(RouteStack $routeStack)
2727
{
28-
$path = $routeStack->getFullPath();
29-
$route = $this->dm->find(null, $path);
30-
31-
if (!$route) {
32-
throw new \RuntimeException(sprintf(
33-
'Expected to find a document at "%s", but didn\'t. This shouldn\'t
34-
happen. Maybe we have a race condition?',
35-
$path
36-
));
37-
}
28+
$paths = $routeStack->getFullPaths();
29+
30+
foreach ($paths as $path) {
31+
$route = $this->dm->find(null, $path);
3832

39-
$routeStack->addRoute($route);
33+
if (!$route) {
34+
throw new \RuntimeException(sprintf(
35+
'Expected to find a document at "%s", but didn\'t. This shouldn\'t
36+
happen. Maybe we have a race condition?',
37+
$path
38+
));
39+
}
40+
41+
$routeStack->addRoute($route);
42+
}
4043
}
4144
}

AutoRoute/PathProvider/FromObjectMethodProvider.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,34 @@
55
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\PathProviderInterface;
66
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\Exception\MissingOptionException;
77
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteStack;
8+
use Symfony\Cmf\Bundle\CoreBundle\Slugifier\SlugifierInterface;
89

910
/**
1011
* @author Daniel Leech <[email protected]>
1112
*/
1213
class FromObjectMethodProvider implements PathProviderInterface
1314
{
1415
protected $method;
16+
protected $slugifier;
17+
protected $slugify;
18+
19+
public function __construct(SlugifierInterface $slugifier)
20+
{
21+
$this->slugifier = $slugifier;
22+
}
1523

1624
public function init(array $options)
1725
{
1826
if (!isset($options['method'])) {
1927
throw new MissingOptionException(__CLASS__, 'method');
2028
}
2129

30+
$options = array_merge(array(
31+
'slugify' => true
32+
), $options);
33+
2234
$this->method = $options['method'];
35+
$this->slugify = $options['slugify'];
2336
}
2437

2538
public function providePath(RouteStack $routeStack)
@@ -33,6 +46,14 @@ public function providePath(RouteStack $routeStack)
3346

3447
$pathElements = $object->$method();
3548

49+
if (is_string($pathElements)) {
50+
if (substr($pathElements, 0, 1) == '/') {
51+
throw new \RuntimeException('Path must not be absolute.');
52+
}
53+
54+
$pathElements = explode('/', $pathElements);
55+
}
56+
3657
if (!is_array($pathElements)) {
3758
throw new \RuntimeException(sprintf(
3859
'FromObjectMethodProvider wants %s:%s to return an array of route names.. got "%s"',
@@ -42,9 +63,14 @@ public function providePath(RouteStack $routeStack)
4263
));
4364
}
4465

45-
// @todo: Validate the validator service.
66+
if (true === $this->slugify) {
67+
$slugifier = $this->slugifier;
68+
array_walk($pathElements, function (&$pathElement) use ($slugifier) {
69+
$pathElement = $slugifier->slugify($pathElement);
70+
});
71+
}
4672

73+
// @todo: Validate the validator service.
4774
$routeStack->addPathElements($pathElements);
4875
}
4976
}
50-

AutoRoute/PathProvider/SpecifiedProvider.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ public function init(array $options)
2525

2626
public function providePath(RouteStack $routeStack)
2727
{
28+
if (substr($this->path, 0, 1) == '/') {
29+
$this->path = substr($this->path, 1);
30+
}
31+
2832
$routeStack->addPathElements(explode('/', $this->path));
2933
}
3034
}

AutoRoute/RouteStack.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class RouteStack
1515

1616
public function __construct(BuilderContext $context)
1717
{
18+
$context->stageRouteStack($this);
1819
$this->context = $context;
1920
}
2021

@@ -27,6 +28,9 @@ public function addPathElements(array $pathElements)
2728

2829
public function addPathElement($pathElement)
2930
{
31+
if (!$pathElement) {
32+
throw new \RuntimeException('Empty path element passed to addPAthElement');
33+
}
3034
if (true === $this->closed) {
3135
throw new \RuntimeException('Cannot add path elements to a closed route stack.');
3236
}
@@ -45,15 +49,15 @@ public function getPaths()
4549

4650
foreach ($this->pathElements as $pathElement) {
4751
$tmp[] = $pathElement;
48-
$paths[] = '/'.implode('/', $tmp);
52+
$paths[] = implode('/', $tmp);
4953
}
5054

5155
return $paths;
5256
}
5357

5458
public function getFullPaths()
5559
{
56-
$parentPath = $this->context->getRouteStackPath($this);
60+
$parentPath = $this->context->getFullPath($this);
5761

5862
$paths = $this->getPaths();
5963

@@ -66,12 +70,22 @@ public function getFullPaths()
6670

6771
public function getFullPath()
6872
{
69-
$parentPath = $this->context->getRouteStackPath($this);
70-
$fullPath = $parentPath.'/'.$this->getPath();
73+
$parentPath = $this->context->getFullPath();
74+
75+
$fullPath = $this->getPath();
76+
77+
if ($parentPath) {
78+
$fullPath = $parentPath.'/'.$fullPath;
79+
}
7180

7281
return $fullPath;
7382
}
7483

84+
public function getAbsolutePath()
85+
{
86+
return '/'.$this->getFullPath();
87+
}
88+
7589
public function getPath()
7690
{
7791
return implode('/', $this->pathElements);

AutoRoute/RouteStack/Builder.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@ public function build(RouteStack $routeStack, BuilderUnitInterface $rsbu)
2525
{
2626
$rsbu->pathAction($routeStack);
2727

28-
$exists = $this->phpcrSession->nodeExists($routeStack->getFullPath());
28+
$exists = $this->phpcrSession->nodeExists('/'.$routeStack->getFullPath());
2929

3030
if ($exists) {
3131
$rsbu->existsAction($routeStack);
3232
} else {
3333
$rsbu->notExistsAction($routeStack);
3434
}
3535

36+
// hmm ...
3637
$routeStack->close();
38+
$routeStack->getContext()->commitRouteStack();
3739
}
3840
}

EventListener/AutoRouteListener.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Doctrine\ODM\PHPCR\Event\OnFlushEventArgs;
66
use Symfony\Component\DependencyInjection\ContainerInterface;
7+
use Symfony\Cmf\Bundle\RoutingAutoBundle\Document\AutoRoute;
78

89
/**
910
* Doctrine PHPCR ODM listener for maintaining automatic routes.
@@ -36,7 +37,7 @@ public function onFlush(OnFlushEventArgs $args)
3637
foreach ($updates as $document) {
3738
if ($this->getArm()->isAutoRouteable($document)) {
3839
$context = $this->getArm()->updateAutoRouteForDocument($document);
39-
foreach ($context->getRouteStack() as $route) {
40+
foreach ($context->getRoutes() as $route) {
4041
$dm->persist($route);
4142
$uow->computeSingleDocumentChangeSet($route);
4243
}
@@ -47,8 +48,15 @@ public function onFlush(OnFlushEventArgs $args)
4748

4849
foreach ($removes as $document) {
4950
if ($this->getArm()->isAutoRouteable($document)) {
50-
$routes = $this->getArm()->fetchAutoRoutesForDocument($document);
51-
foreach ($routes as $route) {
51+
$referrers = $dm->getReferrers($document);
52+
$referrers = $referrers->filter(function ($referrer) {
53+
if ($referrer instanceof AutoRoute) {
54+
return true;
55+
}
56+
57+
return false;
58+
});
59+
foreach ($referrers as $route) {
5260
$uow->scheduleRemove($route);
5361
}
5462
}

0 commit comments

Comments
 (0)