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

Commit 634fac2

Browse files
committed
Lots of refactoring.
1 parent 37a68a4 commit 634fac2

22 files changed

+399
-272
lines changed

AutoRoute/AutoRouteManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function __construct(BuilderUnitChainFactory $bucf)
3636
public function updateAutoRouteForDocument($document)
3737
{
3838
$context = new BuilderContext;
39-
$context->setObject($document);
39+
$context->setContent($document);
4040

4141
$builderUnitChain = $this->bucf->getChain(ClassUtils::getClass($document));
4242
$builderUnitChain->executeChain($context);

AutoRoute/BuilderContext.php

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,75 @@
55
use Symfony\Cmf\Bundle\RoutingExtraBundle\Document\Route;
66

77
/**
8-
* @todo: Should this be renamed to RouteStack?
9-
*
10-
* A route stack would have all of route components l
118
* @author Daniel Leech <[email protected]>
129
*/
1310
class BuilderContext
1411
{
1512
protected $routeStacks = array();
16-
protected $object;
13+
protected $stagedRouteStack;
14+
protected $content;
1715

18-
public function addRouteStack($routeStack)
16+
public function getRouteNodes()
1917
{
20-
if (!$routeStack->isClosed()) {
21-
throw new \RuntimeException('Cannot add closed route stack to context');
18+
$routes = array();
19+
foreach ($this->routeStacks as $routeStack) {
20+
$routes = array_merge($routes, $routeStack->getRouteNodes());
2221
}
22+
}
2323

24-
$this->routeStacks[] = $routeStack;
24+
public function stageRouteStack(RouteStack $routeStack)
25+
{
26+
$this->stagedRouteStack = $routeStack;
2527
}
2628

27-
public function getRouteNodes()
29+
public function commitRouteStack()
2830
{
29-
$routes = array();
31+
if (null === $this->stagedRouteStack) {
32+
throw new \RuntimeException(
33+
'Cannot commit route stack when there is no route stack to commit '.
34+
'(use stageRouteStack to stage)'
35+
);
36+
}
37+
38+
if (false === $this->stagedRouteStack->isClosed()) {
39+
throw new \RuntimeException(
40+
'Staged route stack is not closed, cannot commit.'
41+
);
42+
}
43+
44+
$this->routeStacks[] = $this->stagedRouteStack;
45+
$this->stagedRouteStack = null;
46+
}
47+
48+
public function getRouteStacks()
49+
{
50+
return $this->routeStacks;
51+
}
52+
53+
public function getStagedPath()
54+
{
55+
if (null === $this->stagedRouteStack) {
56+
throw new \RuntimeException('Cannot get staged path when no route stack has been staged (routeStack is null)');
57+
}
58+
59+
$path = array();
60+
3061
foreach ($this->routeStacks as $routeStack) {
31-
$routes = array_merge($routes, $routeStack->getRouteNodes());
62+
$path[] = $routeStack->getPath();
3263
}
64+
65+
$path[] = $this->stagedRouteStack->getPath();
66+
67+
return '/'.implode('/', $path);
3368
}
3469

35-
public function setObject($object)
70+
public function setContent($content)
3671
{
37-
$this->object = $object;
72+
$this->content = $content;
3873
}
3974

40-
public function getObject()
75+
public function getContent()
4176
{
42-
return $this->object;
77+
return $this->content;
4378
}
4479
}

AutoRoute/PathActionInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ interface PathActionInterface
1414
*/
1515
public function init(array $options);
1616

17-
public function execute(BuilderContext $builderContext);
17+
public function execute(RouteStack $stack, BuilderContext $builderContext);
1818
}

AutoRoute/PathExists/AutoIncrementPath.php

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

55
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\PathActionInterface;
66
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\BuilderContext;
7-
use Symfony\Component\Routing\Exception\RouteNotFoundException;
7+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteStack;
88
use Doctrine\ODM\PHPCR\DocumentManager;
99
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteMakerInterface;
1010

@@ -26,17 +26,18 @@ public function init(array $options)
2626
{
2727
}
2828

29-
public function execute(BuilderContext $context)
29+
public function execute(RouteStack $routeStack, BuilderContext $context)
3030
{
3131
$inc = 1;
3232

33-
$path = $context->getLastPath();
33+
$path = $context->getStagedPath();
3434

3535
do {
3636
$newPath = sprintf('%s-%d', $path, $inc++);
3737
} while (null !== $this->dm->find(null, $newPath));
3838

39-
$context->replaceLastPath($newPath);
40-
$this->routeMaker->makeRoutes($context);
39+
$stack->replaceLastPathElement($newPath);
40+
41+
$this->routeMaker->makeRoutes($routeStack);
4142
}
4243
}

AutoRoute/PathExists/UsePath.php

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

55
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\PathActionInterface;
66
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\BuilderContext;
7+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteStack;
78
use Doctrine\ODM\PHPCR\DocumentManager;
89

910
/**
@@ -22,7 +23,7 @@ public function init(array $options)
2223
{
2324
}
2425

25-
public function execute(BuilderContext $context)
26+
public function execute(RouteStack $routeStack, BuilderContext $context)
2627
{
2728
$path = $context->getPath();
2829
$route = $this->dm->find(null, $path);
@@ -35,6 +36,6 @@ public function execute(BuilderContext $context)
3536
));
3637
}
3738

38-
$context->addRoute($route);
39+
$stack->addRoute($route);
3940
}
4041
}

AutoRoute/PathProviderInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ public function init(array $options);
1717
*
1818
* @return string
1919
*/
20-
public function providePath(BuilderContext $builderContext);
20+
public function providePath(RouteStack $routeStack, BuilderContext $builderContext);
2121
}

AutoRoute/RouteStack.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
class RouteStack
99
{
1010
protected $pathElements;
11-
protected $routeNodes = array();
11+
protected $routes = array();
1212

1313
protected $closed = false;
1414

@@ -45,39 +45,44 @@ public function getPaths()
4545
return $paths;
4646
}
4747

48-
public function addRouteNode($routeNode)
48+
public function getPath()
49+
{
50+
return implode('/', $this->pathElements);
51+
}
52+
53+
public function addRoute($route)
4954
{
5055
if (true === $this->closed) {
5156
throw new \RuntimeException('Cannot add path elements to a closed route stack.');
5257
}
5358

54-
$this->routeNodes[] = $routeNode;
59+
$this->routes[] = $route;
5560
}
5661

5762
public function close()
5863
{
59-
if (count($this->routeNodes) != count($this->pathElements)) {
64+
if (count($this->routes) != count($this->pathElements)) {
6065
throw new \RuntimeException(sprintf(
6166
'Attempting to close route stack but the number of path elements (%d) '.
6267
'does not match number of route elements (%d). Registered path elements: "%s"',
6368
count($this->pathElements),
64-
count($this->routeNodes),
69+
count($this->routes),
6570
implode(',', $this->pathElements)
6671
));
6772
}
6873

6974
$this->closed = true;
7075
}
7176

72-
public function getRouteNodes()
77+
public function getRoutes()
7378
{
7479
if (false === $this->closed) {
7580
throw new \RuntimeException(
7681
'You must close the route stack before retrieving the route nodes.'
7782
);
7883
}
7984

80-
return $this->routeNodes;
85+
return $this->routes;
8186
}
8287

8388
public function isClosed()

AutoRoute/RouteStackBuilder.php

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,35 @@
22

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

5+
use PHPCR\SessionInterface as PhpcrSession;
6+
57
/**
8+
* This class is responsible for building and closing
9+
* a RouteStack from a given RouteStackBuilderUnit.
10+
*
611
* @author Daniel Leech <[email protected]>
712
*/
8-
class BuilderUnit implements BuilderUnitInterface
13+
class RouteStackBuilder
914
{
10-
protected $pathProvider;
11-
protected $existsAction;
12-
protected $notExistsAction;
13-
14-
public function __construct(
15-
PathProviderInterface $pathProvider,
16-
PathActionInterface $existsAction,
17-
PathActionInterface $notExistsAction
18-
) {
19-
$this->pathProvider = $pathProvider;
20-
$this->existsAction = $existsAction;
21-
$this->notExistsAction = $notExistsAction;
22-
}
15+
protected $phpcrSession;
2316

24-
public function pathAction(BuilderContext $context)
17+
public function __construct(PhpcrSession $phpcrSession)
2518
{
26-
$this->pathProvider->providePath($context);
19+
$this->phpcrSession = $phpcrSession;
2720
}
2821

29-
public function existsAction(BuilderContext $context)
22+
public function build(RouteStack $routeStack, RouteStackBuilderUnitInterface $rsbu, BuilderContext $context)
3023
{
31-
$this->existsAction->execute($context);
32-
}
24+
$rsbu->pathAction($routeStack, $context);
3325

34-
public function notExistsAction(BuilderContext $context)
35-
{
36-
$this->notExistsAction->execute($context);
26+
$exists = $this->phpcrSession->nodeExists($context->getStagedPath());
27+
28+
if ($exists) {
29+
$rsbu->existsAction($routeStack, $context);
30+
} else {
31+
$rsbu->notExistsAction($routeStack, $context);
32+
}
33+
34+
$routeStack->close();
3735
}
3836
}

AutoRoute/RouteStackBuilderUnit.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute;
4+
5+
use PHPCR\SessionInterface as PhpcrSession;
6+
7+
/**
8+
* Represents a route stack builder /unit/.
9+
*
10+
* The unit is a collection of classes required to
11+
* build and close a RouteStack.
12+
*
13+
* @author Daniel Leech <[email protected]>
14+
*/
15+
class RouteStackBuilderUnit implements RouteStackBuilderUnitInterface
16+
{
17+
protected $pathProvider;
18+
protected $existsAction;
19+
protected $notExistsAction;
20+
21+
public function __construct(
22+
PathProviderInterface $pathProvider,
23+
PathActionInterface $existsAction,
24+
PathActionInterface $notExistsAction
25+
) {
26+
$this->pathProvider = $pathProvider;
27+
$this->existsAction = $existsAction;
28+
$this->notExistsAction = $notExistsAction;
29+
}
30+
31+
/**
32+
* {@inheritDoc}
33+
*/
34+
public function pathAction(RouteStack $routeStack, BuilderContext $context)
35+
{
36+
$this->pathProvider->providePath($routeStack, $context);
37+
}
38+
39+
/**
40+
* {@inheritDoc}
41+
*/
42+
public function existsAction(RouteStack $routeStack, BuilderContext $context)
43+
{
44+
$this->existsAction->execute($routeStack, $context);
45+
}
46+
47+
/**
48+
* {@inheritDoc}
49+
*/
50+
public function notExistsAction(RouteStack $routeStack, BuilderContext $context)
51+
{
52+
$this->notExistsAction->execute($routeStack, $context);
53+
}
54+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute;
4+
5+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\Builder;
6+
7+
/**
8+
* This class is responsible for aggregating route stack
9+
* builder units and executing them sequentially.
10+
*
11+
* @author Daniel Leech <[email protected]>
12+
*/
13+
class RouteStackBuilderUnitChain
14+
{
15+
protected $chain;
16+
protected $builder;
17+
18+
public function __construct(RouteStackBuilder $builder)
19+
{
20+
$this->builder = $builder;
21+
}
22+
23+
public function addRouteStackBuilderUnit($name, RouteStackBuilderUnitInterface $unit)
24+
{
25+
$this->chain[$name] = $unit;
26+
}
27+
28+
public function executeChain(BuilderContext $context)
29+
{
30+
foreach ($this->chain as $name => $builderUnit) {
31+
$routeStack = new RouteStack;
32+
$this->builder->build($routeStack, $builderUnit, $context);
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)