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

Commit a9b8af9

Browse files
committed
All AutoRoute tests pass
1 parent e076326 commit a9b8af9

37 files changed

+450
-454
lines changed

AutoRoute/AutoRouteMaker.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute;
44

55
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteStack\Builder;
6-
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteStack\BuilderUnit;
6+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteStack\BuilderUnitInterface;
77

88
/**
99
* @author Daniel Leech <[email protected]>
@@ -13,15 +13,14 @@ class AutoRouteMaker
1313
protected $builder;
1414
protected $builderUnit;
1515

16-
public function __construct(Builder $builder, BuilderUnit $builderUnit)
16+
public function __construct(Builder $builder, BuilderUnitInterface $builderUnit)
1717
{
1818
$this->builder = $builder;
19-
$this->bulderUnit = $builderUnit;
19+
$this->builderUnit = $builderUnit;
2020
}
2121

22-
public function createOrUpdateAutoRoute(BuilderContext $context)
22+
public function createOrUpdateAutoRoute(AutoRouteStack $autoRouteStack)
2323
{
24-
$stack = new AutoRouteStack($context);
25-
$this->builder->build($stack, $this->builderUnit);
24+
$this->builder->build($autoRouteStack, $this->builderUnit);
2625
}
2726
}

AutoRoute/AutoRouteManager.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute;
44

55
use Doctrine\ODM\PHPCR\DocumentManager;
6-
use Metadata\MetadataFactoryInterface;
7-
use Symfony\Cmf\Bundle\RoutingAutoBundle\Document\AutoRoute;
8-
use Symfony\Cmf\Bundle\CoreBundle\Slugifier\SlugifierInterface;
9-
use PHPCR\Util\NodeHelper;
6+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\AutoRouteStack;
7+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteStack\Builder;
108
use Doctrine\Common\Util\ClassUtils;
119

1210
/**
@@ -18,9 +16,10 @@ class AutoRouteManager
1816
{
1917
protected $factory;
2018

21-
public function __construct(Factory $factory)
19+
public function __construct(Factory $factory, Builder $builder)
2220
{
2321
$this->factory = $factory;
22+
$this->builder = $builder;
2423
}
2524

2625
/**
@@ -36,6 +35,7 @@ public function __construct(Factory $factory)
3635
public function updateAutoRouteForDocument($document)
3736
{
3837
$classFqn = ClassUtils::getClass($document);
38+
3939
$context = new BuilderContext;
4040
$context->setContent($document);
4141

@@ -44,8 +44,9 @@ public function updateAutoRouteForDocument($document)
4444
$builderUnitChain->executeChain($context);
4545

4646
// persist the auto route
47-
$arm = $this->factory->getAutoRouteMaker($classFqn);
48-
$arm->createOrUpdateAutoRoute($context);
47+
$autoRouteStack = new AutoRouteStack($context);
48+
$builderUnit = $this->factory->getContentNameBuilderUnit($classFqn);
49+
$this->builder->build($autoRouteStack, $builderUnit);
4950

5051
return $context;
5152
}

AutoRoute/BuilderContext.php

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ class BuilderContext
1313
protected $stagedRouteStack;
1414
protected $content;
1515

16-
public function getRouteNodes()
16+
public function getRoutes()
1717
{
1818
$routes = array();
1919
foreach ($this->routeStacks as $routeStack) {
20-
$routes = array_merge($routes, $routeStack->getRouteNodes());
20+
$routes = array_merge($routes, $routeStack->getRoutes());
2121
}
22+
23+
return $routes;
2224
}
2325

2426
public function stageRouteStack(RouteStack $routeStack)
@@ -50,23 +52,6 @@ public function getRouteStacks()
5052
return $this->routeStacks;
5153
}
5254

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-
61-
foreach ($this->routeStacks as $routeStack) {
62-
$path[] = $routeStack->getPath();
63-
}
64-
65-
$path[] = $this->stagedRouteStack->getPath();
66-
67-
return '/'.implode('/', $path);
68-
}
69-
7055
public function setContent($content)
7156
{
7257
$this->content = $content;

AutoRoute/Factory.php

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,16 @@ public function getRouteStackBuilderUnitChain($classFqn)
5757
return $this->routeStackChains[$classFqn];
5858
}
5959

60-
public function getAutoRouteMaker($classFqn)
60+
public function getContentNameBuilderUnit($classFqn)
6161
{
62-
if (!isset($this->autoRouteMakers[$classFqn])) {
63-
$this->autoRouteMakers[$classFqn] = $this->generateAutoRouteMaker($classFqn);
62+
if (!isset($this->contentNameBuilderUnits[$classFqn])) {
63+
$mapping = $this->getMapping($classFqn);
64+
$this->contentNameBuilderUnits[$classFqn] = $this->generateBuilderUnit(
65+
$mapping['content_name']
66+
);
6467
}
6568

66-
return $this->autoRouteMakers[$classFqn];
69+
return $this->contentNameBuilderUnits[$classFqn];
6770
}
6871

6972
public function hasMapping($classFqn)
@@ -86,16 +89,6 @@ protected function generateRouteStackChain($classFqn)
8689
return $routeStackChain;
8790
}
8891

89-
protected function generateAutoRouteMaker($classFqn)
90-
{
91-
$mapping = $this->getMapping($classFqn);
92-
$unit = $this->generateBuilderUnit($mapping['content_name']);
93-
94-
$arm = new AutoRouteMaker($this->builder);
95-
96-
return $arm;
97-
}
98-
9992
protected function generateBuilderUnit($config)
10093
{
10194
$pathProvider = $this->getBuilderService($config, 'provider', 'name');

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(RouteStack $stack, BuilderContext $builderContext);
17+
public function execute(RouteStack $stack);
1818
}

AutoRoute/PathExists/AutoIncrementPath.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\BuilderContext;
77
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteStack;
88
use Doctrine\ODM\PHPCR\DocumentManager;
9-
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteMakerInterface;
9+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteMaker;
1010

1111
/**
1212
* @author Daniel Leech <[email protected]>
@@ -16,7 +16,7 @@ class AutoIncrementPath implements PathActionInterface
1616
protected $dm;
1717
protected $routeMaker;
1818

19-
public function __construct(DocumentManager $dm, RouteMakerInterface $routeMaker)
19+
public function __construct(DocumentManager $dm, RouteMaker $routeMaker)
2020
{
2121
$this->dm = $dm;
2222
$this->routeMaker = $routeMaker;
@@ -26,17 +26,17 @@ public function init(array $options)
2626
{
2727
}
2828

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

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

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

39-
$stack->replaceLastPathElement($newPath);
39+
$routeStack->replaceLastPathElement(basename($newPath));
4040

4141
$this->routeMaker->makeRoutes($routeStack);
4242
}

AutoRoute/PathExists/UsePath.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ public function init(array $options)
2323
{
2424
}
2525

26-
public function execute(RouteStack $routeStack, BuilderContext $context)
26+
public function execute(RouteStack $routeStack)
2727
{
28-
$path = $context->getPath();
28+
$path = $routeStack->getFullPath();
2929
$route = $this->dm->find(null, $path);
3030

3131
if (!$route) {
@@ -36,6 +36,6 @@ public function execute(RouteStack $routeStack, BuilderContext $context)
3636
));
3737
}
3838

39-
$stack->addRoute($route);
39+
$routeStack->addRoute($route);
4040
}
4141
}

AutoRoute/PathNotExists/CreatePath.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,27 @@
33
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\PathNotExists;
44

55
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\PathActionInterface;
6-
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\BuilderContext;
7-
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RoutePatcherInterface;
86
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteStack;
7+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteMaker;
98

109
/**
1110
* @author Daniel Leech <[email protected]>
1211
*/
1312
class CreatePath implements PathActionInterface
1413
{
15-
protected $routePatcher;
14+
protected $routeMaker;
1615

17-
public function __construct(RoutePatcherInterface $routePatcher)
16+
public function __construct(RouteMaker $routeMaker)
1817
{
19-
$this->routePatcher = $routePatcher;
18+
$this->routeMaker = $routeMaker;
2019
}
2120

2221
public function init(array $options)
2322
{
2423
}
2524

26-
public function execute(RouteStack $routeStack, BuilderContext $context)
25+
public function execute(RouteStack $routeStack)
2726
{
28-
$this->routePatcher->makeRoutes($routeStack, $context);
27+
$this->routeMaker->makeRoutes($routeStack);
2928
}
3029
}

AutoRoute/PathNotExists/ThrowException.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\PathNotExists;
44

55
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\PathActionInterface;
6-
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\BuilderContext;
76
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\Exception\CouldNotFindRouteException;
7+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteStack;
88

99
/**
1010
* @author Daniel Leech <[email protected]>
@@ -21,9 +21,9 @@ public function init(array $options)
2121
{
2222
}
2323

24-
public function execute(BuilderContext $context)
24+
public function execute(RouteStack $routeStack)
2525
{
26-
throw new CouldNotFindRouteException($context->getPath());
26+
throw new CouldNotFindRouteException($routeStack->getFullPath());
2727
}
2828
}
2929

AutoRoute/PathProvider/FromObjectMethodProvider.php

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

55
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\PathProviderInterface;
66
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\Exception\MissingOptionException;
7-
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\BuilderContext;
7+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteStack;
88

99
/**
1010
* @author Daniel Leech <[email protected]>
@@ -22,20 +22,29 @@ public function init(array $options)
2222
$this->method = $options['method'];
2323
}
2424

25-
public function providePath(BuilderContext $context)
25+
public function providePath(RouteStack $routeStack)
2626
{
27-
$object = $context->getObject();
27+
$object = $routeStack->getContext()->getContent();
2828
$method = $this->method;
2929

3030
if (!method_exists($object, $method)) {
3131
throw new \BadMethodCallException(sprintf('Method "%s" does not exist on class "%s"', $method, get_class($object)));
3232
}
3333

34-
$path = $object->$method();
34+
$pathElements = $object->$method();
35+
36+
if (!is_array($pathElements)) {
37+
throw new \RuntimeException(sprintf(
38+
'FromObjectMethodProvider wants %s:%s to return an array of route names.. got "%s"',
39+
get_class($object),
40+
$method,
41+
gettype($pathElements)
42+
));
43+
}
3544

3645
// @todo: Validate the validator service.
3746

38-
$context->addPath($path);
47+
$routeStack->addPathElements($pathElements);
3948
}
4049
}
4150

0 commit comments

Comments
 (0)