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

Commit e076326

Browse files
committed
Moving towards working functionality (i hope)
1 parent c8fd23d commit e076326

File tree

6 files changed

+126
-29
lines changed

6 files changed

+126
-29
lines changed

AutoRoute/AutoRouteMaker.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute;
4+
5+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteStack\Builder;
6+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteStack\BuilderUnit;
7+
8+
/**
9+
* @author Daniel Leech <[email protected]>
10+
*/
11+
class AutoRouteMaker
12+
{
13+
protected $builder;
14+
protected $builderUnit;
15+
16+
public function __construct(Builder $builder, BuilderUnit $builderUnit)
17+
{
18+
$this->builder = $builder;
19+
$this->bulderUnit = $builderUnit;
20+
}
21+
22+
public function createOrUpdateAutoRoute(BuilderContext $context)
23+
{
24+
$stack = new AutoRouteStack($context);
25+
$this->builder->build($stack, $this->builderUnit);
26+
}
27+
}

AutoRoute/AutoRouteStack.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute;
4+
5+
use Symfony\Cmf\Bundle\RoutingAutoBundle\Document\AutoRoute;
6+
7+
/**
8+
* Special sub class for AutoRoutes.
9+
*
10+
* This enables us to reuse the same strategies for the
11+
* creation of auto routes as for the creation of the content
12+
* path routes.
13+
*
14+
* @author Daniel Leech <[email protected]>
15+
*/
16+
class AutoRouteStack extends RouteStack
17+
{
18+
public function close()
19+
{
20+
if (count($this->routes) > 1) {
21+
throw new \RuntimeException('You can only add one route to the AutoRouteStack.');
22+
}
23+
24+
parent::close();
25+
}
26+
27+
public function addRoute(AutoRoute $route)
28+
{
29+
parent::addRoute($route);
30+
}
31+
}

AutoRoute/Factory.php

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

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

6669
public function hasMapping($classFqn)
@@ -83,6 +86,16 @@ protected function generateRouteStackChain($classFqn)
8386
return $routeStackChain;
8487
}
8588

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+
8699
protected function generateBuilderUnit($config)
87100
{
88101
$pathProvider = $this->getBuilderService($config, 'provider', 'name');
@@ -122,8 +135,21 @@ private function validateMapping($classFqn, $mapping)
122135
}
123136
};
124137

125-
$exists('content_path', function ($mapping) { return isset($mapping['content_path']); });
126-
$exists('content_name', function ($mapping) { return isset($mapping['content_name']); });
138+
$exists('content_path', function ($mapping) {
139+
return isset($mapping['content_path']);
140+
});
141+
$exists('content_name', function ($mapping) {
142+
return isset($mapping['content_name']);
143+
});
144+
$exists('content_name/provider', function ($mapping) {
145+
return isset($mapping['content_name']['provider']);
146+
});
147+
$exists('content_name/exists', function ($mapping) {
148+
return isset($mapping['content_name']['exists']);
149+
});
150+
$exists('content_name/not_exists', function ($mapping) {
151+
return isset($mapping['content_name']['not_exists']);
152+
});
127153
}
128154

129155
private function getBuilderService($builderConfig, $type, $aliasKey)

AutoRoute/RouteMaker.php

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

55
/**
6+
* Class responsible for delegating the creation of
7+
* routes to the appropriate class.
8+
*
69
* @author Daniel Leech <[email protected]>
10+
* @date 13/03/17
711
*/
812
class RouteMaker
913
{
14+
protected $arm;
1015
protected $patcher;
1116

12-
public function __construct(RoutePatcherInterface $patcher)
17+
public function __construct(AutoRouteMaker $arm, RoutePatcherInterface $patcher)
1318
{
19+
$this->arm = $arm;
1420
$this->patcher = $patcher;
1521
}
1622

17-
public function makeRoute(RouteStack $routeStack, BuilderContext $context)
23+
public function makeRoutes(RouteStack $routeStack)
1824
{
19-
foreach ($context->getRouteStacks() as $stack) {
20-
$this->patcher->patchStack($stack, $context);
25+
if ($routeStack instanceOf AutoRouteStack) {
26+
$this->arm->makeAutoRoute($routeStack);
27+
} else {
28+
$this->patcher->patch($routeStack);
2129
}
2230
}
2331
}

AutoRoute/RoutePatcher/GenericPatcher.php

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
* This class will make the Route classes using
1313
* Generic documents using.
1414
*
15-
* @todo: Make this use PHPCR\Util\NodeHelper:makePath
16-
*
1715
* @author Daniel Leech <[email protected]>
1816
*/
1917
class GenericPatcher implements RoutePatcherInterface
@@ -23,32 +21,20 @@ public function __construct(DocumentManager $dm)
2321
$this->dm = $dm;
2422
}
2523

26-
public function makeRoutes(BuilderUnitContext $buc)
24+
public function makeRoutes(RouteStack $routeStack)
2725
{
28-
$components = $buc->getPathComponents();
29-
30-
foreach ($components as $i => $component) {
31-
32-
$path .= '/'.$component;
26+
$paths = $routeStack->getFullPaths();
3327

28+
foreach ($paths as $path) {
3429
$doc = $this->dm->find(null, $path);
3530

3631
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
4432
$doc = new Generic;
45-
$doc->setNodename($component);
46-
$doc->setParent($parent);
47-
var_dump($parent);
48-
}
33+
$meta = $this->dm->getClassMetadata(get_class($doc));
34+
$meta->setIdentifierValue($doc, $path);
4935
}
5036

51-
$context->addRoute($doc);
37+
$routeStack->addRoute($doc);
5238
}
5339
}
5440
}

AutoRoute/RouteStack.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@ class RouteStack
99
{
1010
protected $pathElements;
1111
protected $routes = array();
12+
protected $context;
1213

1314
protected $closed = false;
1415

16+
public function __construct(BuilderContext $context)
17+
{
18+
$this->context = $context;
19+
}
20+
1521
public function addPathElements(array $pathElements)
1622
{
1723
foreach ($pathElements as $pathElement) {
@@ -45,6 +51,19 @@ public function getPaths()
4551
return $paths;
4652
}
4753

54+
public function getFullPaths()
55+
{
56+
$parentPath = $this->context->getRouteStackPath($this);
57+
58+
$paths = $this->getPaths();
59+
60+
array_walk($paths, function (&$path) use ($parentPath) {
61+
$path = $parentPath.'/'.$path;
62+
});
63+
64+
return $paths;
65+
}
66+
4867
public function getPath()
4968
{
5069
return implode('/', $this->pathElements);

0 commit comments

Comments
 (0)