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

Commit a6b6ff0

Browse files
committed
Started some refactoring
1 parent bd16bad commit a6b6ff0

File tree

12 files changed

+287
-210
lines changed

12 files changed

+287
-210
lines changed

AutoRoute/Builder.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,12 @@ public function __construct(PhpcrSession $phpcrSession)
2121

2222
public function build(BuilderUnitInterface $builderUnit, BuilderContext $context)
2323
{
24+
$routeStack = new RouteStack;
2425
$builderUnit->pathAction($context);
2526

2627
$exists = $this->phpcrSession->nodeExists($context->getPath());
2728

2829
if ($exists) {
29-
// todo: check to see if the existing node references
30-
// this content... i.e. allow updates to pass.
31-
3230
$builderUnit->existsAction($context);
3331
} else {
3432
$builderUnit->notExistsAction($context);

AutoRoute/BuilderContext.php

Lines changed: 14 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,63 +5,31 @@
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
811
* @author Daniel Leech <[email protected]>
912
*/
1013
class BuilderContext
1114
{
12-
protected $pathStack = array();
13-
protected $routeStack = array();
15+
protected $routeStacks = array();
16+
protected $object;
1417

15-
protected $isLastBuilder = false;
16-
17-
public function addPath($part)
18-
{
19-
$this->pathStack[] = $part;
20-
}
21-
22-
public function getLastPath()
23-
{
24-
return end($this->pathStack);
25-
}
26-
27-
public function replaceLastPath($path)
28-
{
29-
array_pop($this->pathStack);
30-
$this->pathStack[] = $path;
31-
}
32-
33-
public function getPathStack()
18+
public function addRouteStack($routeStack)
3419
{
35-
return $this->pathStack;
36-
}
37-
38-
public function addRoute($route)
39-
{
40-
$this->routeStack[]= $route;
41-
}
42-
43-
public function getRouteStack()
44-
{
45-
return $this->routeStack;
46-
}
47-
48-
public function getLastRoute()
49-
{
50-
return end($this->routeStack);
51-
}
20+
if (!$routeStack->isClosed()) {
21+
throw new \RuntimeException('Cannot add closed route stack to context');
22+
}
5223

53-
public function getPath()
54-
{
55-
return implode('/', $this->pathStack);
24+
$this->routeStacks[] = $routeStack;
5625
}
5726

58-
public function isLastBuilder($isLastBuilder = null)
27+
public function getRouteNodes()
5928
{
60-
if (null === $isLastBuilder) {
61-
return $this->isLastBuilder;
29+
$routes = array();
30+
foreach ($this->routeStacks as $routeStack) {
31+
$routes = array_merge($routes, $routeStack->getRouteNodes());
6232
}
63-
64-
$this->isLastBuilder = $isLastBuilder;
6533
}
6634

6735
public function setObject($object)

AutoRoute/BuilderUnitChain.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,7 @@ public function executeChain(BuilderContext $context)
2727
$i = 1;
2828

2929
foreach ($this->builderUnitChain as $name => $builderUnit) {
30-
31-
if ($i++ == count($this->builderUnitChain)) {
32-
$context->isLastBuilder(true);
33-
} else {
34-
// This may seem redundant, but it helps the unit test...
35-
$context->isLastBuilder(false);
36-
}
37-
30+
$routeStack = new RouteStack;
3831
$this->builder->build($builderUnit, $context);
3932
}
4033
}

AutoRoute/RouteMaker/GenericMaker.php

Lines changed: 0 additions & 77 deletions
This file was deleted.

AutoRoute/RouteMakerInterface.php

Lines changed: 0 additions & 16 deletions
This file was deleted.
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\RoutePatcher;
4+
5+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RoutePatcherInterface;
6+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\BuilderContext;
7+
use Doctrine\ODM\PHPCR\DocumentManager;
8+
use Doctrine\ODM\PHPCR\Document\Generic;
9+
use Symfony\Cmf\Bundle\RoutingAutoBundle\Document\AutoRoute;
10+
11+
/**
12+
* This class will make the Route classes using
13+
* Generic documents using.
14+
*
15+
* @todo: Make this use PHPCR\Util\NodeHelper:makePath
16+
*
17+
* @author Daniel Leech <[email protected]>
18+
*/
19+
class GenericPatcher implements RoutePatcherInterface
20+
{
21+
public function __construct(DocumentManager $dm)
22+
{
23+
$this->dm = $dm;
24+
}
25+
26+
public function makeRoutes(BuilderUnitContext $buc)
27+
{
28+
$components = $buc->getPathComponents();
29+
30+
foreach ($components as $i => $component) {
31+
32+
$path .= '/'.$component;
33+
34+
$doc = $this->dm->find(null, $path);
35+
36+
if (null === $doc) {
37+
$parent = $context->getLastRoute();
38+
39+
if (null === $parent) {
40+
$parent = $this->dm->find(null, '/');
41+
}
42+
43+
// otherwise create a generic document
44+
$doc = new Generic;
45+
$doc->setNodename($component);
46+
$doc->setParent($parent);
47+
var_dump($parent);
48+
}
49+
}
50+
51+
$context->addRoute($doc);
52+
}
53+
}
54+
}

AutoRoute/RoutePatcherInterface.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute;
4+
5+
/**
6+
* Classes implementing this interface "patch"
7+
* any missing comonents in/a/routes/path
8+
*
9+
* @author Daniel Leech <[email protected]>
10+
*/
11+
interface RoutePatcherInterface
12+
{
13+
public function patch(BuilderContext $context);
14+
}

AutoRoute/RouteStack.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute;
4+
5+
/**
6+
* @author Daniel Leech <[email protected]>
7+
*/
8+
class RouteStack
9+
{
10+
protected $pathElements;
11+
protected $routeNodes = array();
12+
13+
protected $closed = false;
14+
15+
public function addPathElements(array $pathElements)
16+
{
17+
foreach ($pathElements as $pathElement) {
18+
$this->addPathElement($pathElement);
19+
}
20+
}
21+
22+
public function addPathElement($pathElement)
23+
{
24+
if (true === $this->closed) {
25+
throw new \RuntimeException('Cannot add path elements to a closed route stack.');
26+
}
27+
28+
$this->pathElements[] = $pathElement;
29+
}
30+
31+
public function getPathElements()
32+
{
33+
return $this->pathElements;
34+
}
35+
36+
public function getPaths()
37+
{
38+
$tmp = array();
39+
40+
foreach ($this->pathElements as $pathElement) {
41+
$tmp[] = $pathElement;
42+
$paths[] = '/'.implode('/', $tmp);
43+
}
44+
45+
return $paths;
46+
}
47+
48+
public function addRouteNode($routeNode)
49+
{
50+
if (true === $this->closed) {
51+
throw new \RuntimeException('Cannot add path elements to a closed route stack.');
52+
}
53+
54+
$this->routeNodes[] = $routeNode;
55+
}
56+
57+
public function close()
58+
{
59+
if (count($this->routeNodes) != count($this->pathElements)) {
60+
throw new \RuntimeException(sprintf(
61+
'Attempting to close route stack but the number of path elements (%d) '.
62+
'does not match number of route elements (%d). Registered path elements: "%s"',
63+
count($this->pathElements),
64+
count($this->routeNodes),
65+
implode(',', $this->pathElements)
66+
));
67+
}
68+
69+
$this->closed = true;
70+
}
71+
72+
public function getRouteNodes()
73+
{
74+
if (false === $this->closed) {
75+
throw new \RuntimeException(
76+
'You must close the route stack before retrieving the route nodes.'
77+
);
78+
}
79+
80+
return $this->routeNodes;
81+
}
82+
83+
public function isClosed()
84+
{
85+
return $this->closed;
86+
}
87+
}

0 commit comments

Comments
 (0)